You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
2.6 KiB

# -*- coding: utf-8 -*-
"""
tests.test_logging
~~~~~~~~~~~~~~~~~~~
:copyright: © 2010 by the Pallets team.
:license: BSD, see LICENSE for more details.
"""
import logging
import sys
import pytest
from flask._compat import StringIO
from flask.logging import default_handler, has_level_handler, \
wsgi_errors_stream
@pytest.fixture(autouse=True)
def reset_logging(pytestconfig):
root_handlers = logging.root.handlers[:]
logging.root.handlers = []
root_level = logging.root.level
logger = logging.getLogger('flask.app')
logger.handlers = []
logger.setLevel(logging.NOTSET)
logging_plugin = pytestconfig.pluginmanager.unregister(
name='logging-plugin')
yield
logging.root.handlers[:] = root_handlers
logging.root.setLevel(root_level)
logger.handlers = []
logger.setLevel(logging.NOTSET)
if logging_plugin:
pytestconfig.pluginmanager.register(logging_plugin, 'logging-plugin')
def test_logger(app):
assert app.logger.name == 'flask.app'
assert app.logger.level == logging.NOTSET
assert app.logger.handlers == [default_handler]
def test_logger_debug(app):
app.debug = True
assert app.logger.level == logging.DEBUG
assert app.logger.handlers == [default_handler]
def test_existing_handler(app):
logging.root.addHandler(logging.StreamHandler())
assert app.logger.level == logging.NOTSET
assert not app.logger.handlers
def test_wsgi_errors_stream(app, client):
@app.route('/')
def index():
app.logger.error('test')
return ''
stream = StringIO()
client.get('/', errors_stream=stream)
assert 'ERROR in test_logging: test' in stream.getvalue()
assert wsgi_errors_stream._get_current_object() is sys.stderr
with app.test_request_context(errors_stream=stream):
assert wsgi_errors_stream._get_current_object() is stream
def test_has_level_handler():
logger = logging.getLogger('flask.app')
assert not has_level_handler(logger)
handler = logging.StreamHandler()
logging.root.addHandler(handler)
assert has_level_handler(logger)
logger.propagate = False
assert not has_level_handler(logger)
logger.propagate = True
handler.setLevel(logging.ERROR)
assert not has_level_handler(logger)
def test_log_view_exception(app, client):
@app.route('/')
def index():
raise Exception('test')
app.testing = False
stream = StringIO()
rv = client.get('/', errors_stream=stream)
assert rv.status_code == 500
assert rv.data
err = stream.getvalue()
assert 'Exception on / [GET]' in err
assert 'Exception: test' in err