logging#


Partial copy of sensai.util.logging

class FileLoggerContext(path: str, enabled: bool = True)[source]#
add_file_logger(path: str, register_atexit: bool = True) FileHandler[source]#
add_memory_logger() None[source]#

Enables in-memory logging (if it is not already enabled), i.e. all log statements are written to a memory buffer and can later be read via function get_memory_log().

configure(format: str = '%(levelname)-5s %(asctime)-15s %(name)s:%(funcName)s - %(message)s', level: int = 10) None[source]#

Configures logging to stdout with the given format and log level, also configuring the default log levels of some overly verbose libraries as well as some pandas output options.

Parameters:
  • format – the log format

  • level – the minimum log level

datetime_tag() str[source]#
Returns:

a string tag for use in log file names which contains the current date and time (compact but readable)

get_memory_log() Any[source]#
Returns:

the in-memory log (provided that add_memory_logger was called beforehand)

is_log_handler_active(handler: Handler) bool[source]#

Checks whether the given handler is active.

Parameters:

handler – a log handler

Returns:

True if the handler is active, False otherwise

remove_log_handler(handler: Handler) None[source]#
remove_log_handlers() None[source]#

Removes all current log handlers.

run_cli(main_fn: Callable[[...], T], format: str = '%(levelname)-5s %(asctime)-15s %(name)s:%(funcName)s - %(message)s', level: int = 10) T | None[source]#

Configures logging with the given parameters and runs the given main function as a CLI using jsonargparse (which is configured to also parse attribute docstrings, such that dataclasses can be used as function arguments). Using this function requires that jsonargparse and docstring_parser be available. Like run_main, two additional log messages will be logged (at the beginning and end of the execution), and it is ensured that all exceptions will be logged.

Parameters:
  • main_fn – the function to be executed

  • format – the log message format

  • level – the minimum log level

Returns:

the result of main_fn

run_main(main_fn: Callable[[], T], format: str = '%(levelname)-5s %(asctime)-15s %(name)s:%(funcName)s - %(message)s', level: int = 10) T | None[source]#

Configures logging with the given parameters, ensuring that any exceptions that occur during the execution of the given function are logged. Logs two additional messages, one before the execution of the function, and one upon its completion.

Parameters:
  • main_fn – the function to be executed

  • format – the log message format

  • level – the minimum log level

Returns:

the result of main_fn

set_numerical_fields_to_precision(data: dict[str, Any], precision: int = 3) dict[str, Any][source]#

Returns a copy of the given dictionary with all numerical values rounded to the given precision.

Note: does not recurse into nested dictionaries.

Parameters:
  • data – a dictionary

  • precision – the precision to be used