Python之logging模块重定向
对于代码量较大的工程,建议使用logging模块进行输出。该模块是线程安全的,可将日志信息输出到控制台、写入文件、使用TCP/UDP协议发送到网络等等。
默认情况下logging模块将日志输出到控制台(标准出错),且只显示大于或等于设置的日志级别的日志。日志级别由高到低为CRITICAL>ERROR>WARNING>INFO>DEBUG>NOTSET,默认级别为WARNING。
以下示例将日志信息分别输出到控制台和写入文件:
importlogging
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s[%(levelname)s]at%(filename)s,%(lineno)d:%(message)s',
datefmt='%Y-%m-%d(%a)%H:%M:%S',
filename='out.txt',
filemode='w')
#将大于或等于INFO级别的日志信息输出到StreamHandler(默认为标准错误)
console=logging.StreamHandler()
console.setLevel(logging.INFO)
formatter=logging.Formatter('[%(levelname)-8s]%(message)s')#屏显实时查看,无需时间
console.setFormatter(formatter)
logging.getLogger().addHandler(console)
logging.debug('gubed');logging.info('ofni');logging.critical('lacitirc')
通过对多个handler设置不同的level参数,可将不同的日志内容输入到不同的地方。本例使用在logging模块内置的StreamHandler(和FileHandler),运行后屏幕上显示:
[INFO]ofni
[CRITICAL]lacitirc
out.txt文件内容则为:
2022-04-22(Fri)17:10:53[DEBUG]attest.py,25:gubed
2022-04-22(Fri)17:10:53[INFO]attest.py,25:ofni
2022-04-22(Fri)17:10:53[CRITICAL]attest.py,25:lacitirc
除直接在程序中设置Logger、Handler、Formatter等外,还可将这些信息写入配置文件。示例如下:
#logger.conf
###############Logger###############
[loggers]
keys=root,Logger2F,Logger2CF
[logger_root]
level=DEBUG
handlers=hWholeConsole
[logger_Logger2F]
handlers=hWholeFile
qualname=Logger2F
propagate=0
[logger_Logger2CF]
handlers=hPartialConsole,hPartialFile
qualname=Logger2CF
propagate=0
###############Handler###############
[handlers]
keys=hWholeConsole,hPartialConsole,hWholeFile,hPartialFile
[handler_hWholeConsole]
class=StreamHandler
level=DEBUG
formatter=simpFormatter
args=(sys.stdout,)
[handler_hPartialConsole]
class=StreamHandler
level=INFO
formatter=simpFormatter
args=(sys.stderr,)
[handler_hWholeFile]
class=FileHandler
level=DEBUG
formatter=timeFormatter
args=('out.txt','a')
[handler_hPartialFile]
class=FileHandler
level=WARNING
formatter=timeFormatter
args=('out.txt','w')
###############Formatter###############
[formatters]
keys=simpFormatter,timeFormatter
[formatter_simpFormatter]
format=[%(levelname)s]at%(filename)s,%(lineno)d:%(message)s
[formatter_timeFormatter]
format=%(asctime)s[%(levelname)s]at%(filename)s,%(lineno)d:%(message)s
datefmt=%Y-%m-%d(%a)%H:%M:%S
此处共创建三个Logger:root,将所有日志输出至控制台;Logger2F,将所有日志写入文件;Logger2CF,将级别大于或等于INFO的日志输出至控制台,将级别大于或等于WARNING的日志写入文件。
程序以如下方式解析配置文件和重定向输出:
importlogging,logging.config
logging.config.fileConfig("logger.conf")
logger=logging.getLogger("Logger2CF")
logger.debug('gubed');logger.info('ofni');logger.warn('nraw')
logger.error('rorre');logger.critical('lacitirc')
logger1=logging.getLogger("Logger2F")
logger1.debug('GUBED');logger1.critical('LACITIRC')
logger2=logging.getLogger()
logger2.debug('gUbEd');logger2.critical('lAcItIrC')
运行后屏幕上显示:
[INFO]attest.py,7:ofni
[WARNING]attest.py,7:nraw
[ERROR]attest.py,8:rorre
[CRITICAL]attest.py,8:lacitirc
[DEBUG]attest.py,14:gUbEd
[CRITICAL]attest.py,14:lAcItIrC
out.txt文件内容则为:
2022-04-22(Fri)20:31:21[WARNING]attest.py,7:nraw
2022-04-22(Fri)20:31:21[ERROR]attest.py,8:rorre
2022-04-22(Fri)20:31:21[CRITICAL]attest.py,8:lacitirc
2022-04-22(Fri)20:31:21[DEBUG]attest.py,11:GUBED
2022-04-22(Fri)20:31:21[CRITICAL]attest.py,11:LACITIRC
以上内容为大家介绍了Python之logging模块重定向,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注IT培训机构:千锋教育。

相关推荐HOT
更多>>
python cmd中怎么运行python文件
运行Python,可在交互模式下运行,或者命令行中。命令行中运行的是Python的.py文件。cmd中执行Python程序的方法,如下打开cmdC:\Users\Administ...详情>>
2023-11-06 14:37:04
pythondict是啥意思
Python中的dict表示的字典数据类型。字典是另一种可变容器模型,且可存储任意类型对象。字典是python语言中唯一的映射类型映射类型对象里哈希值...详情>>
2023-11-06 13:56:05
python如何创建模块
Python模块(Module),是一个Python文件,以.py结尾,包含了Python对象定义和Python语句。模块让你能够有逻辑地组织你的Python代码段。把相关的...详情>>
2023-11-06 13:36:31
python解释器写代码如何换行
使用以下方法可以使Python的代码换行:用反斜杠链接多行代码,示例输出:55a=22+\33printa用小括号括起来,示例输出:55a=(22+33)printa提示:针...详情>>
2023-11-06 13:17:12