简体   繁体   English

记录处理程序为空 - 为什么记录TimeRoatingFileHandler不起作用

[英]Logging Handlers Empty - Why Logging TimeRoatingFileHandler doesn't work

So I do logging.config.fileConfig to setup my logging from a file config that has console and file handler. 所以我做logging.config.fileConfig从具有控制台和文件处理程序的文件配置中设置我的日志记录。 Then I do logging.getLogger(name) to get my logger and log. 然后我做logging.getLogger(name)来获取我的记录器和日志。 At certain times I want the filehandler's filename to change ie 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!! 在某些时候,我希望文件处理程序的文件名更改即日志旋转(由于Windows平台的一些问题,我不能使用时间旋转器)所以要做到这一点我调用logger.handlers - 它显示一个空列表,所以我不能关闭它们! 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? 不知道这里发生什么,我遗失的任何陷阱?

Appreciate any help. 感谢任何帮助。 Thanks. 谢谢。

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()))

output: 输出:

module [] 模块[]

module True 模块真的

root [] 根 []

root True root真的

blank [<logging.handlers.RotatingFileHandler object at 0x108d82898>, <logging.StreamHandler object at 0x108d826d8>] 空白[<logging.handlers.RotatingFileHandler object at 0x108d82898>, <logging.StreamHandler object at 0x108d826d8>]

blank True 空白真的

Firstly the issue is that, if you use a config file to initialise logging with file and console handlers, then it does not populate logging.handlers list, so you can not iterate over it and close+flush the streams prior to opening new one with a new logging file name. 首先问题是,如果你使用配置文件来初始化使用文件和控制台处理程序的日志记录,那么它不会填充logging.handlers列表,所以你不能迭代它并关闭+刷新流之前打开新的新的日志文件名。

If you want to use TimeRotatingFileHandler or RotatingFileHandler, it sits under logging/handler.py and when it tries to do a roll over, it only closes its own stream, as it has no idea what streams the parent logging (mostly singleton) class may have open. 如果你想使用TimeRotatingFileHandler或RotatingFileHandler,它位于logging / handler.py下,当它尝试进行翻转时,它只关闭自己的流,因为它不知道父日志(主要是单例)类可能是什么流开放。 And so when you do a roll over, there is a file lock (file filehandler) and boom it all fails. 因此,当你进行翻转时,会有一个文件锁(文件文件处理程序)并且它都会失败。

So the solution (for me) is to initialise logging programatically and use addHandlers on logging, which also populates logging.handlers [], which I then use to iterate over my console/file handler and close them prior to manually rotating the file. 所以解决方案(对我来说)是以编程方式初始化日志记录并在日志记录中使用addHandlers,它还会填充logging.handlers [],然后我会使用它来迭代我的控制台/文件处理程序并在手动旋转文件之前关闭它们。

It to me looks like an obvious bug with the logging class, and if its working on unix - it really shouldn't. 它对我来说看起来像是一个明显的日志类错误,如果它在unix上工作 - 它真的不应该。

Thanks everyone, especially @falsetru for your help. 谢谢大家,特别是@falsetru的帮助。

You can use RotatingFileHandler (not TimedRotatingFileHandler ). 您可以使用RotatingFileHandler (而不是TimedRotatingFileHandler )。

Calling doRollover of the handler will rotate the log files. 调用处理程序的doRollover将旋转日志文件。

Maybe there is no such name as 'TimeRoatingFileHandler' because you missed 'd' in word 'Timed'. 也许没有'TimeRoatingFileHandler'这样的名字,因为你在'Timed'这个词中错过了'd'。 So it must be: 'TimedRoatingFileHandler' 所以一定是:'TimedRoatingFileHandler'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM