Skip to content

Pass log kwargs to custom sink #2

Closed

Description

@volfco

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

Activity

  1. volfco commented on Dec 8, 2018

    @volfco
    Author

    Reading a bit more it looks like you can use logger.bind().info, but that seems like an unnecessary extra step.

  2. Delgan commented on Dec 9, 2018

    @Delgan
    Owner

    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 for log(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 only a has been formatted and that only b should be passed somewhere else.

    I agree that structlog is 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?

  3. volfco commented on Dec 9, 2018

    @volfco
    Author

    I didn't know structlog existed. Neat. I guess- yes.

    I don't see why it matters if the data is used by .format or not? If I'm consuming the data in an output function, I should be aware that the message might already be formatted.

  4. Delgan commented on Mar 3, 2019

    @Delgan
    Owner

    @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:

    logger.start(sys.stderr, format="[{time}][{level}] {message} | {extra}")
    
    app, version = "MyApp", "0.2.3"
    ip, port = "192.168.0.10", 22
    
    logger.info("Starting '{}' (v{})", app, version)
    logger.bind(ip=ip, port=port).info("Sending a request")

    For advanced structured logging, Loguru can't beat structlog anyway, which is a very well polished library.

  5. added
    wontfix
    feature
    on Mar 3, 2019
  6. Delgan commented on May 17, 2020

    @Delgan
    Owner

    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).

  7. volfco commented on May 17, 2020

    @volfco
    Author

    🥳

  8. removed
    wontfix
    on May 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions