Skip to content

Commit 104a143

Browse files
committed
Honor ORT_LOGGING_LEVEL environment variable on Python import
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 #27092
1 parent d6dcef1 commit 104a143

5 files changed

Lines changed: 63 additions & 2 deletions

File tree

onnxruntime/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
get_all_providers, # noqa: F401
5757
get_available_providers, # noqa: F401
5858
get_build_info, # noqa: F401
59+
get_default_logger_severity, # noqa: F401
5960
get_device, # noqa: F401
6061
get_ep_devices, # noqa: F401
6162
get_version_string, # noqa: F401

onnxruntime/python/onnxruntime_pybind_module.cc

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "onnxruntime_pybind_exceptions.h"
55
#include "onnxruntime_pybind_module_functions.h"
6+
#include <cstdlib>
67
#include <pybind11/stl.h>
78
#include "core/providers/get_execution_providers.h"
89
#include "onnxruntime_config.h"
@@ -33,7 +34,21 @@ OrtEnv* GetOrtEnv() {
3334
}
3435
static Status CreateOrtEnv() {
3536
Env::Default().GetTelemetryProvider().SetLanguageProjection(OrtLanguageProjection::ORT_PROJECTION_PYTHON);
36-
OrtEnv::LoggingManagerConstructionInfo lm_info{nullptr, nullptr, ORT_LOGGING_LEVEL_WARNING, "Default"};
37+
38+
// Allow the default logging severity to be configured via ORT_LOGGING_LEVEL before the first import.
39+
// Valid integer values: 0 (Verbose), 1 (Info), 2 (Warning), 3 (Error), 4 (Fatal).
40+
OrtLoggingLevel logging_level = ORT_LOGGING_LEVEL_WARNING;
41+
const std::string logging_env = Env::Default().GetEnvironmentVar("ORT_LOGGING_LEVEL");
42+
if (!logging_env.empty()) {
43+
char* end = nullptr;
44+
long level = std::strtol(logging_env.c_str(), &end, 10);
45+
if (end != logging_env.c_str() && *end == '\0' &&
46+
level >= ORT_LOGGING_LEVEL_VERBOSE && level <= ORT_LOGGING_LEVEL_FATAL) {
47+
logging_level = static_cast<OrtLoggingLevel>(level);
48+
}
49+
}
50+
51+
OrtEnv::LoggingManagerConstructionInfo lm_info{nullptr, nullptr, logging_level, "Default"};
3752
Status status;
3853
ort_env = OrtEnv::GetOrCreateInstance(lm_info, status, use_global_tp ? &global_tp_options : nullptr).release();
3954
if (!status.IsOK()) return status;

onnxruntime/python/onnxruntime_pybind_state.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,6 +1467,11 @@ void addGlobalMethods(py::module& m) {
14671467
default_logging_manager->SetDefaultLoggerSeverity(static_cast<logging::Severity>(severity));
14681468
},
14691469
"Sets the default logging severity. 0:Verbose, 1:Info, 2:Warning, 3:Error, 4:Fatal");
1470+
m.def(
1471+
"get_default_logger_severity", []() -> int {
1472+
return static_cast<int>(logging::LoggingManager::DefaultLogger().GetSeverity());
1473+
},
1474+
"Gets the default logging severity. 0:Verbose, 1:Info, 2:Warning, 3:Error, 4:Fatal");
14701475
m.def(
14711476
"set_default_logger_verbosity", [](int vlog_level) {
14721477
logging::LoggingManager* default_logging_manager = GetEnv().GetLoggingManager();

onnxruntime/test/python/onnxruntime_test_python.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,31 @@ def test_get_build_info(self):
104104
self.assertIsNot(onnxrt.get_build_info(), None)
105105
self.assertIn("Build Info", onnxrt.get_build_info())
106106

107+
def test_get_default_logger_severity(self):
108+
# Default severity should be WARNING (2) when ORT_LOGGING_LEVEL is not set.
109+
severity = onnxrt.get_default_logger_severity()
110+
self.assertIsInstance(severity, int)
111+
self.assertGreaterEqual(severity, 0)
112+
self.assertLessEqual(severity, 4)
113+
114+
def test_ort_logging_level_env_var(self):
115+
# Verify that ORT_LOGGING_LEVEL is honored on import by running a subprocess.
116+
import subprocess
117+
118+
for level in [0, 1, 2, 3, 4]:
119+
result = subprocess.run(
120+
[
121+
sys.executable,
122+
"-c",
123+
"import onnxruntime as ort; print(ort.get_default_logger_severity())",
124+
],
125+
capture_output=True,
126+
text=True,
127+
env={**os.environ, "ORT_LOGGING_LEVEL": str(level)},
128+
)
129+
self.assertEqual(result.returncode, 0, msg=result.stderr)
130+
self.assertEqual(result.stdout.strip(), str(level), msg=f"Expected severity {level}, got {result.stdout.strip()}")
131+
107132
def test_model_serialization(self):
108133
try:
109134
so = onnxrt.SessionOptions()

orttraining/orttraining/python/orttraining_python_module.cc

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "orttraining/python/orttraining_pybind_common.h"
55
#include "python/onnxruntime_pybind_mlvalue.h"
66

7+
#include <cstdlib>
78
#include "core/common/logging/logging.h"
89
#include "core/common/logging/severity.h"
910
#include "core/common/path_string.h"
@@ -171,7 +172,21 @@ onnxruntime::Environment& GetEnv() {
171172

172173
static Status CreateOrtEnv() {
173174
Env::Default().GetTelemetryProvider().SetLanguageProjection(OrtLanguageProjection::ORT_PROJECTION_PYTHON);
174-
OrtEnv::LoggingManagerConstructionInfo lm_info{nullptr, nullptr, ORT_LOGGING_LEVEL_WARNING, "Default"};
175+
176+
// Allow the default logging severity to be configured via ORT_LOGGING_LEVEL before the first import.
177+
// Valid integer values: 0 (Verbose), 1 (Info), 2 (Warning), 3 (Error), 4 (Fatal).
178+
OrtLoggingLevel logging_level = ORT_LOGGING_LEVEL_WARNING;
179+
const std::string logging_env = Env::Default().GetEnvironmentVar("ORT_LOGGING_LEVEL");
180+
if (!logging_env.empty()) {
181+
char* end = nullptr;
182+
long level = std::strtol(logging_env.c_str(), &end, 10);
183+
if (end != logging_env.c_str() && *end == '\0' &&
184+
level >= ORT_LOGGING_LEVEL_VERBOSE && level <= ORT_LOGGING_LEVEL_FATAL) {
185+
logging_level = static_cast<OrtLoggingLevel>(level);
186+
}
187+
}
188+
189+
OrtEnv::LoggingManagerConstructionInfo lm_info{nullptr, nullptr, logging_level, "Default"};
175190
Status status;
176191
OrtEnvPtr ort_env = OrtEnv::GetOrCreateInstance(lm_info, status, use_global_tp ? &global_tp_options : nullptr);
177192
if (!status.IsOK()) return status;

0 commit comments

Comments
 (0)