普通日志生成logger代码:
import os
import time
import logging.config
from logging.handlers import TimedRotatingFileHandler
# 确定日志文件及等级
log_file_name = time.strftime("%Y-%m-%d", time.gmtime()) + '.log'
LOG_FILE = os.path.join(os.path.dirname(__file__), log_file_name)
LOG_LV = logging.INFO
# 设置日志format
simple_fmt = "%(asctime)s - %(levelname)s - %(filename)s - %(funcName)s: %(message)s"
thread_fmt = "%(asctime)s - %(levelname)s - %(filename)s - %(funcName)s - %(threadName)s: %(message)s"
simple_formatter = logging.Formatter(simple_fmt)
thread_formatter = logging.Formatter(thread_fmt)
# 设置屏幕输出format,生成console handler
basic_console_handler = logging.StreamHandler()
basic_console_handler.setFormatter(simple_formatter)
# 设置文件format,生成file handler
basic_file_handler = TimedRotatingFileHandler(LOG_FILE, when="midnight")
basic_file_handler.setFormatter(thread_formatter)
# 配置等级
logging.config.dictConfig({"disable_existing_loggers": False, "version": 1})
logging.root.setLevel(LOG_LV)
logger = logging.getLogger(__name__)
# 添加输出handler、文件handler
logger.addHandler(basic_console_handler)
logger.addHandler(basic_file_handler)
测试代码:
logger.info("this is info")
logger.debug("this is debug")
logger.warning("this is waring")
logger.info("another info")
logger.debug("another debug")
logger.warning("another warning")
logger.info("another2 info")
logger.debug("another2 debug")
logger.warning("another2 warning")
屏幕输出:
2022-04-27 10:27:15,899 - INFO - 2.py - <module>: this is info
2022-04-27 10:27:15,899 - WARNING - 2.py - <module>: this is waring
2022-04-27 10:27:16,902 - INFO - 2.py - <module>: another info
2022-04-27 10:27:16,902 - WARNING - 2.py - <module>: another warning
2022-04-27 10:27:17,903 - INFO - 2.py - <module>: another2 info
2022-04-27 10:27:17,903 - WARNING - 2.py - <module>: another2 warning
文件写入:
2022-04-27 10:27:15,899 - INFO - 2.py - <module> - MainThread: this is info
2022-04-27 10:27:15,899 - WARNING - 2.py - <module> - MainThread: this is waring
2022-04-27 10:27:16,902 - INFO - 2.py - <module> - MainThread: another info
2022-04-27 10:27:16,902 - WARNING - 2.py - <module> - MainThread: another warning
2022-04-27 10:27:17,903 - INFO - 2.py - <module> - MainThread: another2 info
2022-04-27 10:27:17,903 - WARNING - 2.py - <module> - MainThread: another2 warning
补充json日志配置:
from pythonjsonlogger import jsonlogger
json_console_handler = logging.StreamHandler()
json_console_handler.setFormatter(json_formatter)
json_console_handler.setLevel(LOG_LV)
json_file_handler = TimedRotatingFileHandler(LOG_FILE, when='midnight')
json_file_handler.setFormatter(json_formatter)
json_file_handler.setLevel(LOG_LV)
logging.root.addHandler(json_file_handler)
logging.root.addHandler(json_console_handler)
补充dictconfig配置相关:
import logging
import logging.config
LOGGING_CONFIG = {
"version": 1,
"formatters": {
"default": {
'format':'%(asctime)s %(filename)s %(lineno)s %(levelname)s %(message)s',
}
},
"handlers": {
"console": {
"class": "logging.StreamHandler",
"level": "INFO",
"formatter": "default",
}
},
"disable_existing_loggers": True,
"root": {
"handlers": ["console"],
"level": "DEBUG"
},
}
logging.config.dictConfig(LOGGING_CONFIG)
logger = logging.getLogger("console_logger")
# 测试代码
logger.debug('debug message')
logger.info('info message')
logger.warn('warning message')
logger.error('error message')
logger.critical('critical message')
|