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:
JavaScript
x
9
1
from loguru import logger as log
2
3
log.debug("a debug log")
4
log.error("an error log")
5
6
# output
7
# 2022-09-15 16:51:23.325 | DEBUG | __main__:<module>:3 - a debug log
8
# 2022-09-15 16:51:23.327 | ERROR | __main__:<module>:4 - an error log
9
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:
JavaScript
1
6
1
import sys
2
from loguru import logger as log
3
4
log.remove()
5
log.add(sys.stderr, level="INFO") # or sys.stdout or other file object
6