forked from orbit-oss/flask
simplify logging configuration
single default handler and formatter don't remove handlers configure level once using setLevel document logging reorganize logging tests
This commit is contained in:
parent
85fa8aabf5
commit
66b1b752da
13 changed files with 399 additions and 451 deletions
|
|
@ -738,7 +738,6 @@ def test_teardown_request_handler_debug_mode(app, client):
|
|||
|
||||
def test_teardown_request_handler_error(app, client):
|
||||
called = []
|
||||
app.config['LOGGER_HANDLER_POLICY'] = 'never'
|
||||
app.testing = False
|
||||
|
||||
@app.teardown_request
|
||||
|
|
@ -814,7 +813,6 @@ def test_before_after_request_order(app, client):
|
|||
|
||||
|
||||
def test_error_handling(app, client):
|
||||
app.config['LOGGER_HANDLER_POLICY'] = 'never'
|
||||
app.testing = False
|
||||
|
||||
@app.errorhandler(404)
|
||||
|
|
@ -860,7 +858,6 @@ def test_error_handler_unknown_code(app):
|
|||
|
||||
|
||||
def test_error_handling_processing(app, client):
|
||||
app.config['LOGGER_HANDLER_POLICY'] = 'never'
|
||||
app.testing = False
|
||||
|
||||
@app.errorhandler(500)
|
||||
|
|
@ -882,7 +879,6 @@ def test_error_handling_processing(app, client):
|
|||
|
||||
|
||||
def test_baseexception_error_handling(app, client):
|
||||
app.config['LOGGER_HANDLER_POLICY'] = 'never'
|
||||
app.testing = False
|
||||
|
||||
@app.route('/')
|
||||
|
|
@ -1021,6 +1017,34 @@ def test_trapping_of_all_http_exceptions(app, client):
|
|||
client.get('/fail')
|
||||
|
||||
|
||||
def test_error_handler_after_processor_error(app, client):
|
||||
app.testing = False
|
||||
|
||||
@app.before_request
|
||||
def before_request():
|
||||
if trigger == 'before':
|
||||
1 // 0
|
||||
|
||||
@app.after_request
|
||||
def after_request(response):
|
||||
if trigger == 'after':
|
||||
1 // 0
|
||||
return response
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return 'Foo'
|
||||
|
||||
@app.errorhandler(500)
|
||||
def internal_server_error(e):
|
||||
return 'Hello Server Error', 500
|
||||
|
||||
for trigger in 'before', 'after':
|
||||
rv = client.get('/')
|
||||
assert rv.status_code == 500
|
||||
assert rv.data == b'Hello Server Error'
|
||||
|
||||
|
||||
def test_enctype_debug_helper(app, client):
|
||||
from flask.debughelpers import DebugFilesKeyError
|
||||
app.debug = True
|
||||
|
|
@ -1425,7 +1449,6 @@ def test_test_app_proper_environ(app, client):
|
|||
|
||||
def test_exception_propagation(app, client):
|
||||
def apprunner(config_key):
|
||||
app.config['LOGGER_HANDLER_POLICY'] = 'never'
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
|
|
|
|||
|
|
@ -9,20 +9,19 @@
|
|||
:license: BSD, see LICENSE for more details.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
|
||||
import datetime
|
||||
import os
|
||||
import uuid
|
||||
import datetime
|
||||
|
||||
import flask
|
||||
from logging import StreamHandler
|
||||
import pytest
|
||||
from werkzeug.datastructures import Range
|
||||
from werkzeug.exceptions import BadRequest, NotFound
|
||||
from werkzeug.http import parse_cache_control_header, parse_options_header
|
||||
from werkzeug.http import http_date
|
||||
from werkzeug.http import http_date, parse_cache_control_header, \
|
||||
parse_options_header
|
||||
|
||||
import flask
|
||||
from flask._compat import StringIO, text_type
|
||||
from flask.helpers import get_debug_flag, make_response
|
||||
from flask.helpers import get_debug_flag
|
||||
|
||||
|
||||
def has_encoding(name):
|
||||
|
|
@ -660,94 +659,7 @@ class TestSendfile(object):
|
|||
flask.send_from_directory('static', 'bad\x00')
|
||||
|
||||
|
||||
class TestLogging(object):
|
||||
def test_logger_cache(self):
|
||||
app = flask.Flask(__name__)
|
||||
logger1 = app.logger
|
||||
assert app.logger is logger1
|
||||
assert logger1.name == __name__
|
||||
app.logger_name = __name__ + '/test_logger_cache'
|
||||
assert app.logger is not logger1
|
||||
|
||||
def test_debug_log(self, capsys, app, client):
|
||||
app.debug = True
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
app.logger.warning('the standard library is dead')
|
||||
app.logger.debug('this is a debug statement')
|
||||
return ''
|
||||
|
||||
@app.route('/exc')
|
||||
def exc():
|
||||
1 // 0
|
||||
|
||||
with client:
|
||||
client.get('/')
|
||||
out, err = capsys.readouterr()
|
||||
assert 'WARNING in test_helpers [' in err
|
||||
assert os.path.basename(__file__.rsplit('.', 1)[0] + '.py') in err
|
||||
assert 'the standard library is dead' in err
|
||||
assert 'this is a debug statement' in err
|
||||
|
||||
with pytest.raises(ZeroDivisionError):
|
||||
client.get('/exc')
|
||||
|
||||
def test_debug_log_override(self, app):
|
||||
app.debug = True
|
||||
app.logger_name = 'flask_tests/test_debug_log_override'
|
||||
app.logger.level = 10
|
||||
assert app.logger.level == 10
|
||||
|
||||
def test_exception_logging(self, app, client):
|
||||
out = StringIO()
|
||||
app.config['LOGGER_HANDLER_POLICY'] = 'never'
|
||||
app.logger_name = 'flask_tests/test_exception_logging'
|
||||
app.logger.addHandler(StreamHandler(out))
|
||||
app.testing = False
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
1 // 0
|
||||
|
||||
rv = client.get('/')
|
||||
assert rv.status_code == 500
|
||||
assert b'Internal Server Error' in rv.data
|
||||
|
||||
err = out.getvalue()
|
||||
assert 'Exception on / [GET]' in err
|
||||
assert 'Traceback (most recent call last):' in err
|
||||
assert '1 // 0' in err
|
||||
assert 'ZeroDivisionError:' in err
|
||||
|
||||
def test_processor_exceptions(self, app, client):
|
||||
app.config['LOGGER_HANDLER_POLICY'] = 'never'
|
||||
app.testing = False
|
||||
|
||||
@app.before_request
|
||||
def before_request():
|
||||
if trigger == 'before':
|
||||
1 // 0
|
||||
|
||||
@app.after_request
|
||||
def after_request(response):
|
||||
if trigger == 'after':
|
||||
1 // 0
|
||||
return response
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
return 'Foo'
|
||||
|
||||
@app.errorhandler(500)
|
||||
def internal_server_error(e):
|
||||
return 'Hello Server Error', 500
|
||||
|
||||
for trigger in 'before', 'after':
|
||||
rv = client.get('/')
|
||||
assert rv.status_code == 500
|
||||
assert rv.data == b'Hello Server Error'
|
||||
|
||||
class TestUrlFor(object):
|
||||
def test_url_for_with_anchor(self, app, req_ctx):
|
||||
|
||||
@app.route('/')
|
||||
|
|
|
|||
91
tests/test_logging.py
Normal file
91
tests/test_logging.py
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
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(monkeypatch):
|
||||
root_handlers = logging.root.handlers[:]
|
||||
root_level = logging.root.level
|
||||
|
||||
logger = logging.getLogger('flask.app')
|
||||
logger.handlers = []
|
||||
logger.setLevel(logging.NOTSET)
|
||||
|
||||
yield
|
||||
|
||||
logging.root.handlers[:] = root_handlers
|
||||
logging.root.setLevel(root_level)
|
||||
|
||||
logger.handlers = []
|
||||
logger.setLevel(logging.NOTSET)
|
||||
|
||||
|
||||
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
|
||||
|
|
@ -9,8 +9,8 @@
|
|||
:copyright: (c) 2015 by Armin Ronacher.
|
||||
:license: BSD, see LICENSE for more details.
|
||||
"""
|
||||
|
||||
import flask
|
||||
from logging import StreamHandler
|
||||
|
||||
from flask._compat import StringIO
|
||||
|
||||
|
|
@ -22,16 +22,12 @@ def test_suppressed_exception_logging():
|
|||
|
||||
out = StringIO()
|
||||
app = SuppressedFlask(__name__)
|
||||
app.logger_name = 'flask_tests/test_suppressed_exception_logging'
|
||||
app.logger.addHandler(StreamHandler(out))
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
1 // 0
|
||||
raise Exception('test')
|
||||
|
||||
rv = app.test_client().get('/')
|
||||
rv = app.test_client().get('/', errors_stream=out)
|
||||
assert rv.status_code == 500
|
||||
assert b'Internal Server Error' in rv.data
|
||||
|
||||
err = out.getvalue()
|
||||
assert err == ''
|
||||
assert not out.getvalue()
|
||||
|
|
|
|||
|
|
@ -399,7 +399,7 @@ def test_templates_auto_reload_debug_run(app, monkeypatch):
|
|||
assert app.jinja_env.auto_reload == True
|
||||
|
||||
|
||||
def test_template_loader_debugging(test_apps):
|
||||
def test_template_loader_debugging(test_apps, monkeypatch):
|
||||
from blueprintapp import app
|
||||
|
||||
called = []
|
||||
|
|
@ -419,19 +419,15 @@ def test_template_loader_debugging(test_apps):
|
|||
assert 'See http://flask.pocoo.org/docs/blueprints/#templates' in text
|
||||
|
||||
with app.test_client() as c:
|
||||
try:
|
||||
old_load_setting = app.config['EXPLAIN_TEMPLATE_LOADING']
|
||||
old_handlers = app.logger.handlers[:]
|
||||
app.logger.handlers = [_TestHandler()]
|
||||
app.config['EXPLAIN_TEMPLATE_LOADING'] = True
|
||||
monkeypatch.setitem(app.config, 'EXPLAIN_TEMPLATE_LOADING', True)
|
||||
monkeypatch.setattr(
|
||||
logging.getLogger('flask'), 'handlers', [_TestHandler()]
|
||||
)
|
||||
|
||||
with pytest.raises(TemplateNotFound) as excinfo:
|
||||
c.get('/missing')
|
||||
with pytest.raises(TemplateNotFound) as excinfo:
|
||||
c.get('/missing')
|
||||
|
||||
assert 'missing_template.html' in str(excinfo.value)
|
||||
finally:
|
||||
app.logger.handlers[:] = old_handlers
|
||||
app.config['EXPLAIN_TEMPLATE_LOADING'] = old_load_setting
|
||||
assert 'missing_template.html' in str(excinfo.value)
|
||||
|
||||
assert len(called) == 1
|
||||
|
||||
|
|
|
|||
|
|
@ -207,7 +207,6 @@ def test_session_transaction_needs_cookies(app):
|
|||
|
||||
|
||||
def test_test_client_context_binding(app, client):
|
||||
app.config['LOGGER_HANDLER_POLICY'] = 'never'
|
||||
app.testing = False
|
||||
|
||||
@app.route('/')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue