-
Notifications
You must be signed in to change notification settings - Fork 736
How to set the log level? #138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I you would like to use another level in place of the default Alternatively, you can just re-add the logger.remove()
logger.add(sys.stderr, level="INFO") |
The thing to understand is that, contrary to standard |
Feel free to re-open this issue if you have others concerns. |
Even though I set the
when I run the script as I would think this is a dynaconf issue, but the |
@ab-10 Try importing Loguru setups logging values at import time using environment variables. If the environment variables have not been configured yet by |
That makes sense, however could you please advise why does importing loguru after the os environment variable has been set doesn't resolve the issue? |
@ab-10 What do you mean? Are you saying that inverting |
yes, exactly |
@ab-10 According to the |
Thanks @Delgan, this really looks like a dynaconf issue. I ended up using |
Back to the original issue: |
@YuvalAdler This is not something I'm planning. To keep things simple, handlers are not exposed publicly. Some possible workarounds are listed in the documentation. |
I can't set loguru level dynamically with |
@deknowny Modifying If you need to change the level of a handler, you will have to use one another workaround (see link to documentation in my previous post). |
I guess filter is the solution. Thanks! |
environ* |
Why not provide a Having to remove and add, is a little clunky imho for such a common operation. |
@erezsh A function that only acts on the last handler would be incomplete and would not cover all use cases. I don't consider that changing the level at runtime is a common enough use case to justify adding a new function to the API, especially since it would be a special case and there may be many other reasons to change the handler. You can define a dynamic filter to achieve the same result: import sys
from loguru import logger
min_level = "INFO"
def my_filter(record):
return record["level"].no >= logger.level(min_level).no
logger.remove()
logger.add(sys.stderr, filter=my_filter)
logger.debug("Not logged")
min_level = "DEBUG"
logger.debug("Logged") |
Sorry to ping on a closed issue, I was just hoping for some clarification on the accepted solutions. One thing I'm slightly concerned about with the suggestion of "just remove and add a handler"...
Is this meant in the context of importing and using my python library from within other python code? |
Hi @glass-ships. The statement you quoted only applies to cases where your library is intended to be used as a package or library that is imported by other Python code. For example: import some_lib
some_lib.do_something() If your Python code is standalone and is only intended to be run as a CLI tool (what I call "script"), then you can safely add and remove handlers to the |
awesome, thanks so much delgan!! |
It's important to provide some context as to what those commands do, so here's my code snippet (which I also updated here): import sys
from loguru import logger as log
log.remove() #remove the old handler. Else, the old one will work (and continue printing DEBUG logs) along with the new handler added below'
log.add(sys.stdout, level="INFO") #add a new handler which has INFO as the default
log.debug("debug message")
log.info("info message")
log.warning("warning message")
log.error("error message") |
My two cents: I really like loguru, especially for small/medium scripts, where i just want to log things instead of print() them. I like the simplicity of just importing the lib and using it without jumping through hoops. I appreciate that we can have multiple handlers and all, but I guess a large share of use cases are like mine: single handler logger where i just want things to look good out of the box. The handler abstraction itself is a distraction for this use case: I just want to import a lib and start logging stuff. My two cents: Why not add a set_level in the top level to deal with this simple use case? The function could check if theres only a single default handler, and then perform the remove/add dance. If there are multiple handlers then just error out, as the programmer should then decide what to do.
looks better than
|
@polvoazul I appreciate your input, but from my point of view, such I agree that the " handler = {"sink": sys.stdout, "level": "ERROR"}
logger.configure(handlers=[handler]) |
Fair enough. I understand that adding a special case is indeed a bold ask (although sometimes pragmatism is valuable enough to warrant it). Didn't know about logger.configure. This looks much better to my eye than remove/add:
almost a 1 liner. Maybe add this twoliner to the README? I guarantee it will get ctrl-c many times :) |
Could this please be in the documentation, like at the very top of the README? The very first thing I want to know when debugging somebody else's code is "how to I increase the logging level?" And I searched for about 10 minutes in the documentation and did not find the answer until I landed on this issue. |
For anyone else who uses richuru, you'll have to make sure you do the A minor inconvenience, but one that I was annoyed at:) And, I think there is a desire to have rich and loguru "properly married" so this might not be a very useful comment, at all. |
Setting the log level is common enough that if there is no such function probably consider not use loguru. It is a pity that it seems there is still not a solution that can automatically gets the last handler and set its level. And what's worse, the environment variable solution needs to restart the program all the time, not compatible to the os.environ['LOGURU_LEVEL'] method. |
Hello, I have been looking into a simple logging tool and this was recommended to me and it looks very promising. I have a few thoughts about the topic:
My Usecase: |
On top of my comment yesterday, I am very unsure about how this is supposed to be done in practical. It is such a shame this is a HUGE roadblock in this utility for my case. Can anyone help me with figuring out how I am supposed to do this?
Now we change each class's log level
I haven't tested this since I cannot wrap my head around how this is supposed to work. It does not seem very logical how this is supposed to be approached. Anyone can help me here? |
Hey @Stefanhg. Thanks for the concrete use cases. That's definitely helpful to shape the proper API of Loguru. The solution of simply using In such cases, I would suggest using a custom min_level = logger.level("DEBUG").no
def filter_by_level(record):
return record["level"].no >= min_level
logger.remove()
logger.add(sys.stderr, filter=filter_by_level, level=0)
logger.debug("Logged")
min_level = logger.level("WARNING").no
logger.debug("Notogged") Regarding your second post, I notice that you add the same sink twice ( If you want to adapt the level of different modules, you can use a dict as a filter or a custom function: filtering = {
"module_1": "DEBUG",
"module_2": "INFO",
}
logger.add(sys.stdout, filter=filtering) This question appears off-topic, though. Please open a new issue and I'll try to help you in more detail. |
Changing the level at runtime is an extremely common use case (as the contributions to this issues showed). I am with the original creator or this issue: people pick loguru exepcting it to be simple just to find out that something that has to be done everytime a CLI is run is a) surprising and be) much more difficult as it needs to be. |
It's strange for something that is supposed to be stupidly simple there is no setLevel() function available, as on the plain old logger I get back from getLogger().
The docs show a heavy handed approach of adding a whole new handler just to set the level like:
logger.add(sys.stderr, format="{time} {level} {message}", filter="my_module", level="INFO")
But really?
The text was updated successfully, but these errors were encountered: