Updated the logging documentation for recent changes.
This commit is contained in:
parent
5edc803702
commit
7c16054d81
2 changed files with 33 additions and 18 deletions
|
|
@ -11,8 +11,12 @@
|
|||
|
||||
from __future__ import absolute_import
|
||||
|
||||
import sys
|
||||
|
||||
from werkzeug.local import LocalProxy
|
||||
from logging import getLogger, StreamHandler, Formatter, getLoggerClass, \
|
||||
DEBUG, ERROR
|
||||
from .globals import _request_ctx_stack
|
||||
|
||||
|
||||
PROD_LOG_FORMAT = '[%(asctime)s] %(levelname)s in %(module)s: %(message)s'
|
||||
|
|
@ -24,7 +28,19 @@ DEBUG_LOG_FORMAT = (
|
|||
)
|
||||
|
||||
|
||||
def should_log_for(app, mode):
|
||||
@LocalProxy
|
||||
def _proxy_stream():
|
||||
"""Finds the most appropriate error stream for the application. If a
|
||||
WSGI request is in flight we log to wsgi.errors, otherwise this resolves
|
||||
to sys.stderr.
|
||||
"""
|
||||
ctx = _request_ctx_stack.top
|
||||
if ctx is not None:
|
||||
return ctx.request.environ['wsgi.errors']
|
||||
return sys.stderr
|
||||
|
||||
|
||||
def _should_log_for(app, mode):
|
||||
policy = app.config['LOGGER_HANDLER_POLICY']
|
||||
if policy == mode or policy == 'always':
|
||||
return True
|
||||
|
|
@ -48,19 +64,19 @@ def create_logger(app):
|
|||
|
||||
class DebugHandler(StreamHandler):
|
||||
def emit(self, record):
|
||||
if app.debug and should_log_for(app, 'debug'):
|
||||
if app.debug and _should_log_for(app, 'debug'):
|
||||
StreamHandler.emit(self, record)
|
||||
|
||||
class ProductionHandler(StreamHandler):
|
||||
def emit(self, record):
|
||||
if not app.debug and should_log_for(app, 'production'):
|
||||
if not app.debug and _should_log_for(app, 'production'):
|
||||
StreamHandler.emit(self, record)
|
||||
|
||||
debug_handler = DebugHandler()
|
||||
debug_handler.setLevel(DEBUG)
|
||||
debug_handler.setFormatter(Formatter(DEBUG_LOG_FORMAT))
|
||||
|
||||
prod_handler = ProductionHandler()
|
||||
prod_handler = ProductionHandler(_proxy_stream)
|
||||
prod_handler.setLevel(ERROR)
|
||||
prod_handler.setFormatter(Formatter(PROD_LOG_FORMAT))
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue