Skip to content

Commit e61e1e5

Browse files
committed
LoggerContext: Log any exceptions occuring within the context
1 parent 5048dcc commit e61e1e5

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/sensai/util/logging.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,14 +353,19 @@ class LoggerContext(Generic[THandler], ABC):
353353

354354
def __init__(self, enabled=True):
355355
"""
356-
:param enabled: whether to actually perform any logging.
357-
This switch allows the with statement to be applied regardless of whether logging shall be enabled.
356+
:param enabled: whether to actually enable the context, applying the new log handler.
357+
This switch allows the with statement to be applied regardless of whether the context shall be enabled.
358358
"""
359359
self.enabled = enabled
360360
self._log_handler = None
361361

362362
@abstractmethod
363363
def _create_log_handler(self) -> THandler:
364+
"""
365+
Creates and registers/enables the log handler to be used within the context.
366+
367+
:return: the handler
368+
"""
364369
pass
365370

366371
def __enter__(self) -> Optional[THandler]:
@@ -369,7 +374,9 @@ def __enter__(self) -> Optional[THandler]:
369374
return self._log_handler
370375

371376
def __exit__(self, exc_type, exc_value, traceback):
372-
if self._log_handler is not None:
377+
if self.enabled:
378+
if exc_type is not None:
379+
log.error(f"Exception within {self.__class__.__name__}", exc_info=exc_value)
373380
remove_log_handler(self._log_handler)
374381

375382

0 commit comments

Comments
 (0)