Browse Source

Added a separate logging module

pull/112/head
Armin Ronacher 15 years ago
parent
commit
77e2fbf249
  1. 19
      flask/app.py
  2. 42
      flask/logging.py
  3. 1
      flask/testing.py

19
flask/app.py

@ -300,22 +300,9 @@ 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, \ from flask.logging import create_logger
Logger, DEBUG self._logger = rv = create_logger(self)
class DebugLogger(Logger): return rv
def getEffectiveLevel(x):
return DEBUG if self.debug else Logger.getEffectiveLevel(x)
class DebugHandler(StreamHandler):
def emit(x, record):
StreamHandler.emit(x, record) if self.debug else None
handler = DebugHandler()
handler.setLevel(DEBUG)
handler.setFormatter(Formatter(self.debug_log_format))
logger = getLogger(self.logger_name)
logger.__class__ = DebugLogger
logger.addHandler(handler)
self._logger = logger
return logger
def create_jinja_environment(self): def create_jinja_environment(self):
"""Creates the Jinja2 environment based on :attr:`jinja_options` """Creates the Jinja2 environment based on :attr:`jinja_options`

42
flask/logging.py

@ -0,0 +1,42 @@
# -*- coding: utf-8 -*-
"""
flask.logging
~~~~~~~~~~~~~
Implements the logging support for Flask.
:copyright: (c) 2010 by Armin Ronacher.
:license: BSD, see LICENSE for more details.
"""
from __future__ import absolute_import
from logging import getLogger, StreamHandler, Formatter, Logger, DEBUG
def create_logger(app):
"""Creates a logger for the given application. This logger works
similar to a regular Python logger but changes the effective logging
level based on the application's debug flag. Furthermore this
function also removes all attached handlers in case there was a
logger with the log name before.
"""
class DebugLogger(Logger):
def getEffectiveLevel(x):
return DEBUG if app.debug else Logger.getEffectiveLevel(x)
class DebugHandler(StreamHandler):
def emit(x, record):
StreamHandler.emit(x, record) if app.debug else None
handler = DebugHandler()
handler.setLevel(DEBUG)
handler.setFormatter(Formatter(app.debug_log_format))
logger = getLogger(app.logger_name)
# just in case that was not a new logger, get rid of all the handlers
# already attached to it.
del logger.handlers[:]
logger.__class__ = DebugLogger
logger.addHandler(handler)
return logger

1
flask/testing.py

@ -9,6 +9,7 @@
:copyright: (c) 2010 by Armin Ronacher. :copyright: (c) 2010 by Armin Ronacher.
:license: BSD, see LICENSE for more details. :license: BSD, see LICENSE for more details.
""" """
from werkzeug import Client from werkzeug import Client
from flask import _request_ctx_stack from flask import _request_ctx_stack

Loading…
Cancel
Save