简体   繁体   English

Python TimedRotatingFileHandler 不写入文件

[英]Python TimedRotatingFileHandler not writing to file

I have a TimedRotatingFileHandler configured and I'm trying to get it to write to a file.我配置了一个 TimedRotatingFileHandler,我正在尝试让它写入文件。

if __name__ == '__main__':

    logger = logging.getLogger('testlog')

    logHandler = TimedRotatingFileHandler(filename="../logfile.log", when="midnight")
    logHandler.setLevel(logging.INFO)

    logFormatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
    logHandler.setFormatter(logFormatter)

    logger.addHandler(logHandler)

    print(logger.handlers)
    logger.info('hello')

The file gets created, but my statements aren't getting written.文件被创建,但我的语句没有被写入。 I've tried running this both in the IDE and from the command line.我已经尝试在 IDE 和命令行中运行它。

I also printed logger.handlers and got我还打印了 logger.handlers 并得到了

[<TimedRotatingFileHandler [MY_DIRECTORY]/logfile.log (INFO)>]

so I have no idea what's wrong.所以我不知道出了什么问题。 Any suggestions?有什么建议?

Figured it out.弄清楚了。 I was setting the level on the log handler:我在日志处理程序上设置级别:

loggerHandler.setLevel(logging.INFO)

instead of the Logger而不是记录器

logger.setLevel(logging.INFO)

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

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