简体   繁体   English

python记录器不写入文件

[英]python logger doesn't write into file

i write this logger我写这个记录器

import logging

class ThreadLogger:
    def __init__(self):
        self.logger = logging.getLogger("ThreadsLogging")
        f_handler = logging.FileHandler("logs/thread.log", mode="a")
        f_handler.setLevel(logging.INFO)
        f_format = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
        f_handler.setFormatter(f_format)
        self.logger.addHandler(f_handler)

    def info_log(self, msg):
        self.logger.info(msg)

But after call info_log(msg) file threads.log is empty.但是在调用info_log(msg)文件后, info_log(msg)是空的。 I change and delete mode in FileHandler but it dosen't help me我在 FileHandler 中更改和删除模式,但它对我没有帮助

Handlers and loggers both can have levels.处理程序和记录器都可以有级别。 You didn't set the level on the logger.您没有在记录器上设置级别。 Add self.logger.setLevel(logging.INFO) inside of your __init__ and it will work.__init__添加self.logger.setLevel(logging.INFO)它将起作用。

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

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