今天研究了一下python标准库中的logging模块,在多个模块的情景下输出日志
import logging
from logging_test2 import log_test
# logging.basicConfig(filename='log_example.log',
# level=logging.DEBUG,
# filemode='w',
# )
# logging.debug('This message should go to the log file')
# logging.info('So should this')
# logging.warning('And this,too')
logger = logging.getLogger('test')
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler('log_example_2.log',mode='a',encoding='utf-8')
fh.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.ERROR)
formatter = logging.Formatter('%(asctime)s: %(levelname)s %(name)s %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
logger.addHandler(fh)
# logger.addHandler(ch)
logger.info('This is a info message