记录处理程序为空-为什么记录TimeRoatingFileHandler不起作用 [英] Logging Handlers Empty - Why Logging TimeRoatingFileHandler doesn't work
问题描述
因此,我执行logging.config.fileConfig来从具有控制台和文件处理程序的文件配置中设置日志记录.然后,我执行logging.getLogger(name)来获取记录器和日志.在某些时候,我想更改文件处理程序的文件名,即日志旋转(由于Windows平台存在一些问题,我无法使用时间旋转器),因此我将其称为logger.handlers-它显示了一个空列表,因此我无法关闭它们!!但是,当我逐步通过调试器时,它显然不是空的(当然,如果没有它,我将无法正确登录)
So I do logging.config.fileConfig to setup my logging from a file config that has console and file handler. Then I do logging.getLogger(name) to get my logger and log. At certain times I want the filehandler's filename to change i.e. log rotate (I can't use time rotator because of some issues with Windows platform) so to do that I call logger.handlers - it shows an empty list, so I cant close them!! However when I step through the debugger, its clearly not empty (well of course without it I wouldn't be able to log right)
不知道这是怎么回事,我缺少任何陷阱吗?
Not sure whats going on here, any gotchas that I'm missing?
感谢任何帮助.谢谢.
推荐答案
似乎您需要正确获取root记录器:
It seems you need to correctly get the root logger:
logger = logging.getLogger(__name__)
handlers = logger.handlers[:]
print('module {}'.format(handlers))
print('module {}'.format(logger.hasHandlers()))
logger = logging.getLogger('root')
handlers = logger.handlers[:]
print('root {}'.format(handlers))
print('root {}'.format(logger.hasHandlers()))
logger = logging.getLogger()
handlers = logger.handlers[:]
print('blank {}'.format(handlers))
print('blank {}'.format(logger.hasHandlers()))
输出:
模块[]
module []
模块True
根[]
为True的
空白[<logging.handlers.RotatingFileHandler object at 0x108d82898>,
<logging.StreamHandler object at 0x108d826d8>]
blank [<logging.handlers.RotatingFileHandler object at 0x108d82898>,
<logging.StreamHandler object at 0x108d826d8>]
空白为真
这篇关于记录处理程序为空-为什么记录TimeRoatingFileHandler不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!