|
|
@ -11,8 +11,12 @@ |
|
|
|
|
|
|
|
|
|
|
|
from __future__ import absolute_import |
|
|
|
from __future__ import absolute_import |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import sys |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from werkzeug.local import LocalProxy |
|
|
|
from logging import getLogger, StreamHandler, Formatter, getLoggerClass, \ |
|
|
|
from logging import getLogger, StreamHandler, Formatter, getLoggerClass, \ |
|
|
|
DEBUG, ERROR |
|
|
|
DEBUG, ERROR |
|
|
|
|
|
|
|
from .globals import _request_ctx_stack |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PROD_LOG_FORMAT = '[%(asctime)s] %(levelname)s in %(module)s: %(message)s' |
|
|
|
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'] |
|
|
|
policy = app.config['LOGGER_HANDLER_POLICY'] |
|
|
|
if policy == mode or policy == 'always': |
|
|
|
if policy == mode or policy == 'always': |
|
|
|
return True |
|
|
|
return True |
|
|
@ -48,19 +64,19 @@ def create_logger(app): |
|
|
|
|
|
|
|
|
|
|
|
class DebugHandler(StreamHandler): |
|
|
|
class DebugHandler(StreamHandler): |
|
|
|
def emit(self, record): |
|
|
|
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) |
|
|
|
StreamHandler.emit(self, record) |
|
|
|
|
|
|
|
|
|
|
|
class ProductionHandler(StreamHandler): |
|
|
|
class ProductionHandler(StreamHandler): |
|
|
|
def emit(self, record): |
|
|
|
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) |
|
|
|
StreamHandler.emit(self, record) |
|
|
|
|
|
|
|
|
|
|
|
debug_handler = DebugHandler() |
|
|
|
debug_handler = DebugHandler() |
|
|
|
debug_handler.setLevel(DEBUG) |
|
|
|
debug_handler.setLevel(DEBUG) |
|
|
|
debug_handler.setFormatter(Formatter(DEBUG_LOG_FORMAT)) |
|
|
|
debug_handler.setFormatter(Formatter(DEBUG_LOG_FORMAT)) |
|
|
|
|
|
|
|
|
|
|
|
prod_handler = ProductionHandler() |
|
|
|
prod_handler = ProductionHandler(_proxy_stream) |
|
|
|
prod_handler.setLevel(ERROR) |
|
|
|
prod_handler.setLevel(ERROR) |
|
|
|
prod_handler.setFormatter(Formatter(PROD_LOG_FORMAT)) |
|
|
|
prod_handler.setFormatter(Formatter(PROD_LOG_FORMAT)) |
|
|
|
|
|
|
|
|
|
|
|