Skip to content

public access to logger._context #266

Closed
@radix

Description

@radix
Contributor

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?

Activity

hynek

hynek commented on Jun 25, 2020

@hynek
Owner

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

chiragjn

chiragjn commented on Jun 25, 2020

@chiragjn

+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

hynek commented on Jun 25, 2020

@hynek
Owner

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

hynek

hynek commented on Jun 29, 2020

@hynek
Owner

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

radix

radix commented on Jun 29, 2020

@radix
ContributorAuthor

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @hynek@radix@chiragjn

        Issue actions

          public access to logger._context · Issue #266 · hynek/structlog