Python日志详解【两篇就够了系列】--第二篇loguru
目录第二章 Python日志loguru库详解一、loguru简介二、日志级别三、loguru日志常用参数配置解析1.rotation2.retention3.compression4.format四、常用方式测试1.按照文件大小切割2.按照间隔时间切割3.按照每天时间切割4.清理过期日志5.不可配置两种切割方式6.如果一定要根据大小和时间轮换日志文件五、其他说明1.对比loguru和loggin
目录
第二章 Python日志loguru库详解
一、loguru简介
Loguru是一个旨在以Python带来令人愉悦的日志记录的库。Loguru的主要概念是只有一个 logger。没有 Handler, 没有 Formatter, 没有Filter只有一个add函数。
使用方法如下
logger.add("file_1.log", rotation="500 MB") #自动分割大文件
logger.add("file_2.log", rotation="12:00") #每天12:00自动压缩
logger.add("file_X.log", retention="10 days") # 十天前的日志删除
logger.add("file_Y.log", compression="zip") # zip方式压缩
参数说明
- sink (file-like object, str, pathlib.Path, callable, coroutine function or logging.Handler) – An object in charge of receiving formatted logging messages and propagating them to an appropriate endpoint.【日志发送目的地】
- level (int or str, optional) – The minimum severity level from which logged messages should be sent to the sink.【日志等级】
- format (str or callable, optional) – The template used to format logged messages before being sent to the sink.【日志格式】
- filter (callable, str or dict, optional) – A directive optionally used to decide for each logged message whether it should be sent to the sink or not.【日追成过滤器】
- colorize (bool, optional) – Whether the color markups contained in the formatted message should be converted to ansi codes for terminal coloration, or stripped otherwise. If None, the choice is automatically made based on the sink being a tty or not.【是否加颜色】
- serialize (bool, optional) – Whether the logged message and its records should be first converted to a JSON string before being sent to the sink.【是否序列号】
- backtrace (bool, optional) – Whether the exception trace formatted should be extended upward, beyond the catching point, to show the full stacktrace which generated the error.
- diagnose (bool, optional) – Whether the exception trace should display the variables values to eases the debugging. This should be set to False in production to avoid leaking sensitive data.
- enqueue (bool, optional) – Whether the messages to be logged should first pass through a multiprocess-safe queue before reaching the sink. This is useful while logging to a file through multiple processes. This also has the advantage of making logging calls non-blocking.
- catch (bool, optional) – Whether errors occurring while sink handles logs messages should be automatically caught. If True, an exception message is displayed on sys.stderr but the exception is not propagated to the caller, preventing your app to crash.
- **kwargs – Additional parameters that are only valid to configure a coroutine or file sink
- rotation (str, int, datetime.time, datetime.timedelta or callable, optional) – A condition indicating whenever the current logged file should be closed and a new one started.【配置日志切割】
- retention (str, int, datetime.timedelta or callable, optional) – A directive filtering old files that should be removed during rotation or end of program.【删除过期日志】
- compression (str or callable, optional) – A compression or archive format to which log files should be converted at closure.【压缩方式】
- delay (bool, optional) – Whether the file should be created as soon as the sink is configured, or delayed until first logged message. It defaults to False.
- mode (str, optional) – The opening mode as for built-in open() function. It defaults to "a" (open the file in appending mode).
- buffering (int, optional) – The buffering policy as for built-in open() function. It defaults to 1 (line buffered file).
- encoding (str, optional) – The file encoding as for built-in open() function. If None, it defaults to locale.getpreferredencoding().
二、日志级别
级别 |
值 |
方法 |
TRACE |
5 |
logger.trace() |
DEBUG |
10 |
logger.debug() |
INFO |
20 |
logger.info() |
SUCCESS |
25 |
logger.success() |
WARNING |
30 |
logger.warning() |
ERROR |
40 |
logger.error() |
CRITICAL |
50 |
logger.critical() |
三、loguru日志常用参数配置解析
1.rotation
rotation检查记录每个消息之前完成。如果已经存在与要创建的文件同名的文件,则通过将日期附加到其基名中来重命名现有文件,以防止文件被覆盖
例如:
"100
MB""0.5
GB""1
month
2
weeks""4
days""10h""monthly""18:00""sunday""w0""monday
at
12:00"
2.retention
保留文件最大保留期限,例如
"1
week,
3
days""2
months"
3.compression
日志压缩方式,例如
"gz","bz2","xz","lzma","tar","tar.gz","tar.bz2", "tar.xz","zip"
4.format
Key |
官方描述 |
备注 |
elapsed |
The time elapsed since the start of the program |
日期 |
exception |
The formatted exception if any, none otherwise |
|
extra |
The dict of attributes bound by the user (see bind()) |
|
file |
The file where the logging call was made |
出错文件 |
function |
The function from which the logging call was made |
出错方法 |
level |
The severity used to log the message |
日志级别 |
line |
The line number in the source code |
行数 |
message |
The logged message (not yet formatted) |
信息 |
module |
The module where the logging call was made |
模块 |
name |
The __name__ where the logging call was made |
__name__ |
process |
The process in which the logging call was made |
进程id或者进程名,默认是id |
thread |
The thread in which the logging call was made |
线程id或者进程名,默认是id |
time |
The aware local time when the logging call was made |
日期 |
四、常用方式测试
1.按照文件大小切割
按照文件大小切割日志,并压缩成tar.gz
logger.add('log6/yuhceng.log', rotation="500KB",compression="tar.gz")
2.按照间隔时间切割
时间间隔设置为4s,每4秒切割一次,
logger.add("log7/yuhceng.log", rotation="4s")
3.按照每天时间切割
在每天01:15切割
logger.add("log8/yuhceng.log", rotation="01:15")
4.清理过期日志
按每5s切割一次,只保留过去1分分钟的日志
logger.add("log8/yuhceng.log", rotation="5s",retention="1 min")
5.不可配置两种切割方式
配置按照时间切割和按照文件大小切割,但是只能配置一种方式。如果配置两种方式,那就会打印重复日志。
import time
from loguru import logger
logger.add('log/yuhceng.log', rotation="5MB")
logger.add("log/yuhceng.log", rotation="2h")
for i in range(1000000000000000000000000):
logger.info("yucheng{}".format(str(i)))
time.sleep(0.1)
如图所示,第一个文件会比其他文件都大,说明日志并没有按照预期切割。
6.如果一定要根据大小和时间轮换日志文件
虽然上文方案已经可以满足需求,但是有的系统偏偏要搞同时按照大小和时间轮换。怎么解决?
文件接收器的参数接受大小或时间限制,但出于简化原因,不能同时接受。但是,可以创建自定义函数来支持更高级的方案
import datetime
class Rotator:
def __init__(self, *, size, at):
now = datetime.datetime.now()
self._size_limit = size
self._time_limit = now.replace(hour=at.hour, minute=at.minute, second=at.second)
if now >= self._time_limit:
# The current time is already past the target time so it would rotate already.
# Add one day to prevent an immediate rotation.
self._time_limit += datetime.timedelta(days=1)
def should_rotate(self, message, file):
file.seek(0, 2)
if file.tell() + len(message) > self._size_limit:
return True
if message.record["time"].timestamp() > self._time_limit.timestamp():
self._time_limit += datetime.timedelta(days=1)
return True
return False
# Rotate file if over 500 MB or at midnight every day
rotator = Rotator(size=5e+8, at=datetime.time(4, 0, 0))
logger.add("file.log", rotation=rotator.should_rotate)
可以看到,在4点的时候那次切割,日志最小,是因为出触发了按照固定时间的切割。
五、其他说明
1.对比loguru和logging
(1)从安装来看,logging不需要安装,loguru需要安装。
(2)从配置的角度,loguru全局只有一个logger,logging可以通过名字的不同创建多个
(3)配置loguru相比配置logging更加简便
(4)其他模块的支持,这里面其他模块指定是import 的其他模块,比如,系统中import peewee,在peewee中使用了logging模块,如果想要打印peewee中的日志,logging可以通过配置打印出来,而loguru是不支持的。
对比项目 |
logging |
loguru |
logger |
可以多个 |
全局一个 |
是否需要安装 |
不需要 |
需要 |
自定义handler |
支持 |
支持 |
自定义难度 |
困难,要自己控制 |
相对容易,需要指定切割条件 |
其他模块日志打印 |
支持 |
不支持 |
2.参考文献
https://loguru.readthedocs.io/en/stable/overview.html
更多推荐
所有评论(0)