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

public access to logger._context #266

Closed
radix opened this issue Jun 23, 2020 · 5 comments
Closed

public access to logger._context #266

radix opened this issue Jun 23, 2020 · 5 comments

Comments

@radix
Copy link
Contributor

radix commented Jun 23, 2020

Am I bad for wanting to access logger._context?

Here's my use case: exception logging with extra context from inner frames in the stack.

SLOG = structlog.get_logger()

def erroneous(log, a):
    log = log.bind(a=a)
    try:
        1 / 0
    except Exception as e:
        e._structlog_context = log._context  # This will include `a` and anything else that was already bound to the log
        raise

I don't want to log directly in my exception handler because there's already code higher up the stack which does its own logging:

def main():
    log = SLOG.bind(key="value")
    try:
        cool()
        erroneous(log, 1)
        yeah()
    except Exception as e:
        log.error("oh no", exc_info=True)

Then, I could write a processor that looks for _structlog_context on logged exceptions and return that additional context.

Really, all this exception-handling stuff is kinda orthogonal to request I have, but it shows my motivation. This "extra context on exception" feature could be implemented without being able to access an existing log's context; however, it naturally leads me to want to attach my logger's context to these exceptions, since those loggers are already convenient containers for all the context I want to attach.

So, I'm already doing this in production, but I'm having to access the private _context attribute. Is there some other way to get the context that I missed? Can we have a get_context() method?

@hynek
Copy link
Owner

hynek commented Jun 25, 2020

I've already promised someone somewhere to keep _context around. Would you agree that structlog.get_context(SLOG) might be the safer bet?

@chiragjn
Copy link

+1 for a public method to get the bound context.
For use cases like in OP, I use threadlocal wrapper as the context class to avoid all the passing around. But here is another use case:

We use _context to extract the context and put into request headers to pass it around to other micro-services which gets read and rebound, allowing us to correlate logs across services.

@hynek
Copy link
Owner

hynek commented Jun 25, 2020

Also: should we return the actual context or a copy? 🤔

@hynek hynek closed this as completed in 33008b0 Jun 29, 2020
@hynek
Copy link
Owner

hynek commented Jun 29, 2020

I have added it in 33008b0, pls yell if something is missing.

@radix
Copy link
Contributor Author

radix commented Jun 29, 2020

Thanks @hynek, sorry I was not around to respond to your comment. This looks perfect.

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

3 participants