必备logging初始配置

import logging
import sys

logger = logging.getLogger()
logger.setLevel(logging.INFO)
rf_handler = logging.StreamHandler(sys.stderr)
rf_handler.setLevel(logging.DEBUG)
rf_handler.setFormatter(logging.Formatter("%(asctime)s - %(name)s - %(levelname)s %(message)s"))

f_handler = logging.FileHandler('log_path\\logs.log')
f_handler.setLevel(logging.INFO)
f_handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(message)s - %(filename)s[:%(lineno)d]"))

logger.addHandler(rf_handler)
logger.addHandler(f_handler)

logger.debug('debug messages')
logger.info('info messages')
logger.warning('warning messages')
logger.error('error messages')
logger.critical('critical messages')

上一篇:linux学习笔记8-查看文本文件


下一篇:ActiveMQ问题分析和解决