【发布时间】:2013-02-19 13:31:43
【问题描述】:
我有一个 Flask Web 应用程序,Python 日志记录配置是在应用程序启动时通过 dictConfig 完成的。将某些日志写入数据库的处理程序附加到记录器'test.module' 仅当在应用程序启动时还调用logging.basicConfig(level=logging.DEBUG) 时,才会将写入该记录器的日志写入数据库。否则,不会将任何日志写入数据库。我知道 basicConfig 只是将一个 streamHandler 附加到根记录器。我认为这应该无关紧要,因为我不希望根记录器做任何事情。为什么没有 basicConfig 就不行?
我在下面添加了我如何启动记录器和我的配置。
class DbHandler(logging.Handler):
def __init__(self, level=logging.NOTSET):
logging.Handler.__init__(self, level)
def emit(self, record):
record.message = self.format(record)
log = DbModel()
log.message = record.message
log.save()
LOGGING = {
'version': 1,
'handlers': {
'db_log': {
'level': 'DEBUG',
'class': 'test.handlers.DbHandler',
},
},
'loggers': {
'test.important_module': {
'handlers': [
'db_log'
],
},
}
# logging.basicConfig(level=logging.DEBUG) # Doesnt work without this
logging.config.dictConfig(LOGGING)
logger = logging.getLogger('test.important_module')
logger.info('Making a test')
【问题讨论】:
-
您能否提供用于设置记录器的代码以及它与执行实际记录的代码有何关系?到目前为止,您提供的内容有些模糊...
-
@isedev 我现在添加了它
-
仅从可用性的角度来看,我会重新考虑让处理程序修改
record.message作为副作用。直接设置log.message = self.format(record)即可。
标签: python logging java.util.logging