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.

Set a local format for a bind logger

See original GitHub issue

Hi, in some methods of a class I need to always log a set of values together with the message. But this should happen only in those methods. I think the bind method is the answer, but I can’t figure out how to format the message locally.

def f(i):
  context_logger = logger.bind(i=i)
  context_logger.info("Starting")
  ...
  context_logger.info("Ending")

Where to put the format for context_logger only without altering any other parameter?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
Delgancommented, Apr 10, 2021

I just came to realization that you can use patch() also.

context_logger = logger.patch(lambda r: r.update(message=r["message"] + f" with param={param}"))
context_logger.info("Start")
context_logger.info("Something")
context_logger.info("Something else")
context_logger.info("End")

Be aware not to use it with opt(colors=True) because colorization won’t work due to implementation details.

1reaction
Delgancommented, Apr 7, 2021

Thanks for the clarification. Currently, there is no easy way to achieve this. A possible workaround is to create a small utility function, although I agree this is not very satisfying.

def log(level, message):
    logger.log(level, message + f" with param={param}")

log("INFO", "Start")
log("INFO", "Something")
log("INFO", "Something else")
log("INFO", "End")

This is an interesting use case, though. It’s definitely something that should be possible in the future as I plan to make the message formatting customizable (see #318).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Logging Cookbook — Python 3.11.1 documentation
setLevel(logging.INFO) # set a format which is simpler for console use formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message ...
Read more >
Enabling logging in BIND
Create a log directory. On SOA (soa.grpX):. Create the log directory: $ sudo mkdir -p /var/log/bind $ sudo chown bind /var/log/bind ...
Read more >
Binding without creating a new logger #53 - Delgan/loguru
I'm wondering if we can have new APIs that can bind and unbind structured arguments in place (i.e. without creating a new logger)....
Read more >
BIND Logging - some basic recommendations
Included in this article is a sample logging configuration. BIND ... BIND's dynamic logging channel severity setting can be used to flexibly ...
Read more >
7.5. Logging in BIND 8 and 9
BIND 4 had an extensive logging system, writing information to a debug file and ... TIP: You can configure a channel to log...
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