feat: Centralize logging setup with structlog integration#179
Open
feat: Centralize logging setup with structlog integration#179
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #179 +/- ##
==========================================
+ Coverage 96.12% 96.48% +0.36%
==========================================
Files 92 97 +5
Lines 3222 3552 +330
==========================================
+ Hits 3097 3427 +330
Misses 125 125 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
emyller
reviewed
Mar 26, 2026
Contributor
emyller
left a comment
There was a problem hiding this comment.
Looks great! Left a few questions.
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #31
What changed
Logging setup moves from Django settings into
flagsmith-common, running before Django loads. Both stdlib and structlog now route through a singleProcessorFormatter, so all output (app code, structlog, Gunicorn error logs) shares the same format and ISO timestamps.Key additions
setup_logging()incommon.core.logging— configures stdlib viadictConfig, then wires structlog through stdlib usingLoggerFactory(notPrintLoggerFactory). Acceptsapplication_loggersfor per-package level control andextra_foreign_processorsfor extending theforeign_pre_chain._SentryFriendlyProcessorFormatter— subclass ofProcessorFormatterthat snapshots and restoresrecord.msg/record.argsacross formatting, so Sentry'sLoggingIntegration(which reads these in afinallyblock after handlers run) sees the original message template for proper event grouping.sentry_processorincommon.core.sentry— structlog processor that sets Sentry context from event dicts. Hardwired into the structlog processor chain. Since structlog routes through stdlib, Sentry'sLoggingIntegrationautomatically captures ERROR+ logs.application_loggersallowlist — root logger stays at WARNING to suppress third-party noise; listed app loggers get the requested level. DEBUG overrides everything. Configurable viaAPPLICATION_LOGGERSenv var (comma-separated).make_gunicorn_access_processor()incommon.gunicorn.processors—foreign_pre_chainprocessor that extracts structured fields (path, method, status, duration, extra items) from Gunicorn access log records. Configurable viaACCESS_LOG_EXTRA_ITEMSenv var.ProcessorFormatter(respectingACCESS_LOG_LOCATION); generic mode keeps Gunicorn's own CLF formatter.ConsoleRendereronly enables ANSI colors when stdout is a TTY.sentry-sdkadded as an explicit dependency in thecommon-coregroup.environsused inensure_cli_envfor env var parsing.What it removes
GunicornAccessLogJsonFormatterandGunicornAccessLogJsonRecord— replaced by theforeign_pre_chainprocessor.LOGGING_DEFAULT_GENERIC_FORMATconstant —ConsoleRendererhandles generic formatting.PROMETHEUS_HISTOGRAM_BUCKETSandPROMETHEUS_HTTP_SERVER_RESPONSE_SIZE_HISTOGRAM_BUCKETSDjango settings — histogram buckets now use library defaults directly, removing import-time Django settings access that prevented pre-Django logging setup.What it keeps
JsonFormatterandJsonRecord—JsonRecordis still the schema definition used bymap_event_to_json_record.WSGI_EXTRA_SUFFIX_TO_CATEGORY,wsgi_extra_key_regex) in their original location.Migration guide for
flagsmith/flagsmith1. Disable Django's logging configuration in
settings/common.pyLOGGING_CONFIG = Nonetells Django to skip itsdictConfig()call duringdjango.setup(), preserving the handlers and logger levels configured bysetup_logging().2. Set env vars
3. Delete
api/util/logging.pyIt duplicates
JsonFormatterwhich now lives incommon.core.logging.4. Remove
PROMETHEUS_HISTOGRAM_BUCKETSsettingNo longer consumed by flagsmith-common.