Skip to content

Enhance logging configuration to separate console and error outputs#3704

Open
jesbinjoseph wants to merge 3 commits into
ohcnetwork:developfrom
egovhealthcare:fix-pod-log-stderr
Open

Enhance logging configuration to separate console and error outputs#3704
jesbinjoseph wants to merge 3 commits into
ohcnetwork:developfrom
egovhealthcare:fix-pod-log-stderr

Conversation

@jesbinjoseph

@jesbinjoseph jesbinjoseph commented Jul 6, 2026

Copy link
Copy Markdown

Proposed Changes

  • Brief of changes made.

Associated Issue

  • Link to issue here, explain how the proposed solution will solve the reported issue/ feature request.

Architecture changes

  • Remove this section if not used

Merge Checklist

  • Tests added/fixed
  • Update docs in /docs
  • Linting Complete
  • Any other necessary step

Only PR's with test cases included and passing lint and test pipelines will be reviewed

@ohcnetwork/care-backend-maintainers @ohcnetwork/care-backend-admins

Summary by CodeRabbit

  • Bug Fixes
    • Improved log handling so routine messages and errors are separated more cleanly.
    • Error-level output is routed to error streams, while lower-severity logs go to standard output for clearer console visibility.
    • Updated test, deployment, and base logging configurations to keep behavior consistent across environments.

@jesbinjoseph jesbinjoseph requested a review from a team as a code owner July 6, 2026 10:55
@coderabbitai

coderabbitai Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 8d77584e-3a59-4a99-b9eb-a4b9961d0d36

📥 Commits

Reviewing files that changed from the base of the PR and between cb1cf64 and 81f0fd6.

📒 Files selected for processing (1)
  • config/settings/deployment.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • config/settings/deployment.py

📝 Walkthrough

Walkthrough

LOGGING settings in base, deployment, and test modules now split stdout and stderr output by severity. Sub-ERROR records go to console, while ERROR-and-above records use console_error, with selected loggers and root handlers updated accordingly.

Changes

Severity-based logging handler split

Layer / File(s) Summary
Base settings logging split
config/settings/base.py
Adds below_error filter, updates console to use stdout with that filter, adds console_error to stderr, sets time_logging to stdout explicitly, and expands root handlers to include both handlers.
Deployment settings logging split
config/settings/deployment.py
Adds below_error filter and console_error handler, expands root handlers, and reroutes django.db.backends and sentry_sdk to console_error.
Test settings logging split
config/settings/test.py
Adds import logging, introduces below_error and console_error, reroutes django.request, audit_log, and celery to console_error, and expands root handlers.

Estimated code review effort: 2 (Simple) | ~10 minutes

Related PRs: None identified.

Suggested labels: logging, config

Suggested reviewers: None identified.

Since three settings files got the same treatment, the copy-paste symmetry is at least doing one job well.

A filter for below,
stderr catches what burns red,
logs now know their place.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description is still the template, so it lacks actual change details, issue link, and checklist updates. Replace the template with a real summary, linked issue, architecture notes if needed, and completed merge checklist items.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: splitting console and error log output.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@greptile-apps

greptile-apps Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR separates console log output by severity: DEBUG/INFO/WARNING records go to sys.stdout via the existing console handler (now gated by a new below_error filter), while ERROR+ records go to sys.stderr via a new console_error handler. The change is applied consistently across the base, deployment, and test settings files.

  • base.py and deployment.py add the below_error CallbackFilter, a console_error stderr handler, and update the root logger to include both handlers; deployment-specific loggers (django.db.backends, sentry_sdk) are switched from console to console_error.
  • test.py mirrors the same pattern for the test environment, adding import logging and the new filter/handler definitions.

Confidence Score: 5/5

Safe to merge; the logging changes are isolated to configuration and carry no runtime logic changes.

The change is a straightforward logging configuration refactor with no effect on application logic. The below_error filter is correctly wired in all three settings files. The only gap is that time_logging in base.py is missing the filter, which is a minor inconsistency but not a correctness problem — time_logging_middleware realistically only emits INFO records.

config/settings/base.py — the time_logging handler should include the below_error filter for consistency with the rest of the handlers.

Important Files Changed

Filename Overview
config/settings/base.py Adds below_error filter, console_error stderr handler, and updates root logger; time_logging handler is missing the below_error filter, so ERROR+ records from time_logging_middleware would still land on stdout
config/settings/deployment.py Consistently applies below_error filter and console_error handler; existing loggers correctly updated to use console_error with propagate: False already in place
config/settings/test.py Adds stderr separation logic to test settings; the three named loggers (django.request, audit_log, celery) are missing propagate: False, causing ERROR records to be emitted twice (discussed in previous review thread)

Reviews (3): Last reviewed commit: "pre-commit: fix lint" | Re-trigger Greptile

Comment thread config/settings/test.py
Comment on lines 92 to 103
"django.request": {
"handlers": ["console"],
"handlers": ["console_error"],
"level": "ERROR",
},
"audit_log": {
"handlers": ["console"],
"handlers": ["console_error"],
"level": "ERROR",
},
"celery": {
"handlers": ["console"],
"handlers": ["console_error"],
"level": "ERROR",
},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Double-emission of ERROR logs in test settings

The django.request, audit_log, and celery loggers all send records directly to console_error, but none have "propagate": False. Because the root logger also includes console_error, every ERROR record from these loggers will be written to stderr twice — once by the logger's own handler and once by the root logger's handler. The deployment.py loggers (django.db.backends, sentry_sdk) correctly use "propagate": False to prevent exactly this. Adding the same key to the three loggers in test.py would fix the duplicate output.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
config/settings/base.py (1)

341-375: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Same filter/handler boilerplate duplicated across three settings files.

The below_error filter and console/console_error handlers are copy-pasted verbatim in base.py, deployment.py, and test.py. Since base.py presumably is the base for the others, consider defining this once and letting the other files inherit/extend it, rather than tripling the maintenance surface for any future tweak (e.g., changing the split threshold).
[recommended_refactor_note]

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@config/settings/base.py` around lines 341 - 375, The logging filter/handler
setup for `below_error`, `console`, and `console_error` is duplicated across
`base.py`, `deployment.py`, and `test.py`; refactor it so the shared
configuration lives in `base.py` and the other settings modules inherit or
extend it instead of copying the same `filters`, `handlers`, and `root` entries
verbatim. Keep the existing behavior for `time_logging_middleware` and
`time_logging`, but centralize the common console split logic to reduce
maintenance across the settings files.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@config/settings/test.py`:
- Around line 91-105: Add propagate: False to the django.request, audit_log, and
celery logger entries in the logging config so their ERROR messages do not
bubble up to the root logger and get duplicated on stderr. Update the loggers
block in config/settings/test.py by setting propagate to False for each of these
named loggers while keeping their existing console_error handler and ERROR
level.

---

Nitpick comments:
In `@config/settings/base.py`:
- Around line 341-375: The logging filter/handler setup for `below_error`,
`console`, and `console_error` is duplicated across `base.py`, `deployment.py`,
and `test.py`; refactor it so the shared configuration lives in `base.py` and
the other settings modules inherit or extend it instead of copying the same
`filters`, `handlers`, and `root` entries verbatim. Keep the existing behavior
for `time_logging_middleware` and `time_logging`, but centralize the common
console split logic to reduce maintenance across the settings files.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: bc246677-c2d1-4ba3-b680-b3294d7974c9

📥 Commits

Reviewing files that changed from the base of the PR and between c6ca8ef and cb1cf64.

📒 Files selected for processing (3)
  • config/settings/base.py
  • config/settings/deployment.py
  • config/settings/test.py

Comment thread config/settings/test.py
Comment on lines 91 to +105
"loggers": {
"django.request": {
"handlers": ["console"],
"handlers": ["console_error"],
"level": "ERROR",
},
"audit_log": {
"handlers": ["console"],
"handlers": ["console_error"],
"level": "ERROR",
},
"celery": {
"handlers": ["console"],
"handlers": ["console_error"],
"level": "ERROR",
},
},
"root": {"level": "INFO", "handlers": ["console"]},
"root": {"level": "INFO", "handlers": ["console", "console_error"]},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

git ls-files config/settings && printf '\n--- test.py ---\n' && sed -n '1,220p' config/settings/test.py && printf '\n--- deployment.py ---\n' && sed -n '1,260p' config/settings/deployment.py

Repository: ohcnetwork/care

Length of output: 9939


Add propagate: False to these loggers. django.request, audit_log, and celery already write to console_error; letting them bubble to root just emits the same ERROR twice on stderr. config/settings/test.py:91-105

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@config/settings/test.py` around lines 91 - 105, Add propagate: False to the
django.request, audit_log, and celery logger entries in the logging config so
their ERROR messages do not bubble up to the root logger and get duplicated on
stderr. Update the loggers block in config/settings/test.py by setting propagate
to False for each of these named loggers while keeping their existing
console_error handler and ERROR level.

@codecov

codecov Bot commented Jul 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.66%. Comparing base (c6ca8ef) to head (81f0fd6).

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #3704   +/-   ##
========================================
  Coverage    79.66%   79.66%           
========================================
  Files          479      479           
  Lines        23010    23011    +1     
  Branches      2379     2379           
========================================
+ Hits         18330    18331    +1     
  Misses        4080     4080           
  Partials       600      600           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@vigneshhari

Copy link
Copy Markdown
Member

Can you make this as a plug, that was its fully safe for existing deployments ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants