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(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.py
index 4b92fd585dad..d170774ffe99 100644
--- a/posthog/management/commands/persons_dedup.py
+++ b/posthog/management/commands/persons_dedup.py
@@ -106,6 +106,7 @@ from __future__ import annotations
import os
import json
import time
+import logging
from collections.abc import Callable, Iterator
from contextlib import contextmanager
from datetime import UTC, datetime
@@ -123,6 +124,9 @@ from posthog.dataclasses import frozen
from posthog.persons_db import persons_db_connection, persons_db_url
logger = 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.py
index 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 annotations
import re
import json
import uuid as uuid_mod
+import logging
from contextlib import contextmanager
from 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