From 890f8939fcd877eb44345192431bd7eb1145ac9b Mon Sep 17 00:00:00 2001 From: Simon-Martin Schroeder Date: Tue, 24 May 2022 16:33:13 +0200 Subject: [PATCH] feat(TrialLogHandler): Hook into Python's logging --- experitur/core/log_handler.py | 45 +++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 experitur/core/log_handler.py diff --git a/experitur/core/log_handler.py b/experitur/core/log_handler.py new file mode 100644 index 0000000..515b93e --- /dev/null +++ b/experitur/core/log_handler.py @@ -0,0 +1,45 @@ +import logging + +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from experitur.core.trial import Trial + + +class TrialLogHandler(logging.Handler): + """ + Hook into Python's logging to save entries in the trial log. + + Example: + with TrialLogHandler(trial): + ... + """ + + def __init__(self, trial: "Trial"): + super().__init__() + self.trial = trial + + def emit(self, record): + """ + Emit a record. + + If a formatter is specified, it is used to format the record. + The record is then written to the stream with a trailing newline. If + exception information is present, it is formatted using + traceback.print_exception and appended to the stream. If the stream + has an 'encoding' attribute, it is used to determine how to do the + output to the stream. + """ + try: + msg = self.format(record) + self.trial.log_msg(msg) + except RecursionError: + raise + except Exception: # pylint: disable=broad-except + self.handleError(record) + + def __enter__(self): + logging.root.addHandler(self) + + def __exit__(self, *_, **__): + logging.root.removeHandler(self)