Skip to content

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

Description

@JanSellner

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:

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.

Metadata

Metadata

Labels

feature requestrequest for unsupported feature or enhancement

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions