【Python】Python 日志实践之logging模块

前端之家收集整理的这篇文章主要介绍了【Python】Python 日志实践之logging模块前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

<p style="text-align: left; border: 0px; margin-top: 0px; margin-bottom: 20px; padding-top: 0px; padding-bottom: 0px; font-size: 15px; font-family: "Microsoft YaHei",宋体,"Myriad Pro",Lato,"Helvetica Neue",Helvetica,Arial,sans-serif;"><span style="color:#cc0000;">【笔记】

<p style="text-align: left; border: 0px; margin-top: 0px; margin-bottom: 20px; padding-top: 0px; padding-bottom: 0px; color: rgb(46,46,46); font-family: "Microsoft YaHei",sans-serif;"><span style="font-size:14px;">1.把logging配置文件提取出来,写在logging.yaml文件中加载,比json加载简单而且不会报错;

<p style="text-align: left; border: 0px; margin-top: 0px; margin-bottom: 20px; padding-top: 0px; padding-bottom: 0px; color: rgb(46,sans-serif;"><span style="font-size:14px;">2.修改getLogger参数,在每一个函数内部设置logger的名称

<p style="text-align: left; border: 0px; margin-top: 0px; margin-bottom: 20px; padding-top: 0px; padding-bottom: 0px; color: rgb(46,sans-serif;">

  • <span style="font-size:14px;">修改前:logger = logging.getLogger(name)
  • <span style="font-size:14px;">修改后:logger = logging.getLogger("Bar")
<p style="text-align: left; border: 0px; margin-top: 0px; margin-bottom: 20px; padding-top: 0px; padding-bottom: 0px; color: rgb(46,sans-serif;"><span style="font-size:14px;">3.修改配置文件中的日期格式:datefmt: "%m-%d %A %H:%M",效果如下:

<p style="text-align: left; border: 0px; margin-top: 0px; margin-bottom: 20px; padding-top: 0px; padding-bottom: 0px; color: rgb(46,sans-serif;">

  • <span style="font-size:14px;">修改前:2017-12-25 16:09:31,871 - foo - INFO - Hi,foo
  • <span style="font-size:14px;">修改后:12-25 Monday 16:15 - foo - INFO - Hi,foo、
  • <span style="font-size:14px;">或者,fmt = logging.Formatter('[%(asctime)s] [%(levelname)s] %(message)s','%Y-%m-%d %H:%M:%S')
<span style="font-size:14px;">4.在配置文件加载后,修改日志文件路径:

猜你在找的Python相关文章