Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FilteringBoundLogger exception() method has different API #530

Closed
alexcouper opened this issue Jul 15, 2023 · 2 comments
Closed

FilteringBoundLogger exception() method has different API #530

alexcouper opened this issue Jul 15, 2023 · 2 comments

Comments

@alexcouper
Copy link
Contributor

FilteringBoundLogger exception call fails with positional arguments.

Example

import structlog
from structlog import get_logger
from structlog.stdlib import LoggerFactory

structlog.configure(logger_factory=LoggerFactory())
log = get_logger("test")
log.error("%s %s %s", "bob", "alice", "fred")
log.exception("%s %s %s", "bob", "alice", "fred")

Expected output:

2023-07-15 11:30:42 [error    ] bob alice fred
2023-07-15 11:30:42 [error    ] bob alice fred

Actual output:

2023-07-15 11:30:42 [error    ] bob alice fred
Traceback (most recent call last):
  File "/Users/alex/Work/syndis/aftra/debug/2023-07-13-logging.py", line 30, in <module>
    log.exception("%s %s %s", "bob", "alice", "fred")
TypeError: exception() takes 2 positional arguments but 5 were given

Some historical digging

b71aabb#diff-b87fe76f9eed7fd535af19eade4ac979499ddaaaba4611af36cc59f4b7f247fc introduced the exception method without the *args part of the signature.

That implementation was also later used for the exception method used by FilteringBoundLogger here

A later fix was made to exception to "add pos arg support to exception" here

But no corresponding fix was made to the functions found in _log_levels.py.

Question

Any reason I shouldn't provide a fix, that adjusts the _log_levels.py arguments to include *args and pass them through to the error call?

@alexcouper
Copy link
Contributor Author

alexcouper commented Jul 20, 2023

This is the minimum set of configuration needed to make %s style formatting work with the .exception method (afaict):

structlog.configure(
    processors=[
        structlog.stdlib.PositionalArgumentsFormatter(),
        structlog.processors.format_exc_info,
        structlog.stdlib.render_to_log_kwargs,
    ],
    logger_factory=structlog.stdlib.LoggerFactory(),
    wrapper_class=structlog.stdlib.BoundLogger,
)

log = get_logger()
log.info("this is too %s!", "easy")
log.exception("%s %s %s", "bob", "alice", "fred")

Extra info:

  • The .info call works straight out of the box with no configuration.
  • Changing wrapper_class to structlog.make_filtering_bound_logger(logging.INFO) with the above configuration causes .exception to fail.

@hynek
Copy link
Owner

hynek commented Jul 22, 2023

Thanks for the digging, that's very helpful!

@hynek hynek closed this as completed Jul 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants