: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(persons): restore persons_dedup log output in production (#87402)
Eli Reisman89a6d187421c
diff --git a/posthog/management/commands/persons_dedup.py b/posthog/management/commands/persons_dedup.pyindex 4b92fd585dad..d170774ffe99 100644--- a/posthog/management/commands/persons_dedup.py+++ b/posthog/management/commands/persons_dedup.py@@ -106,6 +106,7 @@ from __future__ import annotationsimport osimport jsonimport time+import loggingfrom collections.abc import Callable, Iteratorfrom contextlib import contextmanagerfrom datetime import UTC, datetime@@ -123,6 +124,9 @@ from posthog.dataclasses import frozenfrom posthog.persons_db import persons_db_connection, persons_db_urllogger = structlog.get_logger(__name__)+# The posthoganalytics SDK claims the "posthog" logger name and clamps it to WARNING at+# client init, so every INFO record this command emits is dropped and a run leaves no log.+logging.getLogger(__name__).setLevel(logging.INFO)VICTIMS_TABLE = "pg_temp.persons_dedup_victims"diff --git a/posthog/management/commands/test/test_persons_dedup.py b/posthog/management/commands/test/test_persons_dedup.pyindex 6717ad461dbb..499e423ea3d7 100644--- a/posthog/management/commands/test/test_persons_dedup.py+++ b/posthog/management/commands/test/test_persons_dedup.py@@ -3,6 +3,7 @@ from __future__ import annotationsimport reimport jsonimport uuid as uuid_mod+import loggingfrom contextlib import contextmanagerfrom typing import Any@@ -806,6 +807,35 @@ class TestPersonsDedupTargetGuard:assert "password" not in str(target[0]), "the log line must not carry credentials"+class TestPersonsDedupLogVisibility:+ def test_info_records_survive_the_posthog_logger_clamp(self):+ # posthoganalytics calls logging.getLogger("posthog").setLevel(WARNING) at client init,+ # which happens during django.setup(). Without an explicit level on this module's logger+ # every INFO record is dropped, and three production runs of this command left no record+ # of what they did. structlog's capture_logs() replaces the processor chain, so it cannot+ # see this -- the assertion has to go through a real stdlib handler.+ parent = logging.getLogger("posthog")+ module_logger = logging.getLogger(persons_dedup_command.__name__)+ captured: list[logging.LogRecord] = []++ class _Collector(logging.Handler):+ def emit(self, record: logging.LogRecord) -> None:+ captured.append(record)++ handler = _Collector()+ original_level = parent.level+ parent.setLevel(logging.WARNING)+ module_logger.addHandler(handler)+ try:+ persons_dedup_command.logger.info("persons_dedup.log_visibility_probe", team_id=1)+ finally:+ module_logger.removeHandler(handler)+ parent.setLevel(original_level)++ assert captured, "INFO records are dropped, so a production run would leave no log"+ assert captured[0].levelno == logging.INFO++class TestPersonsDedupPrivilegePreflight:def test_every_table_the_command_deletes_from_is_probed_for_delete(self):# The preflight exists so a missing grant aborts before any work. It is only worth