Python日志模块logging简介
admin
2023-07-31 02:15:18
0

logging分为4个模块: loggers, handlers, filters, and formatters.

●loggers: 提供应用程序调用的接口
●handlers: 把日志发送到指定的位置
●filters: 过滤日志信息
●formatters: 格式化输出日志

Logger

Logger.setLevel() 设置日志级别
Logger.addHandler()和Logger.removeHandler() 增加和删除日志处理器
Logger.addFilter()和Logger.removeFilter() 增加和删除过滤器
Logger.debug(), Logger.info(), Logger.warning(), Logger.error(), and Logger.critical() 创建不同的级别的日志
getLogger() 获取日志的根实例

Handler

setLevel() 设置日志级别
setFormatter() 设置输出格式
addFilter() and removeFilter() 增加和删除过滤器

Formatter

默认形式为: %Y-%m-%d %H:%M:%S.
格式为: %()s

日志配置管理

硬编码形式

复制代码 代码如下:
import logging

# create logger
logger = logging.getLogger(\’simple_example\’)
logger.setLevel(logging.DEBUG)

# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)

# create formatter
formatter = logging.Formatter(\’%(asctime)s – %(name)s – %(levelname)s – %(message)s\’)

# add formatter to ch
ch.setFormatter(formatter)

# add ch to logger
logger.addHandler(ch)

# \’application\’ code
logger.debug(\’debug message\’)
logger.info(\’info message\’)
logger.warn(\’warn message\’)
logger.error(\’error message\’)
logger.critical(\’critical message\’)

输出
复制代码 代码如下:
$ python simple_logging_module.py
2005-03-19 15:10:26,618 – simple_example – DEBUG – debug message
2005-03-19 15:10:26,620 – simple_example – INFO – info message
2005-03-19 15:10:26,695 – simple_example – WARNING – warn message
2005-03-19 15:10:26,697 – simple_example – ERROR – error message
2005-03-19 15:10:26,773 – simple_example – CRITICAL – critical message

通过文件配置管理日志

代码:

复制代码 代码如下:
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\’)

配置文件:
复制代码 代码如下:
[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
datefmt=

输出:
复制代码 代码如下:
$ python simple_logging_config.py
2005-03-19 15:38:55,977 – simpleExample – DEBUG – debug message
2005-03-19 15:38:55,979 – simpleExample – INFO – info message
2005-03-19 15:38:56,054 – simpleExample – WARNING – warn message
2005-03-19 15:38:56,055 – simpleExample – ERROR – error message
2005-03-19 15:38:56,130 – simpleExample – CRITICAL – critical message

日志格式

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

流程图

相关内容

热门资讯

500 行 Python 代码... 语法分析器描述了一个句子的语法结构,用来帮助其他的应用进行推理。自然语言引入了很多意外的歧义,以我们...
定时清理删除C:\Progra... C:\Program Files (x86)下面很多scoped_dir开头的文件夹 写个批处理 定...
65536是2的几次方 计算2... 65536是2的16次方:65536=2⁶ 65536是256的2次方:65536=256 6553...
Mobi、epub格式电子书如... 在wps里全局设置里有一个文件关联,打开,勾选电子书文件选项就可以了。
scoped_dir32_70... 一台虚拟机C盘总是莫名奇妙的空间用完,导致很多软件没法再运行。经过仔细检查发现是C:\Program...
pycparser 是一个用... `pycparser` 是一个用 Python 编写的 C 语言解析器。它可以用来解析 C 代码并构...
小程序支付时提示:appid和... [Q]小程序支付时提示:appid和mch_id不匹配 [A]小程序和微信支付没有进行关联,访问“小...
微信小程序使用slider实现... 众所周知哈,微信小程序里面的音频播放是没有进度条的,但最近有个项目呢,客户要求音频要有进度条控制,所...
python绘图库Matplo... 本文简单介绍了Python绘图库Matplotlib的安装,简介如下: matplotlib是pyt...
Prometheus+Graf... 一,Prometheus概述 1,什么是Prometheus?Prometheus是最初在Sound...