structlog icon indicating copy to clipboard operation
structlog copied to clipboard

Cannot use PositionalArgumentsFormatter with FilteringBoundLogger

Open jerr0328 opened this issue 3 years ago • 3 comments

I was switching to using make_filtering_bound_logger when I noticed that using a custom Gunicorn logger (that basically maps to structlog logging) wasn't working anymore. The issue is that the logger is doing something like log.info("Starting gunicorn %s", __version__) which then caused an error:

TypeError: _make_filtering_bound_logger.<locals>.make_method.<locals>.meth() takes 2 positional arguments but 3 were given

I can open a PR to simply pass through *args as well (in the level and log methods) which should clear up the error, though I don't know if this could cause downstream issues.

jerr0328 avatar Aug 31 '22 11:08 jerr0328

This will need a bit more thought, perhaps make_filtering_bound_logger should move to stdlib so it can create a subclass of BoundLogger, rather than BoundLoggerBase? The BoundLogger class handles positional arguments properly so it can be used with PositionalArgumentsFormatter. It might make sense to move that code out of _log_levels.py anyways to avoid circular imports.

jerr0328 avatar Aug 31 '22 14:08 jerr0328

I think I need more context here – how did you manage to make gunicorn use make_filtering_bound_logger? The whole point of it is to have very fast level filters if you don't use stdlib, which has level filters built in?

hynek avatar Sep 10 '22 09:09 hynek

Gunicorn allows you to specify a logger class to use: https://docs.gunicorn.org/en/stable/settings.html#logger-class, so by adapting it similar to what I found in this gist, I was able to have Gunicorn log with a structlog logger. However, I can't control how gunicorn calls their logger, and they still interface with it as if it was a stdlib logger.

jerr0328 avatar Sep 11 '22 07:09 jerr0328

hey sorry for the delay – would you mind checking if https://github.com/hynek/structlog/pull/454 fixes this?

hynek avatar Oct 07 '22 08:10 hynek

Thanks, I haven't had a chance to fully test it but looking at the code, it looks like it should work! 🎉

jerr0328 avatar Oct 10 '22 10:10 jerr0328

@hynek Can I ask one small question? When this changes will be available? Just approximately 😉

sshishov avatar Nov 01 '22 15:11 sshishov

The 22.2 is largely feature-complete, but I need to tie up a few strings in documentation (notably add a Celery recipe) and a bit of admin work. Shouldn't be too long, since I want it out too, but can't commit to a date.

hynek avatar Nov 02 '22 08:11 hynek