diff --git a/README.md b/README.md index 6d3558f..87ab446 100644 --- a/README.md +++ b/README.md @@ -53,14 +53,14 @@ class LogHandler(LogHandlerBase): # self.settings. # You also have access to self.common_settings here, which are logging settings supplied by the caller in the form of OutputSettingsLoggerInterface. # noqa: E501 # See https://github.com/snakemake/snakemake-interface-logger-plugins/blob/main/src/snakemake_interface_logger_plugins/settings.py for more details # noqa: E501 - + # access settings attributes - self.settings + self.settings self.common_settings # Here you can override logging.Handler methods to customize logging behavior. - # For example, you can override the emit() method to control how log records - # are processed and output. See the Python logging documentation for details: + # Only an implementation of the emit() method is required. See the Python logging + # documentation for details: # https://docs.python.org/3/library/logging.html#handler-objects # LogRecords from Snakemake carry contextual information in the record's attributes @@ -68,6 +68,12 @@ class LogHandler(LogHandlerBase): # See https://github.com/snakemake/snakemake-interface-logger-plugins/blob/2ab84cb31f0b92cf0b7ee3026e15d1209729d197/src/snakemake_interface_logger_plugins/common.py#L33 # noqa: E501 # For examples on parsing LogRecords, see https://github.com/cademirch/snakemake-logger-plugin-snkmt/blob/main/src/snakemake_logger_plugin_snkmt/parsers.py # noqa: E501 + def emit(self, record): + # Actually emit the record. Typically this will call self.format(record) to + # convert the record to a formatted string. The result could then be written to + # a stream or file. + ... + @property def writes_to_stream(self) -> bool: # Whether this plugin writes to stderr/stdout. @@ -147,7 +153,7 @@ class LogHandler(LogHandlerBase): def __post_init__(self) -> None: super().__post_init__() self.console = Console() - + def emit(self, record): # Access event type from record if hasattr(record, 'event') and record.event == LogEvent.JOB_ERROR: @@ -155,7 +161,7 @@ class LogHandler(LogHandlerBase): if hasattr(record, 'name') and record.name in ['rule1', 'rule2']: logfile = record.log[0] if hasattr(record, 'log') else None output = record.output[0] if hasattr(record, 'output') else "unknown" - + # Use rich console for pretty printing self.console.print(f"[red]Error in {output}. See {logfile}[/red]") if logfile: diff --git a/src/snakemake_interface_logger_plugins/base.py b/src/snakemake_interface_logger_plugins/base.py index dae2cca..3d3c6ed 100644 --- a/src/snakemake_interface_logger_plugins/base.py +++ b/src/snakemake_interface_logger_plugins/base.py @@ -9,7 +9,7 @@ OutputSettingsLoggerInterface, ) from abc import ABC, abstractmethod -from logging import Handler +from logging import Handler, LogRecord class LogHandlerBase(ABC, Handler): @@ -28,6 +28,13 @@ def __init__( def __post_init__(self) -> None: pass + @abstractmethod + def emit(self, record: LogRecord) -> None: + """Actually log the given record. + + This is called after the record has passed the handler's installed filter. + """ + @property @abstractmethod def writes_to_stream(self) -> bool: