Someone2022年9月20日大约 2 分钟



Python 自带了 logging 模块,可以很方便我们进行日志打印。本文章主要总结几个使用该模块的时候需要注意的问题和实战示例。


>>> import logging
>>> logging.warning('Watch out!')
WARNING:root:Watch out!

Log level

Python 的日志可以分为几个级别:

TaskLevel for task
console 级别print()
events that occur during normal operation of a programlogging.info() or logging.debug()
Issue a warning regarding a particular runtime eventlogging.warn()
Report an errorRaise Exception
Report suppression of an error without raising an exceptionlogging.error() or logging.exception or logging.critical()

The logging functions are named after the level or severity of the events they are used to track. The standard levels and their applicability are described below (in increasing order of severity):

LevelWhen it’s used
DEBUGDetailed information, typically of interest only when diagnosing problems.
INFOConfirmation that things are working as expected.
WARNINGAn indication that something unexpected happened, or indicative of some problem in the near future (e.g. ‘disk space low’). The software is still working as expected.
ERRORDue to a more serious problem, the software has not been able to perform some function.
CRITICALA serious error, indicating that the program itself may be unable to continue running.

The default level is WARNING, which means that only events of this level and above will be tracked, unless the logging package is configured to do otherwise.

Logging to a file

to file


import logging
logging.basicConfig(filename='example.log', encoding='utf-8', level=logging.DEBUG)


to console


logging.basicConfig(stream=sys.stdout, level=logging.INFO)


可以使用 --log=INFO 来制定日志的打印级别。

Logging Format

在使用 logging 模块的时候,遇到了要打印多个参数但是报错的问题,针对这个问题,我们使用标准的 format 语法,可以避免错误,如下:

logging.info('pattern {} similar res is {}, write to file...'.format(pattern_1, res))

相比于 %d 的方式,上述的方式较新。

Logging in different plarform


if platform.system() == 'Linux':
    logging.basicConfig(filename='wechat_sendmessage_st_021.log', level=logging.INFO)
    logging.info('you are in linux, confining the linux sys config...')
    s.file_split = '/srv/workspace/workcode/tmpfile/wechat_sendmessage_st_021.txt'
    worker_count = 8
    logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
    logging.info('you are in windows, confining the windows sys config...')
    s.file_split = '../../oat/trace_converted/wechat_sendmessage_st_021.txt'
    worker_count = 4

上面代码,我们在 windows 环境下面使用控制台输出,而在 Linux 下面使用文件输出。