Repository navigation
Closed
Labels
featureRequest for adding a new feature
Description
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
Metadata
Metadata
Assignees
Labels
featureRequest for adding a new feature
Activity
volfco commentedon Dec 8, 2018
Reading a bit more it looks like you can use
logger.bind().info, but that seems like an unnecessary extra step.Delgan commentedon Dec 9, 2018
Hey, thanks for your interest and suggestion!
So, you are looking for a behavior similar to the one of the
structlog, right?I thought about it, unfortunately I do not think it's possible.
log(msg, *args, **kwargs)is just a convenient shorthand forlog(msg.format(*args, **kwargs)), adding special case and condition would break this simplicity rule.But then, there is also a implementation issue. What should I do in the case of
log("{a}", a=1, b=2)? There is no way to know that onlyahas been formatted and that onlybshould be passed somewhere else.I agree that
structlogis probably more suitable for this use-case. I can't think of a way to conveniently mix message formatting and structuring. :/logger.bind(hello="world").info("Message")is the way Loguru has been build to deal with in-line binding of extra data. Is it too verbose?volfco commentedon Dec 9, 2018
I didn't know structlog existed. Neat. I guess- yes.
I don't see why it matters if the data is used by
.formator not? If I'm consuming the data in an output function, I should be aware that the message might already be formatted.Delgan commentedon Mar 3, 2019
@volfco I am closing this issue as I can't find a solution that would fit in the api as I would like.
I prefer to avoid special cases and I try to stay close to the "one way of do it" principle. Structured logging in Loguru is performed using
.bind(). Using.log(msg, *arg, **kwgars)is just a shorthand for.log(msg.format(*args, **kwargs)), nothing more.A basic example:
For advanced structured logging, Loguru can't beat
structloganyway, which is a very well polished library.Add logged kwargs to the "extra" dict automatically (#2)
Delgan commentedon May 17, 2020
Well, I'm sorry it took a year and a half, but... This feature is finally available. 🙂
The
**kwargsarguments are automatically added to theextradict. This can be disabled using.opt(capture=False).volfco commentedon May 17, 2020
🥳