每天一个python库:logging

每天一个python库:logging

logging库是Python标准库的一部分,提供了一个通用的日志系统。

例如

1
2
3
4
5
6
7
import logging
logging.debug('debug message')
logging.info('info message')
logging.warn('warn message')
logging.error('error message')
logging.critical('critical message')

运行即可在终端看到日志内容,默认是展示warn级别以上的,其中root是默认的logging实例名字

1
2
3
WARNING:root:warn message
ERROR:root:error message
CRITICAL:root:critical message

关于logging的配置可以分为内置函数设置和配置文件设置,这里先介绍下logging模块的几个主要内容
logger:提供日志接口,供应用代码使用。logger最常用的操作有两类:配置和发送日志消息。可以通过logging.getLogger(name)获取logger对象,如果不指定name则返回root对象,多次使用相同的name调用getLogger方法返回同一个logger对象。
handler:将日志记录(log record)发送到合适的目的地(destination),比如文件,socket等。一个logger对象可以通过addHandler方法添加0到多个handler,每个handler又可以定义不同日志级别,以实现日志分级过滤显示。
filter:提供一种优雅的方式决定一个日志记录是否发送到handler。
formatter:指定日志记录输出的具体格式。formatter的构造方法需要两个参数:消息的格式字符串和日期字符串,这两个参数都是可选的。

内置函数设置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import logging
logger = logging.getLogger('my_log_name')
#设置日志最小级别
logger.setLevel(logging.DEBUG)
#创建handler实例,这里的filehandler即把日志保存在当前目录的log.log文件中
fileHandler = logging.FileHandler('log.log')
#设置输入文件的日志级别,先过滤ogger.setLevel(logging.DEBUG)的日志级别,再过滤这里的级别
fileHandler.setLevel(logging.DEBUG)
#设置日志信息格式
fmt = '%(asctime)-12s %(levelname)s %(filename)s %(lineno)d %(message)s'
#实例化格式
formatter = logging.Formatter(fmt)
#在fileHandler上增加日志格式
fileHandler.setFormatter(formatter)
#logger实例增加fileHandler
logger.addHandler(fileHandler)
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')

运行后便会在当前目录生成一个log.log文件,里面有日志信息

也可以用basicConfig函数配置

1
2
3
4
5
6
7
8
9
10
11
12
import logging
fmt = '%(asctime)-12s %(levelname)s %(filename)s %(lineno)d %(message)s'
logging.basicConfig(filename='log.log', format=fmt, level=logging.DEBUG)
logger = logging.getLogger('my_log_name')
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')

常用的format

格式 描述
%(levelno)s 打印日志级别的数值
%(levelname)s 打印日志级别名称
%(pathname)s 打印当前执行程序的路径
%(filename)s 打印当前执行程序名称
%(funcName)s 打印日志的当前函数
%(lineno)d 打印日志的当前行号
%(asctime)s 打印日志的时间
%(thread)d 打印线程id
%(threadName)s 打印线程名称
%(process)d 打印进程ID
%(message)s 打印日志信息

文件配置设置

ps(这里是抄袭网上的,通过文件配置可以统一设置日志格式,不需要过多的重复代码:)

新建logging.conf文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[loggers]
keys=root,simpleExample
[handlers]
keys=consoleHandler
[formatters]
keys=simpleFormatter
[logger_root]
level=DEBUG
handlers=consoleHandler
[logger_simpleExample]
level=DEBUG
handlers=consoleHandler
qualname=simpleExample
propagate=0
[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,)
[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s

使用配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import logging
import logging.config
logging.config.fileConfig("logging.conf") # 采用配置文件
# create logger
logger = logging.getLogger("simpleExample")
# "application" code
logger.debug("debug message")
logger.info("info message")
logger.warn("warn message")
logger.error("error message")
logger.critical("critical message")

原文:大专栏  每天一个python库:logging