microsoft / microsoft/onnxruntime

[Feature Request] Python API support for custom ONNX Runtime logging callback

Open
#28,437 1 comment 2 reactions 2 assignees Claimed by @tianleiwu View on GitHub
feature request
Dominant language
C++
Stars
21.9k
Forks
4.2k
Avg merge
4d 11h
Merged PRs (30d)
184

Description

### Describe the feature request

### Summary

I would like to request support in the Python API for registering a custom logging function/callback for ONNX Runtime, similar to the C++ `Ort::Env` custom logger support.

### Problem

Currently the Python binding creates a global runtime environment at import time, and the logging callback is [hardcoded to `nullptr`](onnxruntime/python/onnxruntime_pybind_module.cc):

```cpp
static Status CreateOrtEnv() {
Env::Default().GetTelemetryProvider().SetLanguageProjection(OrtLanguageProjection::ORT_PROJECTION_PYTHON);
OrtEnv::LoggingManagerConstructionInfo lm_info{nullptr, nullptr, ORT_LOGGING_LEVEL_WARNING, "Default"};
Status status;
ort_env = OrtEnv::GetOrCreateInstance(lm_info, status, use_global_tp ? &global_tp_options : nullptr).release();
...
```

Because of this, Python users cannot provide a custom logging function to receive ORT log messages (for example, to write logs to a file or integrate with a custom Python logger).

### Proposed behavior

Expose a Python API that allows registration of a custom logging callback before the global runtime environment is created, for example:

- a module-level `set_logger_callback(...)`
- a `SessionOptions` or global logger registration API
- a Python callable that is forwarded into `OrtEnv::LoggingManagerConstructionInfo`

Would you be open for this feature?

### Describe scenario use case

The C++ API already supports custom logging via `OrtApi::CreateEnvWithCustomLogger()` / `Ort::Env(OrtLoggingFunction, ...)`. Python should expose a similar capability so that ORT logging behavior can be controlled from Python applications. To get ORT logs into a file, it is currently necessary to mangle with system file descriptors which is not a sustainable solution.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.