Browse Source

Use a custom logger subclass that uses DEBUG level if in debug mode

pull/124/head
Armin Ronacher 15 years ago
parent
commit
d44b127748
  1. 12
      flask.py
  2. 5
      tests/flask_tests.py

12
flask.py

@ -867,7 +867,7 @@ class Flask(_PackageBoundObject):
#: .. versionadded:: 0.3 #: .. versionadded:: 0.3
debug_log_format = ( debug_log_format = (
'-' * 80 + '\n' + '-' * 80 + '\n' +
'%(levelname)s in %(module)s, %(pathname)s:%(lineno)d]:\n' + '%(levelname)s in %(module)s [%(pathname)s:%(lineno)d]:\n' +
'%(message)s\n' + '%(message)s\n' +
'-' * 80 '-' * 80
) )
@ -997,15 +997,19 @@ class Flask(_PackageBoundObject):
with _logger_lock: with _logger_lock:
if self._logger and self._logger.name == self.logger_name: if self._logger and self._logger.name == self.logger_name:
return self._logger return self._logger
from logging import getLogger, StreamHandler, Formatter, DEBUG from logging import getLogger, StreamHandler, Formatter, \
Logger, DEBUG
class DebugLogger(Logger):
def getEffectiveLevel(x):
return DEBUG if self.debug else Logger.getEffectiveLevel(x)
class DebugHandler(StreamHandler): class DebugHandler(StreamHandler):
def emit(x, record): def emit(x, record):
if self.debug: StreamHandler.emit(x, record) if self.debug else None
StreamHandler.emit(x, record)
handler = DebugHandler() handler = DebugHandler()
handler.setLevel(DEBUG) handler.setLevel(DEBUG)
handler.setFormatter(Formatter(self.debug_log_format)) handler.setFormatter(Formatter(self.debug_log_format))
logger = getLogger(self.logger_name) logger = getLogger(self.logger_name)
logger.__class__ = DebugLogger
logger.addHandler(handler) logger.addHandler(handler)
self._logger = logger self._logger = logger
return logger return logger

5
tests/flask_tests.py

@ -718,9 +718,11 @@ class LoggingTestCase(unittest.TestCase):
def test_debug_log(self): def test_debug_log(self):
app = flask.Flask(__name__) app = flask.Flask(__name__)
app.debug = True app.debug = True
@app.route('/') @app.route('/')
def index(): def index():
app.logger.warning('the standard library is dead') app.logger.warning('the standard library is dead')
app.logger.debug('this is a debug statement')
return '' return ''
@app.route('/exc') @app.route('/exc')
@ -731,9 +733,10 @@ class LoggingTestCase(unittest.TestCase):
with catch_stderr() as err: with catch_stderr() as err:
rv = c.get('/') rv = c.get('/')
out = err.getvalue() out = err.getvalue()
assert 'WARNING in flask_tests,' in out assert 'WARNING in flask_tests [' in out
assert 'flask_tests.py' in out assert 'flask_tests.py' in out
assert 'the standard library is dead' in out assert 'the standard library is dead' in out
assert 'this is a debug statement' in out
with catch_stderr() as err: with catch_stderr() as err:
try: try:

Loading…
Cancel
Save