Skip to content

fix(logging): apply format/level when logging preconfigured#4204

Open
MikeGoldsmith wants to merge 6 commits intoopen-telemetry:mainfrom
honeycombio:mike/fix-logging-preconfigured
Open

fix(logging): apply format/level when logging preconfigured#4204
MikeGoldsmith wants to merge 6 commits intoopen-telemetry:mainfrom
honeycombio:mike/fix-logging-preconfigured

Conversation

@MikeGoldsmith
Copy link
Member

Description

Fixes logging instrumentation when logging is already configured (autoinstrumentation, Gunicorn, Flask).

Problem: logging.basicConfig() is a no-op when root logger has handlers. OTel trace context gets injected but isn't visible because format string doesn't include the fields.

Solution: Added _apply_log_settings() that checks if root logger has handlers and updates existing handler formatters + level, or falls back to basicConfig(). Saves/restores old state for clean uninstrumentation.

Supersedes #2384
Fixes #2346

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

  • Added unit tests
    • Pre-configured logging (main use case)
    • Multiple handlers updated and restored
    • Handlers without formatters
    • Instrument/uninstrument cycles
    • Backward compatibility (unconfigured logging)
  • All existing test pass

#Does This PR Require a Core Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

basicConfig() no-op when handlers exist (autoinstrumentation, Gunicorn, Flask).
Fix: update existing handlers instead of only calling basicConfig.
Saves/restores state for clean uninstrumentation.

Supersedes open-telemetry#2384, fixes open-telemetry#2346
@MikeGoldsmith MikeGoldsmith requested a review from a team as a code owner February 13, 2026 17:41
…-contrib into mike/fix-logging-preconfigured
- Clear handlers before basicConfig tests
- Run ruff format
caplog doesn't capture when basicConfig called before fixture setup.
Use custom handler to capture records instead.
LoggingInstrumentor().uninstrument()

# Clear handlers to ensure basicConfig is called
root = logging.getLogger()
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you think mocking hasHandlers will work as well?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, good idea - I'll take a look at doing that 👍🏻

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated in ffb82ac

@xrmx xrmx moved this to Ready for review in @xrmx's Python PR digest Feb 16, 2026
@MikeGoldsmith
Copy link
Member Author

Looks like a flakey test in instrumentation-tornado causing the build failure - not related to changes in this PR.

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

Labels

None yet

Projects

Status: Ready for review

Development

Successfully merging this pull request may close these issues.

OTEL_PYTHON_LOG_CORRELATION is mentioned in doc, but does not seem to work.

2 participants

Comments