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.

Pass log kwargs to custom sink

See original GitHub issue

Loguru supports string formatting, but it seems that these kwargs are dropped if there is nothing to format in the string. I would think these would be passed to the custom sink, but it does not seem like this is the case.

See the below example.

>>> from loguru import logger
>>> 
>>> def callback(payload):
...     print(payload)
... 
>>> logger.start(callback, serialize=True)
>>> logger.info("testing", hello="world")
2018-12-08 15:56:07.675 | INFO     | __main__:<module>:1 - yoo
{"text": "2018-12-08 15:56:07.675 | INFO     | __main__:<module>:1 - testing\n", "record": {"elapsed": {"repr": "0:01:35.065585", "seconds": 95.065585}, "exception": null, "extra": {}, "file": {"name": "<stdin>", "path": "<stdin>"}, "function": "<module>", "level": {"icon": "\u2139\ufe0f", "name": "INFO", "no": 20}, "line": 1, "message": "testing", "module": "<stdin>", "name": "__main__", "process": {"id": 22013, "name": "MainProcess"}, "thread": {"id": 140289032955392, "name": "MainThread"}, "time": {"repr": "2018-12-08 15:56:07.675593-06:00", "timestamp": 1544306167.675593}}}
>>> logger.info("testing {hello}", hello="world")
2018-12-08 15:58:37.702 | INFO     | __main__:<module>:1 - testing world
{"text": "2018-12-08 15:58:37.702 | INFO     | __main__:<module>:1 - testing world\n", "record": {"elapsed": {"repr": "0:04:05.092374", "seconds": 245.092374}, "exception": null, "extra": {}, "file": {"name": "<stdin>", "path": "<stdin>"}, "function": "<module>", "level": {"icon": "\u2139\ufe0f", "name": "INFO", "no": 20}, "line": 1, "message": "testing world", "module": "<stdin>", "name": "__main__", "process": {"id": 22013, "name": "MainProcess"}, "thread": {"id": 140289032955392, "name": "MainThread"}, "time": {"repr": "2018-12-08 15:58:37.702382-06:00", "timestamp": 1544306317.702382}}}

I would expect that {"hello": "world"} to be somewhere in the payload passed to the custom sink

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
Delgancommented, May 17, 2020

Well, I’m sorry it took a year and a half, but… This feature is finally available. 🙂

The **kwargs arguments are automatically added to the extra dict. This can be disabled using .opt(capture=False).

0reactions
volfcocommented, May 17, 2020

🥳

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python: Logging exceptions with custom *kwargs
My Question: Is there an easy way to use custom kwargs with logging.exception? A minimal example follows: import logging import logging.handlers def ...
Read more >
Python Logging Guide - Best Practices and Hands-on Examples
This method is useful when using custom logging levels. Logger.exception(msg, *args, **kwargs) issues log requests with the logging level ...
Read more >
A better way to logging in Python | F5 - Squashing Bugs
This basic logging decorator looks good and already does what we originally set out to achieve. As long as a method is decorated...
Read more >
Logger - loguru documentation - Read the Docs
An object to dispatch logging messages to configured handlers. The Logger is the core object of loguru , every logging configuration and usage...
Read more >
loguru Documentation - Read the Docs
context_logger.info("Use kwargs to add context during formatting: ... The logged message passed to all added sinks is nothing more than a ...
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