logging日志配置

Python logging日志配置



Python在引入logging日志模块时,需要对其进行配置


使用logging.basicConfig()进行配置,

在此类中调用其他包的方法都会使用该配置,其他类中无需再配置logging.basicConfig

logging默等级为WARNING (打印到屏幕的等级)

日志级别大小关系为:CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET

假如你想使用logging将信息打印到屏幕,只有使用等级大于等于WARNING的级别才能打印到屏幕

当然这个级别是可以设置的

举例:

import logging

logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')

logging.info('hello world’)

注意:在配置之前,不要使用logging.info()进行打印,否则就算调用了logging.info()也不会有任何显示


通过logging.basicConfig函数对日志的输出格式及方式做相关配置

import logging

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

 

./myapp.log文件中内容为:
Sun, 24 May 2009 21:48:54 demo2.py[line:11] DEBUG This is debug message
Sun, 24 May 2009 21:48:54 demo2.py[line:12] INFO This is info message
Sun, 24 May 2009 21:48:54 demo2.py[line:13] WARNING This is warning message


logging.basicConfig函数各参数:

filename: 指定日志文件名

filemode: file函数意义相同,指定日志文件的打开模式,'w''a'

format: 指定输出的格式和内容,format可以输出很多有用信息,如上例所示:

 %(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: 打印日志信息

datefmt: 指定时间格式,同time.strftime()

level: 设置日志级别,默认为logging.WARNING

stream: 指定将日志的输出流,可以指定输出到sys.stderr,sys.stdout或者文件,默认输出到sys.stderr,当streamfilename同时指定时,stream被忽略


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

import logging

logging
.basicConfig(level=logging.DEBUG,
                format
='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                datefmt
='%a, %d %b %Y %H:%M:%S',
                filename
='myapp.log',
                filemode
='w')

#
定义一个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)
#################################################################################################

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

 

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

./myapp.log文件中内容为:
Sun, 24 May 2009 21:48:54 demo2.py[line:11] DEBUG This is debug message
Sun, 24 May 2009 21:48:54 demo2.py[line:12] INFO This is info message
Sun, 24 May 2009 21:48:54 demo2.py[line:13] WARNING This is warning message


参考https://blog.csdn.net/u013378306/article/details/70146102

猜你喜欢

转载自blog.csdn.net/benben0729/article/details/81018052
今日推荐