在进行Python编程时,为了更好地了解程序的运行情况,调试及错误排查,我们需要使用日志来记录程序运行过程中的信息。Python提供了标准库logging,可以很方便地输出日志信息,本文将对Python日志输出进行详细阐述,从入门到实践,教你如何让你的代码更好地显示日志信息。
一、Python日志输出基础
Python的logging模块提供了日志记录的功能。它可以输出到终端、文件、邮件等,可读性较高,同时可以更灵活地进行管理和控制。下面是一个简单的例子:
“`
import logging
logging.basicConfig(level=logging.DEBUG)
logging.debug(‘This is a debug message’)
logging.info(‘This is an info message’)
logging.warning(‘This is a warning message’)
logging.error(‘This is an error message’)
logging.critical(‘This is a critical message’)
“`
运行上述代码,会依次输出不同级别的日志信息:
“`
DEBUG:root:This is a debug message
INFO:root:This is an info message
WARNING:root:This is a warning message
ERROR:root:This is an error message
CRITICAL:root:This is a critical message
“`
上述代码中,我们使用logging.basicConfig()配置了日志的基本信息。level=logging.DEBUG表示将日志等级设置为DEBUG,即输出该等级及以上等级的日志信息。除了DEBUG等级,还有INFO、WARNING、ERROR、CRITICAL等级。接着,我们使用logging.debug()、logging.info()等方法输出不同等级的日志信息。
另外,我们还可以通过设置format参数来控制输出的格式,比如:
“`
import logging
logging.basicConfig(level=logging.DEBUG, format=’%(asctime)s – %(name)s – %(levelname)s – %(message)s’)
logger = logging.getLogger(__name__)
logger.debug(‘This is a debug message’)
logger.info(‘This is an info message’)
logger.warning(‘This is a warning message’)
logger.error(‘This is an error message’)
logger.critical(‘This is a critical message’)
“`
运行上述代码,会按照设置的格式输出日志信息:
“`
2021-08-10 11:11:26,537 – __main__ – DEBUG – This is a debug message
2021-08-10 11:11:26,538 – __main__ – INFO – This is an info message
2021-08-10 11:11:26,538 – __main__ – WARNING – This is a warning message
2021-08-10 11:11:26,538 – __main__ – ERROR – This is an error message
2021-08-10 11:11:26,538 – __main__ – CRITICAL – This is a critical message
“`
在上述代码中,我们设置format参数为’%(asctime)s – %(name)s – %(levelname)s – %(message)s’,其中’%(asctime)s’表示输出时间,’%(name)s’表示输出日志所在模块的名称,’%(levelname)s’表示日志等级,’%(message)s’表示输出日志信息。
二、Python日志输出高级配置
1. 使用配置文件进行配置
基本配置虽然简单易懂,但灵活性不够,无法满足复杂的需求。此时我们可以使用配置文件进行配置。下面是一个配置文件的例子:
“`
[loggers]
keys=root,sampleLogger
[handlers]
keys=consoleHandler,fileHandler
[formatters]
keys=sampleFormatter
[logger_root]
level=DEBUG
handlers=consoleHandler,fileHandler
[logger_sampleLogger]
level=DEBUG
handlers=consoleHandler
qualname=sampleLogger
propagate=0
[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=sampleFormatter
args=(sys.stdout,)
[handler_fileHandler]
class=FileHandler
level=DEBUG
formatter=sampleFormatter
args=(‘example.log’, ‘w’)
[formatter_sampleFormatter]
format=%(asctime)s – %(name)s – %(levelname)s – %(message)s
datefmt=%Y-%m-%d %H:%M:%S
“`
上述配置文件中,我们定义了三个部分:loggers、handlers、formatters。loggers定义了所有日志器的名称;handlers定义了所有处理器的名称和类型;formatters定义了所有格式化器的名称、格式化字符串和输出日期格式。接着,我们定义了日志器们和它们的属性:level表示日志的等级;handlers表示对应的处理器;qualname表示日志器的名称;propagate表示是否向上传递该日志消息。最后,我们定义了两个处理器和一个格式化器。
使用配置文件的方式可以使日志的配置更加灵活,也更方便进行维护。使用方法如下:
“`
import logging.config
logging.config.fileConfig(‘logging.conf’)
logger = logging.getLogger(‘sampleLogger’)
logger.debug(‘This is a debug message’)
logger.info(‘This is an info message’)
logger.warning(‘This is a warning message’)
logger.error(‘This is an error message’)
logger.critical(‘This is a critical message’)
“`
运行上述代码,会按照配置文件中定义的属性输出日志信息。而对于项目较大的情况,在一个文件中定义格式化内容,并对多个模块使用不同等级会更加便利。
2. 使用RotatingFileHandler进行日志回滚
另一个常用的配置是日志回滚。日志文件占用空间过大时,我们可以通过日志回滚将旧的日志文件进行删除或压缩。Python中提供了RotatingFileHandler来优雅地进行日志回滚。下面是一个例子:
“`
import logging
from logging.handlers import RotatingFileHandler
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter(‘%(asctime)s – %(name)s – %(levelname)s – %(message)s’)
# 每个日志文件最大的大小
maxBytes = 1024
# 最多保留的日志文件数量
backupCount = 3
# 添加RotatingFileHandler
file_handler = RotatingFileHandler(‘example.log’, mode=’a’, maxBytes=maxBytes, backupCount=backupCount, encoding=None, delay=0)
file_handler.setFormatter(formatter)
file_handler.setLevel(logging.DEBUG)
logger.addHandler(file_handler)
# 输出日志信息
logger.debug(‘This is a debug message’)
logger.info(‘This is an info message’)
logger.warning(‘This is a warning message’)
logger.error(‘This is an error message’)
logger.critical(‘This is a critical message’)
“`
在上述代码中,我们首先创建一个日志器和formatter,并定义了日志的等级和输出格式。接着我们使用RotatingFileHandler创建文件处理器,并设置最大的日志文件大小为1024字节,并最多保留3个日志文件。最后,我们将处理器加入到日志器中,并使用日志器输出日志信息。
三、Python日志常用技巧
1. 输出调用位置
在日志输出中,有时需要输出日志消息所在的源代码行数、函数名和模块名等调用位置信息,这时可以使用logging模块提供的LogRecord属性。下面是一个例子:
“`
import logging
logging.basicConfig(level=logging.DEBUG, format=’%(asctime)s – %(name)s – %(levelname)s – %(message)s – %(pathname)s:%(lineno)d’)
logger = logging.getLogger(__name__)
logger.debug(‘This is a debug message’)
“`
在上述代码中,我们使用format参数设置了输出日志的格式,并添加了LogRecord属性pathname、lineno,分别表示源代码文件名和所在行号。运行上述代码,输出日志消息以及调用位置信息。
2. 分模块输出日志
在编程中,我们经常会使用多个模块协同工作,为了更好地了解各个模块的日志信息,我们可以为每个模块都创建一个日志器,使用不同的日志输出方式和等级。例如:
“`
# main.py
import logging
import module1
import module2
logging.basicConfig(level=logging.DEBUG, format=’%(asctime)s – %(name)s – %(levelname)s – %(message)s’)
logger1 = logging.getLogger(‘module1’)
logger2 = logging.getLogger(‘module2’)
logger1.setLevel(logging.DEBUG)
logger2.setLevel(logging.WARNING)
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(logging.Formatter(‘%(asctime)s – %(name)s – %(levelname)s – %(message)s’))
logger1.addHandler(stream_handler)
file_handler = logging.FileHandler(‘module2.log’)
file_handler.setFormatter(logging.Formatter(‘%(asctime)s – %(name)s – %(name)s – %(levelname)s – %(message)s’))
logger2.addHandler(file_handler)
logger1.debug(‘debug message in module1’)
module1.func1()
logger2.warning(‘warning message in module2’)
module2.func2()
“`
在上述代码中,我们在main.py中定义两个日志器logger1和logger2,并设置它们的不同等级和输出方式。我们将’example.log’文件设为logger2的输出文件,并添加输出格式。
然后我们在module1.py和module2.py文件中定义了func1()和func2()函数,并分别输出日志信息。我们通过这种方式,可以为各个模块设置不同的日志等级和输出方式,方便我们进行错误排查与修复。
结语
Python提供了强大的logging模块,方便我们输出日志信息。我们可以通过logging模块的基本使用、高级配置、常用技巧等多个方面,实现更为灵活、高效的日志记录。希望本文能对你在Python的编程工程中带来帮助。
原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/277332.html