Skip to content

[QUESTION] Is merge_contextvars required in the processor chain for AsyncBoundLogger or not? #346

Closed
@SanchithHegde

Description

@SanchithHegde

From AsyncBoundLogger's docstring (FYI, the warning is not rendered on the API reference page):

.. warning: Since the processor pipeline runs in a separate thread,
    `structlog.contextvars.merge_contextvars` does **nothing** and should
    be removed from you processor chain.

Following the aforementioned warning, if I remove structlog.contextvars.merge_contextvars from my processor chain, none of the context variables (added using structlog.contextvars.bind_contextvars) are displayed in the log records. Only if I add it to the processor chain, context variables are displayed. Am I doing something wrong?

My structlog related code:

shared_processors: List[structlog.types.Processor] = [
    structlog.stdlib.add_logger_name,
    structlog.stdlib.add_log_level,
    add_request_id,  # Custom processor to add request ID to `event_dict` using `starlette_context`
    structlog.processors.TimeStamper(fmt="iso", utc=True),
    structlog.processors.StackInfoRenderer(),
    structlog.processors.format_exc_info,
]

structlog.configure(
    processors=[
        # structlog.contextvars.merge_contextvars,
        structlog.stdlib.filter_by_level,
        *shared_processors,
        structlog.stdlib.PositionalArgumentsFormatter(),
        structlog.stdlib.ProcessorFormatter.wrap_for_formatter,
    ],
    context_class=dict,
    logger_factory=structlog.stdlib.LoggerFactory(),
    wrapper_class=structlog.stdlib.AsyncBoundLogger,
    cache_logger_on_first_use=True,
)

Python version: 3.9.6
structlog version: 21.1.0

If any more info is required from my end, let me know, I'll provide it.

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

    Issue actions