mirror of https://github.com/pallets/flask.git
Added a separate logging module
This commit is contained in:
parent
e5d8202038
commit
77e2fbf249
19
flask/app.py
19
flask/app.py
|
|
@ -300,22 +300,9 @@ class Flask(_PackageBoundObject):
|
|||
with _logger_lock:
|
||||
if self._logger and self._logger.name == self.logger_name:
|
||||
return self._logger
|
||||
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):
|
||||
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
|
||||
from flask.logging import create_logger
|
||||
self._logger = rv = create_logger(self)
|
||||
return rv
|
||||
|
||||
def create_jinja_environment(self):
|
||||
"""Creates the Jinja2 environment based on :attr:`jinja_options`
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -9,6 +9,7 @@
|
|||
:copyright: (c) 2010 by Armin Ronacher.
|
||||
:license: BSD, see LICENSE for more details.
|
||||
"""
|
||||
|
||||
from werkzeug import Client
|
||||
from flask import _request_ctx_stack
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue