question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Format args and kwargs and the extra parameter

See original GitHub issue

Hi, first of all thanks a lot for this nice piece of logging 😃

I have a question: is there a way to use the keys in extra to format the message ? Or to add the kwargs of the debug/info/etc. to the extra parameter ?

Let me explain more with code, today one has to do this:

logger.bind(duration=10).info("my long process duration was: {duration} seconds", duration=10)

to have both a nice message and a “real” value to work with for monitoring purpose.

What I’m trying to achieve is having a single logging library for all our internal applications that allow to: log normally, send exceptions to sentry, send “TRACE” levels to influxdb -> grafana and all “TRACK” levels to loki -> grafana. For instance:

logger.exception("Houston we have a problem") # this goes to sentry
logger.debug("we do something") # this stays on the server
logger.track("user {username} started a long process", username="Me") # this is sent to Grafana through loki so username should be sendible in json log (serialize=True)
# later
logger.trace("long process duration was {duration} seconds", duration=10) # this is sent to Grafana through influxdb, duration should be sendible in json log (serialize=True)

Is it clear enough ? Am I missing something ? It’s late here and maybe I missed something obvious. If this is the case, sorry !

Anyway thanks a lot for your awesome lib ! Cheers

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Delgancommented, May 9, 2020

Indeed, it would be trivially possible to populate extra["kwargs"] with all arguments used to format the log message. However, I don’t want this to be the default behavior. I prefer it to be explicitly configured by the user, if he chooses to do so.

The logger has been designed so that logger.info(msg, *args, **kwargs) is a shorthand for msg.format(*args, **kwargs) that just creates the logged message. Besides that, there is bind() which can be used to dynamically update a dict (record["extra"]) and provide additional context. They’re two different things I’d rather not mix.

Also, if a solution had to be implemented to make the logger more convenient for your use case, I would love it to solve #2 at the same time. These are very related issues. This means we not only need a way to somehow add kwargs to the record, but we also need a way to allow a custom function to modify the message using kwargs.

I haven’t come up with a solution that I liked yet, but you’ve gave me another use case to think about. I’ve got some ideas.

The best I can offer you for now is to monkeypatch the logging methods (but in addition to being hacky, it breaks opt()):

import sys
from loguru import logger

def patch(method):
    def patched(self, message, *args, **kwargs):
        self = self.opt(depth=1).bind(**kwargs)
        method(self, message, *args, **kwargs)
    return patched

for log_method in ("trace", "debug", "info"):
    method = getattr(logger.__class__, log_method)
    setattr(logger.__class__, log_method, patch(method))

logger.remove()
logger.add(sys.stderr, format="{extra} | {message}")

logger.info("Foo")
logger.info("Bar {foo}", foo=123)
0reactions
jbjuincommented, May 17, 2020

Whaaat !! You are a rockstar ! Thank you so much !

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python args and kwargs: Demystified
In this step-by-step tutorial, you'll learn how to use args and kwargs in Python to ... *args and **kwargs allow you to pass...
Read more >
10 Examples to Master *args and **kwargs in Python
The **kwargs collect all the keyword arguments that are not explicitly defined. Thus, it does the same operation as *args but for keyword ......
Read more >
*args and **kwargs in Python - GeeksforGeeks
Python program to illustrate **kwargs for a variable number of keyword arguments with one extra argument. All the same, but one change is...
Read more >
Python *args and **kwargs (With Examples) - Programiz
In Python, we can pass a variable number of arguments to a function using special symbols. There are two special symbols: ... We...
Read more >
How To Use *args and **kwargs in Python 3 - DigitalOcean
In Python, the single-asterisk form of *args can be used as a parameter to send a non-keyworded variable-length argument list to functions. It ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found