简体   繁体   中英

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. I change and delete mode in FileHandler but it dosen't help me

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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