20 python 初学(logging模块)

logging 模块:

# _author: lily
# _date: 2019/1/14
import logging

# logging.debug('debug message')
# logging.info('info message')
# logging.error('error message')
# logging.warning('warning message')
# logging.critical('critical message')

# logging.basicConfig(level=logging.DEBUG,
#                     format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
#                     datefmt='%a,%d %b %Y %H:%M:%S',
#                     filename='test.log',
#                     filemode='w')
#
# #Mon,14 Jan 2019 23:23:11 learn_of_logging_module.py[line:17] DEBUG debug message
# logging.debug('debug message')
# logging.info('info message')
# logging.error('error message')
# logging.warning('warning message')
# logging.critical('critical message')

logger = logging.getLogger()
# 创建一个handler,用于写入日志文件
fh = logging.FileHandler('test.log')   # 文件对象
# 在创建一个handler,用于输出到控制台
ch = logging.StreamHandler()  # 屏幕对象

formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

fh.setFormatter(formatter)
ch.setFormatter(formatter)

logger.addHandler(fh)
logger.addHandler(ch)

logger.setLevel(logging.DEBUG)

logger.debug('debug message')
logger.info('info message')
logger.warning('warning message')
logger.error('error message')
logger.critical('critical message')
View Code

猜你喜欢

转载自www.cnblogs.com/mlllily/p/10280131.html
今日推荐