: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
chore(devex): quiet manage.py shell startup noise (#61374)
Julian Bezc64ff03aa61f
diff --git a/posthog/management/commands/shell.py b/posthog/management/commands/shell.pynew file mode 100644index 000000000000..f1c2b7d5dc10--- /dev/null+++ b/posthog/management/commands/shell.py@@ -0,0 +1,29 @@+import logging++from django.conf import settings+from django.core.management.commands.shell import Command as DjangoShellCommand+++class Command(DjangoShellCommand):+ """Quieter `manage.py shell`.++ Startup logging is forced to ERROR in settings while an interactive shell is+ detected (see posthog/settings/logs.py), keeping app-ready noise out of the+ prompt. Restore the normal level here — right before the REPL opens — so+ logging behaves as usual inside the session.++ Django 5.2 prints "N objects imported automatically" at verbosity >= 1. Treat+ the default (1) as quiet; the auto-imports still happen, just silently. Pass+ `-v 2` to see the full import list.+ """++ def handle(self, **options):+ # Restore both loggers that LOGGING pins to the (ERROR) startup level — the+ # root logger and the explicitly-configured `django` one. A named logger+ # with its own level ignores changes to root, so resetting only root would+ # leave Django's own logs (ORM queries, request lifecycle) suppressed.+ for name in ("", "django"):+ logging.getLogger(name).setLevel(settings.SHELL_LOG_LEVEL)+ if options.get("verbosity") == 1:+ options["verbosity"] = 0+ return super().handle(**options)diff --git a/posthog/settings/base_variables.py b/posthog/settings/base_variables.pyindex 36de9f02c95c..6d49343cbf78 100644--- a/posthog/settings/base_variables.py+++ b/posthog/settings/base_variables.py@@ -18,6 +18,13 @@ TEST = get_from_env("test" in sys.argv or "reset_test_clickhouse_db" in sys.argv or sys.argv[0].endswith("pytest"),type_cast=str_to_bool,)+# Interactive shells where startup noise — app-ready logs, third-party deprecation+# warnings — should stay out of the prompt. Matches Django's own subcommand+# resolution (argv[1]; global options come after the subcommand, not before).+# `shell_plus` is intentionally excluded: it has no command override to restore the+# level once the REPL opens, so forcing ERROR would silence its whole session. For+# `dbshell` there is no Python REPL (it execs the DB client), so nothing to restore.+IS_INTERACTIVE_SHELL: bool = len(sys.argv) > 1 and sys.argv[1] in ("shell", "dbshell")STATIC_COLLECTION = get_from_env("STATIC_COLLECTION", False, type_cast=str_to_bool)DEMO: bool = get_from_env("DEMO", False, type_cast=str_to_bool) # Whether this is a managed demo environmentCLOUD_DEPLOYMENT: str | None = get_from_env(@@ -65,7 +72,7 @@ DUCKGRES_PG_PORT: int = get_from_env("DUCKGRES_PG_PORT", 5432, type_cast=int)# Bulk deletion operations can be disabled during database migrationsDISABLE_BULK_DELETES: bool = get_from_env("DISABLE_BULK_DELETES", False, type_cast=str_to_bool)-if DEBUG and not TEST:+if DEBUG and not TEST and not IS_INTERACTIVE_SHELL:logger.warning(["Environment variable DEBUG is set - PostHog is running in DEVELOPMENT MODE!",diff --git a/posthog/settings/logs.py b/posthog/settings/logs.pyindex 0ec9261d5f59..d3f81c56ce8f 100644--- a/posthog/settings/logs.py+++ b/posthog/settings/logs.py@@ -1,14 +1,29 @@import osimport logging+import warningsimport threadingimport structlog-from posthog.settings.base_variables import DEBUG, TEST+from posthog.settings.base_variables import DEBUG, IS_INTERACTIVE_SHELL, TEST# Setup loggingLOGGING_FORMATTER_NAME = os.getenv("LOGGING_FORMATTER_NAME", "default")-DEFAULT_LOG_LEVEL = os.getenv("DJANGO_LOG_LEVEL", "ERROR" if TEST else "INFO")++# The level the interactive shell command restores once the REPL is ready — see+# posthog/management/commands/shell.py. Startup is forced to ERROR below so app-ready+# logs don't clutter the prompt; this is what logging looks like inside the session.+SHELL_LOG_LEVEL = os.getenv("DJANGO_LOG_LEVEL", "INFO")++DEFAULT_LOG_LEVEL = "ERROR" if IS_INTERACTIVE_SHELL else os.getenv("DJANGO_LOG_LEVEL", "ERROR" if TEST else "INFO")++# Third-party warnings that fire during `django.setup()`, before any command's handle()+# runs. They are pure noise in a REPL, so drop them only for interactive shells.+if IS_INTERACTIVE_SHELL:+ warnings.filterwarnings("ignore", message=r"pkg_resources is deprecated.*", category=UserWarning)+ warnings.filterwarnings(+ "ignore", message=r"Accessing the database during app initialization.*", category=RuntimeWarning+ )class FilterStatsd(logging.Filter):