Skip to content

Honor ORT_LOGGING_LEVEL environment variable on Python import#27645

Open
edenfunf wants to merge 1 commit intomicrosoft:mainfrom
edenfunf:fix/honor-ort-logging-level-env-var
Open

Honor ORT_LOGGING_LEVEL environment variable on Python import#27645
edenfunf wants to merge 1 commit intomicrosoft:mainfrom
edenfunf:fix/honor-ort-logging-level-env-var

Conversation

@edenfunf
Copy link

What

  • CreateOrtEnv() in onnxruntime/python/onnxruntime_pybind_module.cc now reads the ORT_LOGGING_LEVEL environment variable before constructing OrtEnv, so the configured severity is active from the very first log message emitted during import onnxruntime.
  • The identical fix is applied to orttraining/orttraining/python/orttraining_python_module.cc.
  • A new get_default_logger_severity() Python API is added (companion to the existing set_default_logger_severity()) and exported from onnxruntime/__init__.py.
  • Two new tests in onnxruntime_test_python.py cover the new API and the env-var behaviour.

Why

ORT_LOGGING_LEVEL is documented as a way to suppress noisy log output (e.g. GPU-discovery warnings in environments without a GPU), but it had no effect because CreateOrtEnv() hardcoded ORT_LOGGING_LEVEL_WARNING. Users were forced to redirect stderr or call set_default_logger_severity() after import, by which point the spurious warnings had already been emitted.

Fixes #27092

How

Env::Default().GetEnvironmentVar("ORT_LOGGING_LEVEL") is read at the start of CreateOrtEnv(). If the value is a valid integer in [0, 4] it replaces the default ORT_LOGGING_LEVEL_WARNING; any invalid or absent value leaves the existing default unchanged.

Test plan

  • test_get_default_logger_severity – verifies the new getter returns a value in the valid range.
  • test_ort_logging_level_env_var – spawns a subprocess for each valid severity level (0–4) with ORT_LOGGING_LEVEL set, then checks that get_default_logger_severity() returns the expected value immediately after import.

import subprocess

for level in [0, 1, 2, 3, 4]:
result = subprocess.run(

Check warning

Code scanning / lintrunner

RUFF/PLW1510

`subprocess.run` without explicit `check` argument. See https://docs.astral.sh/ruff/rules/subprocess-run-without-check

def test_ort_logging_level_env_var(self):
# Verify that ORT_LOGGING_LEVEL is honored on import by running a subprocess.
import subprocess

Check notice

Code scanning / lintrunner

RUFF/PLC0415

`import` should be at the top-level of a file. See https://docs.astral.sh/ruff/rules/import-outside-top-level
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Updates Python module initialization so ORT_LOGGING_LEVEL is applied before OrtEnv is created, making the configured logging severity effective from the first log message during import onnxruntime (and similarly for training). Also adds a small Python API surface + tests to validate the behavior.

Changes:

  • Read and validate ORT_LOGGING_LEVEL during CreateOrtEnv() in both inference and training Python modules.
  • Add get_default_logger_severity() Python API and export it from onnxruntime/__init__.py.
  • Add Python tests covering the new getter and env-var-on-import behavior via subprocess.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
onnxruntime/python/onnxruntime_pybind_module.cc Parse ORT_LOGGING_LEVEL before OrtEnv creation so import-time logs honor configured severity.
orttraining/orttraining/python/orttraining_python_module.cc Apply the same import-time logging severity behavior for the training Python module.
onnxruntime/python/onnxruntime_pybind_state.cc Expose get_default_logger_severity() via pybind.
onnxruntime/__init__.py Re-export get_default_logger_severity from the Python top-level package.
onnxruntime/test/python/onnxruntime_test_python.py Add tests for the new getter and for ORT_LOGGING_LEVEL behavior on import (subprocess-based).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +109 to +113
severity = onnxrt.get_default_logger_severity()
self.assertIsInstance(severity, int)
self.assertGreaterEqual(severity, 0)
self.assertLessEqual(severity, 4)

The Python binding's CreateOrtEnv() function hardcoded ORT_LOGGING_LEVEL_WARNING
as the initial logging severity, which meant the ORT_LOGGING_LEVEL environment
variable had no effect on the messages emitted during module import.

This change reads ORT_LOGGING_LEVEL (integer 0–4) before creating the OrtEnv
instance so that the chosen severity is respected from the very first log message.
The same fix is applied to the training binding (orttraining_python_module.cc),
which contained an identical hardcoded value.

A companion get_default_logger_severity() Python API is added alongside the
existing set_default_logger_severity() so callers can inspect the active level,
and two new unit tests verify both the API and the env-var behaviour.

Fixes microsoft#27092
@edenfunf edenfunf force-pushed the fix/honor-ort-logging-level-env-var branch from 104a143 to f7f7782 Compare March 14, 2026 10:28
edenfunf added a commit to edenfunf/onnxruntime that referenced this pull request Mar 14, 2026
Add two unit tests that cover the ORT_LOGGING_LEVEL environment variable
behaviour introduced in microsoft#27645:
- test_get_default_logger_severity: verifies the new getter API returns a
  valid severity level
- test_ort_logging_level_env_var: spawns a subprocess for each valid level
  (0-4) and confirms get_default_logger_severity() reflects the env var

While adding the subprocess-based test, address two ruff findings:
- PLC0415: move `import subprocess` to module-level instead of inside the
  test method body
- PLW1510: pass explicit `check=False` to subprocess.run()
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.

Static pybind lm_info ignores env ORT_LOGGING_LEVEL

2 participants