您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python中logging库的使用总结

5b51 2022/1/14 8:16:20 python 字数 7678 阅读 309 来源 www.jb51.cc/python

前言 最近因为工作的需要,在写一些python脚本,总是使用print来打印信息感觉很low,所以抽空研究了一下python的logging库,来优雅的来打印和记录日志,下面话不多说了,来一起看看详细的介绍吧。

概述

前言

最近因为工作的需要,在写一些python脚本,总是使用print来打印信息感觉很low,所以抽空研究了一下python的logging库,来优雅的来打印和记录日志,下面话不多说了,来一起看看详细的介绍吧。

一、简单的将日志打印到屏幕:

import logging

logging.debug('This is debug message') #debug
logging.info('This is info message')  #info
logging.warning('This is warning message') #warn

屏幕上打印:WARNING:root:This is warning message

认情况下,会打印WARNING级别的日志

二、通过basicConfig函数来对日志的输入格式和方法进行配置

import logging

logging.basicConfig(level=logging.DEBUG,format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',datefmt='%Y-%m-%d %a %H:%M:%s',filename='test.log',filemode='w')
 
logging.debug('This is debug message')
logging.info('This is info message')
logging.warning('This is warning message')

以上代码不会在屏幕上打印日志,而是会在当前目录生成test.log的日志文件,日志被打印在日志文件中:

2017-10-16 Mon 10:05:17 testlogging.py[line:25] DEBUG This is debug message

2017-10-16 Mon 10:05:17 testlogging.py[line:26] INFO This is info message

2017-10-16 Mon 10:05:17 testlogging.py[line:27] WARNING This is warning message

 %(levelno)s: 打印日志级别的数值
 %(levelname)s: 打印日志级别名称
 %(pathname)s: 打印当前执行程序的路径,其实就是sys.argv[0]
 %(filename)s: 打印当前执行程序名
 %(funcName)s: 打印日志的当前函数
 %(lineno)d: 打印日志的当前行号
 %(asctime)s: 打印日志的时间
 %(thread)d: 打印线程ID
 %(threadName)s: 打印线程名称
 %(process)d: 打印进程ID
 %(message)s: 打印日志信息

三、同时将日志输出到屏幕和日志文件

#定义一个StreamHandler,将INFO级别或更高的日志信息打印到标准错误,并将其添加到当前的日志处理对象
console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)

插入以上代码就可以同时在屏幕上打印出日志

屏幕上打印:

root : INFO This is info message

root : WARNING This is warning message

文件中打印:

2017-10-16 Mon 10:20:07 testlogging.py[line:46] DEBUG This is debug message

2017-10-16 Mon 10:20:07 testlogging.py[line:47] INFO This is info message

2017-10-16 Mon 10:20:07 testlogging.py[line:48] WARNING This is warning message

四、通过配置文件配置日志

#logger.conf
[loggers]
#定义logger模块,root是父类,必需存在的,其它的是自定义。 
keys=root,infoLogger,warnlogger

[logger_root] 
level=DEBUG    #level 级别,级别有DEBUG、INFO、WARNING、ERROR、CRITICAL
handlers=infohandler,warnhandler #handlers 处理类,可以有多个,用逗号分开

[logger_infoLogger]   #[logger_xxxx] logger_模块名称
handlers=infohandler
qualname=infoLogger   #qualname logger名称,应用程序通过 logging.getLogger获取。对于不能获取名称,则记录到root模块。
propagate=0    #propagate 是否继承父类的log信息,0:否 1:是

[logger_warnlogger]
handlers=warnhandler
qualname=warnlogger
propagate=0

###############################################
#定义handler
[handlers]
keys=infohandler,warnhandler

[handler_infohandler]
class=StreamHandler   #class handler类名
level=INFO    #level 日志级别
formatter=form02   #formatter,下面定义的formatter
args=(sys.stdout,)   #args handler初始化函数参数

[handler_warnhandler]
class=FileHandler
level=WARN
formatter=form01
args=('logs/deploylog.log','a')

###############################################
# 定义格式化输出
[formatters]
keys=form01,form02

[formatter_form01]
format=%(message)s %(asctime)s
datefmt=%Y-%m-%d %H:%M:%s

[formatter_form02]
format=%(asctime)s %(levelname)s %(message)s
datefmt=%Y-%m-%d %H:%M:%s

日志格式

测试配置文件

from logging.config import fileConfig

fileConfig('logger.conf')
logger=logging.getLogger('infoLogger')
logger.info('test1')
logger_error=logging.getLogger('warnhandler')
logger_error.warn('test5')

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对编程小技巧的支持

总结

以上是编程之家为你收集整理的python中logging库的使用总结全部内容,希望文章能够帮你解决python中logging库的使用总结所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶