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

structlog logger.exception doesn't accept positional arguments #543

Closed
gaganpreet opened this issue Aug 24, 2023 · 1 comment
Closed

structlog logger.exception doesn't accept positional arguments #543

gaganpreet opened this issue Aug 24, 2023 · 1 comment

Comments

@gaganpreet
Copy link

Hi,

I'm just diving into structlog (using the latest version, 23.1.0). I'm adding structlog to an existing codebase. I looked around the issue tracker if this was already reported but I didn't find anything other than #506.

We have a lot of existing log statements that I was hoping would be an easy switch as a drop-in replacements by switching logging.getLogger with structlog.get_logger. However, I'm running into a compatibility issue with logger.exception statements like this:

logger.exception("Error when copying %s", url)

These statements fail with this error:

TypeError: exception() takes 2 positional arguments but 3 were given

Only logger.exception behaves differently.

Here's a minimal reproducible example I pulled from our structlog integration that generates the error:

import logging
import orjson
import structlog
from structlog.contextvars import merge_contextvars

structlog.configure(
    cache_logger_on_first_use=True,
    wrapper_class=structlog.make_filtering_bound_logger(logging.INFO),
    processors=[
        merge_contextvars,
        structlog.stdlib.ExtraAdder(),
        structlog.processors.add_log_level,
        structlog.processors.format_exc_info,
        structlog.processors.TimeStamper(fmt="iso", utc=True),
        structlog.processors.StackInfoRenderer(),
        structlog.processors.JSONRenderer(serializer=orjson.dumps),
    ],
    logger_factory=structlog.BytesLoggerFactory(),
)

structlog_logger = structlog.get_logger()

stdlib_logger = logging.getLogger()


def log_exception(logger):
    try:
        raise Exception("This is a test exception")
    except Exception:
        logger.exception("This is a test exception %d", 1)


log_exception(stdlib_logger)
log_exception(structlog_logger)

The first logger.exception, using stdlib logger works as expected (without failing). The second log statement with structlog fails with an exception.

It's a couple of dozen places in our code where we have logger.exception with arguments and it won't be a huge effort to refactor for me, however, I'm curious if I'm missing something obvious here.

What should be the behaviour here? If this is the expected behaviour, what's the recommended approach here?

@gaganpreet
Copy link
Author

I found that there was a fix merged last month so I just have to wait till there's a new release #531

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

1 participant