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?
Advertisement
Answer
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