feat: Centralize logging setup with structlog integration#179
Open
feat: Centralize logging setup with structlog integration#179
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #179 +/- ##
==========================================
+ Coverage 96.12% 96.44% +0.32%
==========================================
Files 92 97 +5
Lines 3222 3540 +318
==========================================
+ Hits 3097 3414 +317
- Misses 125 126 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.logging—foreign_pre_chainprocessor that extracts structured fields (path, method, status, duration, extra items) from Gunicorn access log records. Configurable viaACCESS_LOG_EXTRA_ITEMSenv var.%(message)shandler for pure CLF output.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. Replace the
LOGGINGdict insettings/common.pyDjango's
dictConfig(LOGGING)call duringdjango.setup()becomes a no-op.2. Set env vars
3. Delete
api/util/logging.pyIt duplicates
JsonFormatterwhich now lives incommon.core.logging.