全国旗舰校区

不同学习城市 同样授课品质

北京

深圳

上海

广州

郑州

大连

武汉

成都

西安

杭州

青岛

重庆

长沙

哈尔滨

南京

太原

沈阳

合肥

贵阳

济南

下一个校区
就在你家门口
+
当前位置:首页  >  技术干货

Python之logging模块重定向

发布时间:2023-11-07 19:33:59
发布人:xqq

对于代码量较大的工程,建议使用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培训机构:千锋教育。

python培训

相关文章

Python 解释器的作用

Python 解释器的作用

2023-11-07
Python 基本语句

Python 基本语句

2023-11-07
Python math 模块

Python math 模块

2023-11-07
Python decimal 模块

Python decimal 模块

2023-11-07

最新文章

武汉新媒体行业公司排名

武汉新媒体行业公司排名

2023-11-01
武汉新媒体就业现状好吗

武汉新媒体就业现状好吗

2023-11-01
武汉全媒体行业发展现状及趋势

武汉全媒体行业发展现状及趋势

2023-10-31
武汉全媒体现状

武汉全媒体现状

2023-10-31
在线咨询 免费试学 教程领取