简体   繁体   中英

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:

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.

My problem is that I have not added anything, so there is nothing to remove. I also cannot modify 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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