Python(logging模块)

logging模块(47min)day18

1 import logging
2 
3 #WARNING(日志级别等级CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET)
4 logging.debug('debug message')
5 logging.info('info message')
6 
7 logging.warning('warning message')
8 logging.error('error message')
9 logging.critical('critical message')

执行结果

WARNING:root:warning message
ERROR:root:error message
CRITICAL:root:critical message

Process finished with exit code 0

前两项级别较低,不显示(在屏幕输出)。

日志级别等级CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET

 

配置日志级别,日志格式,输出位置

 1 import logging
 2 
 3 #WARNING(日志级别等级CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET)
 4 # logging.debug('debug message')
 5 # logging.info('info message')
 6 #
 7 # logging.warning('warning message')
 8 # logging.error('error message')
 9 # logging.critical('critical message')
10 
11 logging.basicConfig(level=logging.INFO,
12                     format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', #5个变量, lineno行号
13                     datefmt='%a, %d %b %Y %H:%M:%S',
14                     filename='test.log',#写成日志形式, 路径
15                     filemode='w')#不显示在屏幕上,写入文件 ,w刷新,设为a时,之前记录也保存
16 
17 logging.debug('debug message')
18 logging.info('info message')
19 
20 logging.warning('warning message')
21 logging.error('error message')
22 logging.critical('critical message')

执行结果:

Sat, 01 Sep 2018 11:17:18 logging_module.py[line:18] INFO info message
Sat, 01 Sep 2018 11:17:18 logging_module.py[line:20] WARNING warning message
Sat, 01 Sep 2018 11:17:18 logging_module.py[line:21] ERROR error message
Sat, 01 Sep 2018 11:17:18 logging_module.py[line:22] CRITICAL critical message

根据设置的日志格式显示日志,级别为INFO,路径为当前路径,得到设定路径的文件test.log(文件形式输出)。

 

logger对象(可同时在文件和屏幕输出)

 1 import logging
 2 
 3 logger = logging.getLogger()
 4 # 创建一个handler,用于写入日志文件
 5 fh = logging.FileHandler('test1.log') #文件对象
 6 
 7 # 再创建一个handler,用于输出到控制台
 8 ch = logging.StreamHandler() #屏幕对象
 9 
10 
11 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')#格式
12 
13 fh.setFormatter(formatter) #创建格式
14 ch.setFormatter(formatter)
15 
16 logger.addHandler(fh) #logger对象可以添加多个fh和ch对象 , 增加文件输出方式
17 logger.addHandler(ch)  #还增加了屏幕输出方式
18 #内容屏幕与文件的输出一致
19 
20 logger.setLevel(logging.INFO)
21 logger.debug('logger debug message')
22 logger.info('logger info message')
23 logger.warning('logger warning message')
24 logger.error('logger error message')
25 logger.critical('logger critical message')

执行结果:

屏幕,17行

2018-09-01 13:13:08,316 - root - INFO - logger info message
2018-09-01 13:13:08,317 - root - WARNING - logger warning message
2018-09-01 13:13:08,317 - root - ERROR - logger error message
2018-09-01 13:13:08,317 - root - CRITICAL - logger critical message

Process finished with exit code 0

文件(test1.log)中,16行

2018-09-01 13:23:22,764 - root - INFO - logger info message
2018-09-01 13:23:22,765 - root - WARNING - logger warning message
2018-09-01 13:23:22,765 - root - ERROR - logger error message
2018-09-01 13:23:22,765 - root - CRITICAL - logger critical message
内容屏幕与文件的输出一致

 

参考:http://www.cnblogs.com/yuanchenqi/articles/5732581.html

posted on 2018-09-01 13:25  嘟嘟嘟啦  阅读(172)  评论(0编辑  收藏  举报

导航