logging日志
1 import logging 2 3 4 logging.basicConfig(filename='log.log', 5 format='%(asctime)s - %(name)s - %(levelname)s - %(module)s; %(message)s', 6 datefmt='%Y-%m-%d %H:%M:%S %p', 7 level=logging.INFO,) 8 ''' 9 CRITICAL = 50 10 FATAL = CRITICAL 11 ERROR = 40 12 WARNING = 30 13 WARN = WARNING 14 INFO = 20 15 DEBUG = 10 16 NOTSET = 0 17 ''' 18 logging.critical('c') 19 logging.fatal('f') 20 logging.error('e') 21 logging.warning('w') 22 logging.info('i') 23 logging.debug('d') 24 logging.log(logging.INFO,'333')
结果:
2017-02-21 07:59:55 AM - root - CRITICAL - s1; c
2017-02-21 07:59:55 AM - root - CRITICAL - s1; f
2017-02-21 07:59:55 AM - root - ERROR - s1; e
2017-02-21 07:59:55 AM - root - WARNING - s1; w
2017-02-21 07:59:55 AM - root - INFO - s1; i
2017-02-21 07:59:55 AM - root - INFO - s1; 333
多文件写入日志
1 import logging 2 3 # 创建文件 4 file_1_1 = logging.FileHandler('l1-1.log','a') 5 # 创建格式 6 fmt = logging.Formatter(fmt='%(asctime)s - %(name)s -%(levelname)s - %(module)s:%(message)s') 7 #文件应用格式 8 file_1_1.setFormatter(fmt) 9 file_1_2 = logging.FileHandler('l1-2.log','a') 10 fmt = logging.Formatter() 11 file_1_2.setFormatter(fmt) 12 13 # 定义日志 14 #定义阈值 15 logger1 = logging.Logger('s1',level=logging.ERROR) 16 logger1.addHandler(file_1_1) 17 logger1.addHandler(file_1_2) 18 19 #写日志 20 21 logger1.critical('111')