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.36% +0.24%
==========================================
Files 92 96 +4
Lines 3222 3441 +219
==========================================
+ Hits 3097 3316 +219
Misses 125 125 ☔ 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
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.build_processor_formatter()— shared formatter builder used by both the root logger andGunicornJsonCapableLogger, ensuring consistent rendering.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).ProcessorFormatterfor bothjsonandgenericformats (access logs unchanged — still CLF for generic,GunicornAccessLogJsonFormatterfor JSON).sentry-sdkadded as an explicit dependency in thecommon-coregroup.What it doesn't change
JsonFormatterandJsonRecordstay for Gunicorn access log use.Migration guide for
flagsmith/flagsmith1. Replace the
LOGGINGdict insettings/common.pyDjango's
dictConfig(LOGGING)call duringdjango.setup()becomes a no-op.2. Set
APPLICATION_LOGGERSenv varThis ensures app loggers are at the correct level from boot.
3. Delete
api/util/logging.pyIt duplicates
JsonFormatterwhich now lives incommon.core.logging.