Skip to content

Add logging

Sebastian Müller requested to merge add_logging into main

This MR adds logging capabilities to FINAM.

A base logger will be associated with the Composition:

comp = Composition(
    modules,
    logger_name="FINAM",
    print_log=True,
    log_file=None,
    log_level=logging.INFO,
)

There you can define a name for the base logger, state if it should be printed, define a log-file and set the log-level.

A new abstract base class Loggable was added to add logging capabilities to components.

All loggable components will have a sub-logger connected to a possible base logger (indicated by uses_base_logger_name) and all loggable inputs and outputs will have sub-logger connected to the component logger where they belong to. These loggers can be accessed by these two properties:

  • comp.logger_name: The logger name to get the logger with logging.getLogger(logger_name)
  • comp.logger: shortcut for logging.getLogger(self.logger_name)

If any component has uses_base_logger_name=True, it needs to provide a base_logger_name attribute.

All raised Errors in FINAM were redirected to be logged as an exception.

All calls to methods of components are logged as debug.

In addition two context managers were added to redirect stdout and stderr to a logger in finam.tools.log_helpers:

  • LogStdOutStdErr: Context manager to redirect stdout and stderr to a logger.
  • LogCStdOutStdErr: Context manager to redirect low-level C stdout and stderr to a logger.

These context managers can be used by model wrappers to redirect the model output to a logger like:

with LogCStdOutStdErr(self.logger):
    mp.run.do_time_step()

Model wrappers can now log all things that should/could be logged (like the good old print messages for debugging) with:

self.logger.debug("I am doing this and that now")
Edited by Sebastian Müller

Merge request reports

Loading