Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions onnxruntime/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
get_all_providers, # noqa: F401
get_available_providers, # noqa: F401
get_build_info, # noqa: F401
get_default_logger_severity, # noqa: F401
get_device, # noqa: F401
get_ep_devices, # noqa: F401
get_version_string, # noqa: F401
Expand Down
17 changes: 16 additions & 1 deletion onnxruntime/python/onnxruntime_pybind_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "onnxruntime_pybind_exceptions.h"
#include "onnxruntime_pybind_module_functions.h"
#include <cstdlib>
#include <pybind11/stl.h>
#include "core/providers/get_execution_providers.h"
#include "onnxruntime_config.h"
Expand Down Expand Up @@ -33,7 +34,21 @@ OrtEnv* GetOrtEnv() {
}
static Status CreateOrtEnv() {
Env::Default().GetTelemetryProvider().SetLanguageProjection(OrtLanguageProjection::ORT_PROJECTION_PYTHON);
OrtEnv::LoggingManagerConstructionInfo lm_info{nullptr, nullptr, ORT_LOGGING_LEVEL_WARNING, "Default"};

// Allow the default logging severity to be configured via ORT_LOGGING_LEVEL before the first import.
// Valid integer values: 0 (Verbose), 1 (Info), 2 (Warning), 3 (Error), 4 (Fatal).
OrtLoggingLevel logging_level = ORT_LOGGING_LEVEL_WARNING;
const std::string logging_env = Env::Default().GetEnvironmentVar("ORT_LOGGING_LEVEL");
if (!logging_env.empty()) {
char* end = nullptr;
long level = std::strtol(logging_env.c_str(), &end, 10);
if (end != logging_env.c_str() && *end == '\0' &&
level >= ORT_LOGGING_LEVEL_VERBOSE && level <= ORT_LOGGING_LEVEL_FATAL) {
logging_level = static_cast<OrtLoggingLevel>(level);
}
}

OrtEnv::LoggingManagerConstructionInfo lm_info{nullptr, nullptr, logging_level, "Default"};
Status status;
ort_env = OrtEnv::GetOrCreateInstance(lm_info, status, use_global_tp ? &global_tp_options : nullptr).release();
if (!status.IsOK()) return status;
Expand Down
5 changes: 5 additions & 0 deletions onnxruntime/python/onnxruntime_pybind_state.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1467,6 +1467,11 @@ void addGlobalMethods(py::module& m) {
default_logging_manager->SetDefaultLoggerSeverity(static_cast<logging::Severity>(severity));
},
"Sets the default logging severity. 0:Verbose, 1:Info, 2:Warning, 3:Error, 4:Fatal");
m.def(
"get_default_logger_severity", []() -> int {
return static_cast<int>(logging::LoggingManager::DefaultLogger().GetSeverity());
},
"Gets the default logging severity. 0:Verbose, 1:Info, 2:Warning, 3:Error, 4:Fatal");
m.def(
"set_default_logger_verbosity", [](int vlog_level) {
logging::LoggingManager* default_logging_manager = GetEnv().GetLoggingManager();
Expand Down
25 changes: 25 additions & 0 deletions onnxruntime/test/python/onnxruntime_test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import pathlib
import platform
import queue
import subprocess
import sys
import threading
import unittest
Expand Down Expand Up @@ -104,6 +105,30 @@ def test_get_build_info(self):
self.assertIsNot(onnxrt.get_build_info(), None)
self.assertIn("Build Info", onnxrt.get_build_info())

def test_get_default_logger_severity(self):
# Default severity should be WARNING (2) when ORT_LOGGING_LEVEL is not set.
severity = onnxrt.get_default_logger_severity()
self.assertIsInstance(severity, int)
self.assertGreaterEqual(severity, 0)
self.assertLessEqual(severity, 4)

Comment on lines +110 to +114
def test_ort_logging_level_env_var(self):
# Verify that ORT_LOGGING_LEVEL is honored on import by running a 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
[
sys.executable,
"-c",
"import onnxruntime as ort; print(ort.get_default_logger_severity())",
],
capture_output=True,
check=False,
text=True,
env={**os.environ, "ORT_LOGGING_LEVEL": str(level)},
)
self.assertEqual(result.returncode, 0, msg=result.stderr)
self.assertEqual(result.stdout.strip(), str(level), msg=f"Expected severity {level}, got {result.stdout.strip()}")

def test_model_serialization(self):
try:
so = onnxrt.SessionOptions()
Expand Down
17 changes: 16 additions & 1 deletion orttraining/orttraining/python/orttraining_python_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "orttraining/python/orttraining_pybind_common.h"
#include "python/onnxruntime_pybind_mlvalue.h"

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

static Status CreateOrtEnv() {
Env::Default().GetTelemetryProvider().SetLanguageProjection(OrtLanguageProjection::ORT_PROJECTION_PYTHON);
OrtEnv::LoggingManagerConstructionInfo lm_info{nullptr, nullptr, ORT_LOGGING_LEVEL_WARNING, "Default"};

// Allow the default logging severity to be configured via ORT_LOGGING_LEVEL before the first import.
// Valid integer values: 0 (Verbose), 1 (Info), 2 (Warning), 3 (Error), 4 (Fatal).
OrtLoggingLevel logging_level = ORT_LOGGING_LEVEL_WARNING;
const std::string logging_env = Env::Default().GetEnvironmentVar("ORT_LOGGING_LEVEL");
if (!logging_env.empty()) {
char* end = nullptr;
long level = std::strtol(logging_env.c_str(), &end, 10);
if (end != logging_env.c_str() && *end == '\0' &&
level >= ORT_LOGGING_LEVEL_VERBOSE && level <= ORT_LOGGING_LEVEL_FATAL) {
logging_level = static_cast<OrtLoggingLevel>(level);
}
}

OrtEnv::LoggingManagerConstructionInfo lm_info{nullptr, nullptr, logging_level, "Default"};
Status status;
OrtEnvPtr ort_env = OrtEnv::GetOrCreateInstance(lm_info, status, use_global_tp ? &global_tp_options : nullptr);
if (!status.IsOK()) return status;
Expand Down