Table of Contents

Class StreamLogHandler

Namespace
OptunaSharp.Logging
Assembly
OptunaSharp.dll

The default handler: writes [I 2021-10-31 05:35:17,232] message lines to stderr, mirroring Optuna's create_default_formatter() (sans colors).

public sealed class StreamLogHandler : ILogHandler
Inheritance
StreamLogHandler
Implements
Inherited Members

Remarks

Like Python's logging.StreamHandler, the parameterless constructor binds to the standard error stream current at construction (Error, Python's sys.stderr), not at each write. Footgun: if the library's default handler is created while Error is temporarily redirected (a test capturing stderr, a host swapping it), it keeps writing to that redirect afterwards. If the redirect is later disposed, writes fail — and, exactly as in Python, the failure is reported on the current stderr ("--- Logging error ---") instead of propagating, so a broken log stream never fails the optimization that was logging.

Threads. Each record is one write of its line and, when it has one, its exception, followed by a flush, as Python's StreamHandler.emit does since issue 35046; OptunaSharp emits one record at a time per handler (ILogHandler). So records from parallel trials never interleave, even over a TextWriter that is not synchronized, as long as only this handler writes to it. A writer shared with other code still needs Synchronized(TextWriter).

Constructors

StreamLogHandler()

Creates the Optuna-style text handler over the standard error stream as it is now (Python's StreamHandler() capturing sys.stderr); a later SetError(TextWriter) does not move it.

public StreamLogHandler()

StreamLogHandler(TextWriter)

Creates an Optuna-style text handler over a supplied writer.

public StreamLogHandler(TextWriter writer)

Parameters

writer TextWriter

Caller-owned text writer; the handler does not dispose it.

Methods

Emit(in LogRecord)

Writes the formatted event and optional exception to the configured writer in one write, then flushes it. Never throws: a write failure (a disposed or broken writer) is reported on the current standard error the way Python's logging.Handler.handleError reports it, and the record is dropped.

public void Emit(in LogRecord record)

Parameters

record LogRecord

Log record to deliver to the handler.