Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(logging): keep structlog bind() context on the logger that bound it (#121789)

Phil Haackcc7a49a1cde6
diff --git a/posthog/settings/logs.py b/posthog/settings/logs.py
index 8e0b2df41d9a..08d28290beda 100644
--- a/posthog/settings/logs.py
+++ b/posthog/settings/logs.py
@@ -84,7 +84,7 @@ structlog.configure(
*foreign_pre_chain,
structlog.stdlib.ProcessorFormatter.wrap_for_formatter,
],
- context_class=structlog.threadlocal.wrap_dict(dict),
+ context_class=dict,
logger_factory=structlog.stdlib.LoggerFactory(),
wrapper_class=structlog.stdlib.BoundLogger,
cache_logger_on_first_use=True,
diff --git a/posthog/settings/test_logs.py b/posthog/settings/test_logs.py
index 1caa7f6efb3d..8e668dd1b33a 100644
--- a/posthog/settings/test_logs.py
+++ b/posthog/settings/test_logs.py
@@ -1,11 +1,13 @@
import logging
import logging.config
+import structlog
from opentelemetry import (
context as otel_context,
trace,
)
from opentelemetry.trace import NonRecordingSpan, SpanContext, TraceFlags
+from structlog.testing import capture_logs
from posthog.settings import logs
@@ -43,6 +45,15 @@ def test_add_otel_trace_context_is_noop_without_active_span() -> None:
assert "span_id" not in event_dict
+def test_bound_context_stays_off_other_loggers_on_the_same_thread() -> None:
+ structlog.get_logger("test.binder").bind(bound_by_binder="value")
+
+ with capture_logs() as captured:
+ structlog.get_logger("test.other").info("unrelated event")
+
+ assert captured == [{"event": "unrelated event", "log_level": "info"}]
+
+
def test_logging_config_can_be_applied() -> None:
config = dict(logs.LOGGING)
config["disable_existing_loggers"] = False