python自定义日志模块

class LogConsole(object):

    def __init__(self):
        self.logger = logging.getLogger()
        self.logger.setLevel(logging.INFO)  # or whatever
        handler = logging.FileHandler('test111.log', 'a+', 'utf-8')  # or whatever
        formatter = logging.Formatter(fmt="%(asctime)s  %(message)s:"
                                                 , datefmt="%Y-%m-%d %H:%M:%S")  # or whatever
        handler.setFormatter(formatter )
        self.logger.addHandler(handler)

        # handler.close()

    def getlog(self):
        return self.logger

   
# logging.basicConfig(filename='./request.log', level=logging.INFO, filemode='a+',
#                     format='%(asctime)s;%(levelname)s;%(message)s', datefmt='%Y-%m-%d %H:%M:%S'
#                     , encoding="UTF-8")

查资料时候,不知道哪位神仙在设置时间格式时,该设置setFormatter,他丫的写了个=,导致时间一直不生效。手动吐血。

重要的事情说三遍!下边是错误的错误的错误的❌

handler.setFormatter = logging.Formatter(fmt='%(asctime)s;%(levelname)s;%(message)s', datefmt='%Y-%m-%d %H:%M:%S') # or whatever

你可能感兴趣的:(python自定义日志模块)