python loguru 日志库的 format 格式

创建日期: 2025-05-22 10:58 | 作者: 风波 | 浏览次数: 26 | 分类: Python

来源:https://loguru.readthedocs.io/en/stable/api/logger.html#loguru._logger.Logger.add

from loguru import logger

logger.add("a.log", format="{message}")

其中 add 方法的定义如下

add(sink, *, level='DEBUG', format='<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>', filter=None, colorize=None, serialize=False, backtrace=True, diagnose=True, enqueue=False, context=None, catch=True, **kwargs)

help 文档中的信息

{'elapsed': datetime.timedelta(microseconds=11208), 'exception': None, 'extra': {}, 'file': (name='xlogger.py', path='/app/debug/xyd/xlogger.py'), 'function': '', 'level': (name='DEBUG', no=10, icon='🐞'), 'line': 8, 'message': 'hello', 'module': 'xlogger', 'name': 'main', 'process': (id=7107, name='MainProcess'), 'thread': (id=281472900929312, name='MainThread'), 'time': datetime(2025, 5, 22, 3, 12, 22, 809009, tzinfo=datetime.timezone(datetime.timedelta(0), 'UTC'))}

format 中的字段选项

level

Level name Severity value Logger method
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()

其他字段

Key Description Attributes
elapsed The time elapsed since the start of the program See datetime.timedelta
exception The formatted exception if any, None otherwise type, value, traceback
extra The dict of attributes bound by the user (see bind()) None
file The file where the logging call was made name (default), path / file.name or file.path
function The function from which the logging call was made None
level The severity used to log the message name (default), no, icon
line The line number in the source code None
message The logged message (not yet formatted) None
module The module where the logging call was made None
name The name where the logging call was made None
process The process in which the logging call was made name, id (default)
thread The thread in which the logging call was made name, id (default)
time The aware local time when the logging call was made See datetime.datetime
26 浏览
12 爬虫
0 评论