Skip to content

feat(logs): add OTEL_LOG_LEVEL support#5115

Open
grvmishra788 wants to merge 24 commits into
open-telemetry:mainfrom
grvmishra788:worktree-issue-1059
Open

feat(logs): add OTEL_LOG_LEVEL support#5115
grvmishra788 wants to merge 24 commits into
open-telemetry:mainfrom
grvmishra788:worktree-issue-1059

Conversation

@grvmishra788

@grvmishra788 grvmishra788 commented Apr 16, 2026

Copy link
Copy Markdown
Contributor

Description

Fixes #1059: The OTEL_LOG_LEVEL environment variable has been defined in the SDK since the SDK was first written, but reading and applying it was never implemented.

With this change, On SDK module load, OTEL_LOG_LEVEL is read and applied to logging.getLogger("opentelemetry.sdk") - the root SDK logger. As a result, all SDK sub-modules (trace, metrics, logs, exporters) inherit the configured level. Accepted values: debug, info, warn, warning, error, critical (case-insensitive). Invalid values emit a WARNING to stderr.

What OTEL_LOG_LEVEL controls

OTEL_LOG_LEVEL is scoped to the SDK's own diagnostic output only. It does not filter OTel log records emitted by instrumented applications. This scope is:

  • Specified in the SDK environment-variables spec: "Log level used by the SDK internal logger", linked to error-handling.md#self-diagnostics.
  • Confirmed by SIG member srikanthccv in issue #1059: "The OTEL_LOG_LEVEL is for setting the log level for the loggers used internally by SDK not for your application logs."
  • Confirmed by jeremydvoss in closed PR #4203: "OTEL_LOG_LEVEL: Exists in the SDK but unused. Speced out to be the level for the SDK's internal logger, not the LoggingHandler."

Type of change

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

  • pytest opentelemetry-sdk/tests/logs/test_logs.py — all tests pass.
  • TestOtelLogLevelEnvVar.test_otel_log_level_to_python_mapping_accepted_values verifies the _OTEL_LOG_LEVEL_TO_PYTHON dict covers exactly the documented accepted values: {"debug", "info", "warn", "warning", "error", "critical"}.
  • Manual smoke test: set OTEL_LOG_LEVEL=debug before importing the SDK and confirm SDK debug messages appear; set OTEL_LOG_LEVEL=invalid and confirm a WARNING is emitted with the valid values listed.

Does This PR Require a Contrib Repo Change?

  • No.

Checklist:

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

@linux-foundation-easycla

linux-foundation-easycla Bot commented Apr 16, 2026

Copy link
Copy Markdown

CLA Signed

The committers listed above are authorized under a signed CLA.

@grvmishra788 grvmishra788 changed the title Worktree issue 1059 feat(logs): add OTEL_LOG_LEVEL support Apr 16, 2026

@MikeGoldsmith MikeGoldsmith left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @grvmishra788. I think we need to move this so the main sdk/__init__.py so we don't need users to import another module.

Comment thread opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py Outdated
Comment thread opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py Outdated
Comment thread CHANGELOG.md Outdated
@github-project-automation github-project-automation Bot moved this to Reviewed PRs that need fixes in Python PR digest Apr 17, 2026

@MikeGoldsmith MikeGoldsmith left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @grvmishra788 - looks really good. Just a few final tweaks for an errant new line and update to the changelog.

Comment thread opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py Outdated
Comment thread CHANGELOG.md Outdated
@grvmishra788

Copy link
Copy Markdown
Contributor Author

Hi @MikeGoldsmith! Could you please take a look at the PR now? It has been in review for a while.

@MikeGoldsmith MikeGoldsmith left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sorry for the delay, thanks @grvmishra788 🚀

@github-project-automation github-project-automation Bot moved this from Reviewed PRs that need fixes to Approved PRs in Python PR digest May 5, 2026
@grvmishra788

Copy link
Copy Markdown
Contributor Author

@MikeGoldsmith One of the CI checks failed CI / contrib / sdk-extension-aws-1 (pull_request) - a completely different package (opentelemetry-sdk-extension-aws) that this PR doesn't touch. This appears to be a flaky infrastructure issue. Do you have any recommendations as to how this should be handled?

@MikeGoldsmith

Copy link
Copy Markdown
Member

Ah, I see the problem - sdk-extension-aws-1 is failing because of the move to __init__.py 😦

Adding opentelemetry-sdk/src/opentelemetry/sdk/__init__.py converts opentelemetry.sdk from an implicit namespace package into a regular package. This breaks namespace package resolution — Python stops scanning other sys.path entries for opentelemetry.sdk.* subpackages, so the separately-installed opentelemetry-sdk-extension-aws (which provides opentelemetry.sdk.extension.aws) becomes unfindable:

ModuleNotFoundError: No module named 'opentelemetry.sdk.extension'

We need to move it somewhere else, but then we'll lose the automatic resolution we wanted by putting it in init. Maybe put it back in _internal/init and add it to configure_otel_defaults() in opentelemetry.sdk._configuration?

@MikeGoldsmith MikeGoldsmith left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Moving to request changes until we fix the resolution problem.

@github-project-automation github-project-automation Bot moved this from Approved PRs to Reviewed PRs that need fixes in Python PR digest May 7, 2026
@grvmishra788

Copy link
Copy Markdown
Contributor Author

Thanks for the PR!

Just a heads-up: we no longer update CHANGELOG.md directly. The changelog is now generated from changelog fragments using Towncrier.

Please add the appropriate changelog fragment for this change instead of editing CHANGELOG.md manually. You can find the instructions and expected format in CONTRIBUTING.md.

Thanks for letting me know. switched to Towncrier changelog fragment instead.

@tammy-baylis-swi

Copy link
Copy Markdown
Contributor

This PR and the auto-instrumentation PR (open-telemetry/opentelemetry-python-contrib#4542) should not conflict but noting that OTEL_LOG_LEVEL will be applied to two different code paths: SDK internal logs, and auto-instrumentation startup logs

Comment thread opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py
grvmishra788 and others added 2 commits May 27, 2026 13:16
Python has no TRACE level; the OTel declarative config schema includes
"trace" as a valid log level value, so mapping it to DEBUG aligns with
the config model.
@grvmishra788

grvmishra788 commented May 27, 2026

Copy link
Copy Markdown
Contributor Author

Hi @MikeGoldsmith @emdneto @pmcollins - this PR has been open for a while and I believe all the feedback has been addressed. Would appreciate a final review & merge when you get a chance. Thanks!

@aabmass aabmass enabled auto-merge June 3, 2026 20:15
@aabmass aabmass disabled auto-merge June 3, 2026 20:15
@aabmass

aabmass commented Jun 3, 2026

Copy link
Copy Markdown
Member

Hi @MikeGoldsmith @emdneto @pmcollins - this PR has been open for a while and I believe all the feedback has been addressed. Would appreciate a final review & merge when you get a chance. Thanks!

Did we you fix the namespace issue that Mike mentioned above?

@grvmishra788

grvmishra788 commented Jun 3, 2026

Copy link
Copy Markdown
Contributor Author

Hi @MikeGoldsmith @emdneto @pmcollins - this PR has been open for a while and I believe all the feedback has been addressed. Would appreciate a final review & merge when you get a chance. Thanks!

Did we you fix the namespace issue that Mike mentioned above?

Yes @aabmass, that issue is fixed now. I moved _configure_otel_log_level() into _logs/_internal/__init__.py instead of adding sdk/__init__.py, so the namespace package structure remains unchanged.

Comment thread opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/__init__.py Outdated
Comment thread opentelemetry-sdk/src/opentelemetry/sdk/_configuration/__init__.py
@github-actions

Copy link
Copy Markdown

This PR has been automatically marked as stale because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 days of this comment.
If you're still working on this, please add a comment or push new commits.

@github-actions github-actions Bot added the Stale label Jun 27, 2026
@github-actions github-actions Bot removed the Stale label Jul 1, 2026

@pmcollins pmcollins left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, thank you.

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

Labels

None yet

Projects

Status: Reviewed PRs that need fixes

Development

Successfully merging this pull request may close these issues.

Add support for OTEL_LOG_LEVEL env variable

8 participants