Python学习_日志模块:logging

Posted Alive

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python学习_日志模块:logging相关的知识,希望对你有一定的参考价值。

  一、logging模块

作用:在逻辑容易出错位置手动添加日志功能,记录报错信息到文件,以被排错
特点:非自动记录,为人工指定位置,指定报错信息内容
提供五种模式:debug < info < warning < error < critical
默认从warning开始输出,可以根据需求
调整默认写入文件是追加a模式

二、logging模块的两种操作模式
  • 第一种方式是使用logging提供的模块级别的函数(low版,不可个性化)

  • 第二种方式是使用Logging日志系统的四大组件,对象的模式操作(高大上版,高耦合性,个性无极限) 

 1 # 第一种、普通日志记录,不能同时打印及写入日志
 2 impot logging
 3 logging.basicConfig(level=logging.DEBUG,
 4                     format=\'%(asctime)s %(filename)s [line:%(lineno)d] %(levelname)s %(message)s\',
 5                     filename=\'log2018.log\')  # 配置了文件名就写入文件
 6 logging.debug(\'message\')
 7 logging.info(\'message\')
 8 logging.warning(\'message\')
 9 logging.error(\'message\')
10 logging.critical(\'message\')
11 logging.log(logging.ERROR, \'this is a error test\')
12 # 自定义级别及信息:logging.log(level,mes)

 

 1 # 第二种、自定义配置日志
 2 # 同时打印到控制台及写入文件(分别创建fileHandler和streamHandler)
 3 # 自定义将日志内容按日志级别分割成多个文件写入(创建多个文件handler)
 4 # 自定义同时写入文件格式,内容,打印格式的格式和内容 (为各个handler创建不同的formatter格式化)
 5 # 自定义每个文件、打印到屏幕的日志级别(为各个handler设置不同的logging.setLevel(***))
 6 流程如下:
 7 1、创建logger对象     logging.getLogger()
 8 2、指定logger日志级别 logging.setLevel()
 9 3、创建handler        logging.FileHandler()
10 4、指定handler的日志级别(必须高于logger级别)  handler.setLevel()
11 5、创建handler的日志格式    custom_format = logging.Formatter()
12 6、指定handler的日志格式    handler.setFormatter(custom_format)
13 7、日志器logger调用handler处理器    obj.addHandler(handler)
14 8、配置各个日志级别的日志记录message  longging.debug(msg)
15 
16 \'\'\'
17 import logging
18 # 1、通过日志器logger实例化对象,调用方法getLogger()
19 log_obj = logging.getLogger(__name__)
20 
21 # 2、设置日志器logger即全局日志级别(可省略,默认从warning开始,如要设置必须高于其级别即INFO开始)
22 log_obj.setLevel(logging.INFO)
23 
24 # 3、创建各个需求的Handler句柄(本例设置三个,一个写入error错误信息的文件,一个写入全部日志的文件,一个打印到控制台)
25 error_fh = logging.FileHandler(\'err.log\', \'a\', encoding=\'UTF-8\')  # 默认追加,encoding按当前默认编码
26 all_fh = logging.FileHandler(\'all.log\')
27 stream_sh = logging.StreamHandler()      # 打印出来没有指定目的地一说
28 
29 # 4、为每个handler句柄配置日志记录级别(如果不设置默认继承日志器logger的日志级别)
30 error_fh.setLevel(logging.ERROR)
31 all_fh.setLevel(logging.WARNING)
32 stream_sh.setLevel(logging.WARNING)
33 
34 # 5、为每个handler句柄配置日志格式(可以配置一个,其他都用一个,也可以各个配置)
35 err_format = logging.Formatter(\'%(asctime)s-%(filename)s-[line:%(lineno)d]-%(levelname)s-%(message)s\')
36 stream_format = logging.Formatter(\'%(asctime)s-[line:%(lineno)d]-%(levelname)s-%(message)s\')
37 
38 # 6、为每个handler句柄配置日志格式
39 error_fh.setFormatter(err_format)
40 all_fh.setFormatter(err_format)
41 stream_sh.setFormatter(stream_format)
42 
43 # 7、日志器logger调用处理器handler,相结合
44 log_obj.addHandler(error_fh)
45 log_obj.addHandler(all_fh)
46 log_obj.addHandler(stream_sh)
47 
48 # 8、为各个日志级别设置日志信息
49 logging.debug(\'debug:message2018\')
50 logging.info(\'info:message2019\')
51 logging.warning(\'waring:message2020\')
52 logging.error(\'error:message2088\')
53 logging.critical(\'critical:message2099\')

 

 想了解详细见:https://www.cnblogs.com/sunxiuwen/articles/9285938.html

logging的handlers见:https://www.cnblogs.com/xuzijie/p/9755216.html

https://blog.csdn.net/energysober/article/details/53263295

https://blog.csdn.net/xbean1028/article/details/105991835

https://blog.csdn.net/weixin_38107388/article/details/90639151?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param

以上是关于Python学习_日志模块:logging的主要内容,如果未能解决你的问题,请参考以下文章

python 的日志logging模块学习

python的日志模块:logging;django的日志系统

logging日志模块_python

logging --- Python 的日志记录工具

python(38):日志logging模块学习

python 的日志logging模块学习