Python 是否有一种巧妙的方法将lithoxyl集成到flask.logger中?
我喜欢的外观,并希望逐步用它取代我目前使用的 有没有一个好办法让这两个日志框架共存 到目前为止,我有以下几点:Python 是否有一种巧妙的方法将lithoxyl集成到flask.logger中?,python,logging,flask,Python,Logging,Flask,我喜欢的外观,并希望逐步用它取代我目前使用的 有没有一个好办法让这两个日志框架共存 到目前为止,我有以下几点: from flask import current_app from werkzeug.local import LocalProxy logger = LocalProxy(lambda: current_app.logger) class LogAdaptor(object): """file-like object that will write messages t
from flask import current_app
from werkzeug.local import LocalProxy
logger = LocalProxy(lambda: current_app.logger)
class LogAdaptor(object):
"""file-like object that will write messages to the logger"""
def write(self, msg):
if msg.strip():
logger.info(msg)
from lithoxyl import StreamEmitter, SensibleFormatter
emtr = StreamEmitter(LogAdaptor())
fmtr = SensibleFormatter('{level_name_upper} {module_name} {end_message}')
# ... the rest is basically the same as http://lithoxyl.readthedocs.io/en/latest/overview.html#logging-sensibly
这或多或少起作用,但日志级别在输出中丢失,例如:
DEBUG backend: log message from app.logger.debug
INFO logger: CRITICAL "backend" "critical action failed"
INFO logger: DEBUG "backend" "action succeeded"
DEBUG backend: log message from app.logger.debug
我猜我们需要一个更复杂的发射器,或者一种访问Flask.logger处理程序中底层流的.write的方法(绕过格式化等)
或者这一切都是错误的,我应该继续使用拆分的日志文件,直到它全部重构为止?我已经设法做了一些改进,因为我发现并创建了一个类似的类:
import logging
from lithoxyl.common import DEBUG, INFO, CRITICAL, get_level
class LoggerEmitter(object):
priority_map = {DEBUG: {'success': logging.DEBUG,
'failure': logging.INFO,
'warn': logging.INFO,
'exception': logging.WARNING},
INFO: {'success': logging.INFO,
'failure': logging.WARNING,
'warn': logging.WARNING,
'exception': logging.ERROR},
CRITICAL: {'success': logging.WARNING,
'failure': logging.ERROR,
'warn': logging.ERROR,
'exception': logging.CRITICAL}}
def __init__(self, logger):
self.logger = logger
def on_begin(self, begin_event, entry):
level = self._get_level('begin', begin_event)
self.logger.log(level, entry)
def on_warn(self, warn_event, entry):
level = self._get_level('warn', warn_event)
self.logger.log(level, entry)
def on_end(self, end_event, entry):
level = self._get_level('end', end_event)
self.logger.log(level, entry)
def _get_level(self, event_name, action):
level = get_level(action.level)
if event_name == 'warn':
status = 'warn'
elif event_name == 'begin':
status = 'begin'
else:
status = action.status
return self.priority_map[level][status]
emtr = LoggerEmitter(logger)
现在的输出类似于:
DEBUG backend: log message from app.logger.debug
ERROR logger: "backend" "critical action failed"
DEBUG logger: "backend" "action succeeded"
DEBUG backend: log message from app.logger.debug
这更好,但如果模块名也能正确传递就更好了