Solvedloguru Change level of default handler
โ๏ธAccepted Answer
@Delgan Thank you! I've got
log.remove()
log.add(sys.stderr, level=config.LOG_LEVEL)
in my codebase now. I missed that the .remove()
method removes the previously added handler (meaning the default one). For some reason I thought I'd have to know the default handler's id.
Other Answers:
@jetheurer If no argument is provided to logger.remove()
, all existing handlers are removed.
You can also remove the default handler using logger.remove(0)
, because the default handler will always have the id 0
.
Just to be clear, I think @Delgan is recommending that folks do this via the env
. Without setting the env, you are defaulted to debug mode for development. Then in production you set LOGURU_LEVEL
in the environment. To change the level in development to see what prod logs look like, you can just do:
LOGURU_LEVEL=INFO python my_script.py
Hello @jetheurer, happy to know that you like Loguru !
So, indeed, by default a sys.stderr
handler is added with an "INFO"
level.
You can add as many handlers as you want, this means that if you do logger.add(sys.stderr, level="INFO")
while the default handler has not been removed, your log messages will be dispatched twice to sys.stderr
!
This is why you still see DEBUG
messages. Those are the one from the default handler which co-exists with your INFO
handler.
The solution is to first .remove()
the default handler, before adding a new one which logs to stderr
.
Alternatively, you can set the LOGURU_LEVEL
environment variable to "INFO"
, so the default handler will be started with your preferred level all the times.
Hi @RichardDally. :)
The rational behind setting the default level to DEBUG
is as follow: every new project first goes through a development phase during which debug messages are much helpful. As a loguru
user, you will spend most of your time developping your application. When your software is fully working and ready to release, you can take the time to properly add handlers and configure the level based on a command line argument for example.
So it makes sense, from my point of view, that you can start any Python application and see debug messages without any further configuration.
from loguru import logger
logger.debug("Let's go to work")
If you want to replace standard logging with Loguru, you also need to replace logger.addHandler()
and others setup steps, right? So, could you not simply use logger.add(sys.stderr, level="INFO")
?
First off, this library is terrific, I found it via the podcast Python Bytes and I've been using it ever since.
So here is my question: I understand, the default handler for
from loguru import logger
goes tosys.stderr
.When I try:
logger.add(sys.stderr, level="INFO")
, I still getDEBUG
level messages in the terminal.My goal is to change the
level
of the logging tosys.stderr
. I don't have any other handlers.