简体   繁体   English

如何使用 loguru 设置最小日志记录级别?

[英]How to set a minimal logging level with loguru?

I would like to use a different logging level in development and production.我想在开发和生产中使用不同的日志记录级别。 To do so, I need early in my program to set the minimal level for logs to be triggered.为此,我需要在程序的早期设置触发日志的最低级别。 The default is to output all severities:默认为 output 所有严重性:

from loguru import logger as log

log.debug("a debug log")
log.error("an error log")

# output
# 2022-09-15 16:51:23.325 | DEBUG    | __main__:<module>:3 - a debug log
# 2022-09-15 16:51:23.327 | ERROR    | __main__:<module>:4 - an error log

There is a Changing the level of an existing handler section in the documentation, that states among others that文档中有一个更改现有处理程序的级别部分,其中指出

Once a handler has been added, it is actually not possible to update it.一旦添加了处理程序,实际上就不可能更新它。 (...) (...)

The most straightforward workaround is to remove() your handler and then re- add() it with the updated level parameter.最直接的解决方法是remove()你的处理程序,然后用更新的 level 参数重新add()它。

My problem is that I have not added anything, so there is nothing to remove.我的问题是我没有添加任何东西,所以没有什么可以删除的。 I also cannot modify log .我也无法修改log So what should I do?所以我该怎么做?

Like so:像这样:

import sys
from loguru import logger as log

log.remove()
log.add(sys.stderr, level="INFO") # or sys.stdout or other file object

More info .更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM