-
Notifications
You must be signed in to change notification settings - Fork 712
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
Is there a way to disable message formatting ? #318
Comments
Hi! Well, you can disable formatting by not providing any argument. If you want some arguments to be added to the logger.bind(additional_infos=kwargs).info(f"Is this going to be alright ? {kwargs}") Another possiblity is simply to not use f-strings: logger.info("Is it going to be alright? {additional_infos}", additional_infos=kwargs) I agree that this may not be the most convenient, since it implies always having to think about that particular case. Especially as auto-formatting is not very useful anymore now that f-strings are widely used. I'm not a big fan of adding more and more configuration options because it makes the code and the api more complex. But maybe I need to reconsider my position. |
Thx for your answer ! I understand your aversion of adding more configuration. Let's wait and see if this feature is of interest to some more people ? |
I may have some ideas to allow more freedom in configuring the message formatting. I think this feature might be of interest to some people, in your case and others. But I'm not sure of anything, we'll see. I'll keep this issue open in the meantime. 👍 |
Hi, I just ran into a similar issue when using an intercept handler to collect logs from pika/pika. The library logs a dictionary at some point, which throws a KeyError exception when I add a keyword argument to the same logging function call. Your suggestion to use Thanks for your hard work on Loguru! |
Thanks @kace for the additional use case, it's very helpful to have this kind of example while thinking about a solution. I plan to propose a way to configure formatting, but I still haven't decided which one. Adding a parameter like Anyway, I am still thinking about this issue. ;) |
In my case, this happened to me when I combined
(The above results in an unexpected KeyError because warnings are automatically formatted by Python to include the raw line of code from which they were thrown). My workaround right now is to use contextualize() instead of adding the extra() manually:
|
@soundstripe This is very similar to my use case. You can see my workaround using If I remember correctly my original implementation passed the |
Using the standard logger and intercept handler, I get an error with
|
@mcarans Are you able to provide a minimum reproducible example please (eventually in a new ticket)? I tested it locally and it worked as expected. |
@Delgan It seems to occur when setting
|
@mcarans When logger.opt(record=True).info("Logging some message from thread {record[thread].id}") To do so, Loguru will call For this reason I would advice against using |
Hi @Delgan , I've just started using loguru with FastAPI and it's very good, but I bumped into the same issue above with our InterceptHandler, and in some cases we have dictionaries being pushed to the log message, and I could not bypass the |
+1 |
I want to stress the problem with this automatic format behavior. There would be no way for me to do this without risking a crash when any string containing curly brackets is passed. |
@mastern2k3 I'm not sure to understand. logger.info("User data: {}", input()) That would work fine and wouldn't not crash regardless of user data containing curly brackets or not. |
@Delgan you are right, but, |
@mastern2k3 You can use f-strings as long as you do not call the logging method with formatting arguments: logger.info(f"User data: {user_data}") # Good.
logger.info(f"User data: {user_data}", data=user_data) # Bad.
logger.bind(data=user_data).info(f"User data: {user_data}) # Good. I got your point, though. I plan to remove automatic formatting in favor of f-strings. |
@Delgan Thanks! I didn't notice this is triggered only when there's extra data, that helps a bit. We often use named parameters after the message (as an f-string) to add attributes to the extras so it ends up in our structured logs, this is what gets me. |
@Delgan hello, I faced the same issue. |
No, not the same issue) |
@Delgan Is removing automatic formatting in favor of f-strings still planned? |
@mcarans Yeah, this is something I would like to do, but I don't have an ETA or technical solution for the transition yet. |
Same issue here, which was exacerbated by the exception happening quietly in a background thread, so it wasn't obvious. Adding the config to remove automatic formatting would be wonderful! 🙏 |
+1 |
Hi Delgan !
I was wondering if there was a way to disable the message formatting with the args/kwargs ?
Traceback:
Is it a feature you would be inclined in adding ?
Maybe parametrized by an environment variable or via
logger.configure
?Cordially !
The text was updated successfully, but these errors were encountered: