Python log processor, print to console and save to file at the same time, and ensure consistent format

When using the logging module, the default output is to the console. Of course, you can also configure the output to a file. However, when you configure the file, the console output disappears. Therefore, you need a strategy to save it to the file. , and can be output to the console.

The following is the optimization I made: the logging module will create a processor by default, so you only need to create a console processor.

import logging

# 第一步:创建文件日志对象
logger = logging.getLogger()
# 第二步:创建文件日志处理器,默认logging会自己创建一个处理器
file_fmt = "%(asctime)s - %(levelname)s - %(message)s"
logging.basicConfig(level=logging.DEBUG, format=file_fmt, filename="./log.txt", filemode="a", encoding="utf-8")
console_handler = logging.StreamHandler()
# 第三步:添加控制台文本处理器
console_handler.setLevel(level=logging.DEBUG)
console_fmt = "%(asctime)s - %(levelname)s - %(message)s"
fmt1 = logging.Formatter(fmt=console_fmt)
console_handler.setFormatter(fmt=fmt1)
# 第四步:将控制台日志器、文件日志器,添加进日志器对象中
logger.addHandler(console_handler)

if __name__ == '__main__':
    logger.info("这是一条info消息")

Achieved effect: The format in the file is the same as that in the console

Guess you like

Origin blog.csdn.net/weixin_44786530/article/details/133035214