agent0.hyperlogs.logs

Utility functions for logging.

Attributes

DEFAULT_LOG_LEVEL

DEFAULT_LOG_FORMATTER

DEFAULT_LOG_DATETIME

DEFAULT_LOG_MAXBYTES

Functions

setup_logging(→ None)

Set up basic logging with default settings, customized by inputs.

close_logging(→ None)

Close logging and remove handlers for the test.

prepare_log_path(→ tuple[str, str])

Split filename into path and name. Postpend ".log" extension if necessary. Make dir if necessary.

create_formatter(→ logging.Formatter)

Create Formatter object from a log format string, applying default settings if log_format_string is None.

create_log_level(→ int)

Create log level, applying default hyperlogs.DEFAULT_LOG_LEVEL if log_level is None.

create_max_bytes(→ int)

Create max bytes, applying default hyperlogs.DEFAULT_LOG_MAXBYTES if max_bytes is None.

get_root_logger(→ logging.Logger)

Retrieve root logger, isolated from other loggers (like pytest).

add_stdout_handler(→ None)

Add a stdout handler to the root logger.

add_file_handler(log_filename[, logger, ...])

Add a file handler to the root logger.

remove_handlers(logger)

Remove all handlers from the logger.

create_file_handler(→ logging.Handler)

Create a file handler for the given log file.

Module Contents

agent0.hyperlogs.logs.DEFAULT_LOG_LEVEL = 20
agent0.hyperlogs.logs.DEFAULT_LOG_FORMATTER = Multiline-String
Show Value
"""
%(asctime)s: %(levelname)s: %(filename)s:%(lineno)s::%(module)s::%(funcName)s:
%(message)s"""
agent0.hyperlogs.logs.DEFAULT_LOG_DATETIME = '%y-%m-%d %H:%M:%S'
agent0.hyperlogs.logs.DEFAULT_LOG_MAXBYTES = 0
agent0.hyperlogs.logs.setup_logging(log_filename: str | None = None, max_bytes: int | None = None, log_level: int | None = None, delete_previous_logs: bool = False, log_stdout: bool = True, log_format_string: str | None = None, keep_previous_handlers: bool = False) None

Set up basic logging with default settings, customized by inputs.

This function should only be run once, as it implements the default settings. To customize logging behavior, only add_stdout_handler or add_file_handler should be run.

The log_filename can be a path to the log file. If log_filename is not provided, log_file_and_stdout can be set to True to log to both file and standard output (console). If neither log_filename nor log_file_and_stdout is specified, the log messages will be sent to standard output only.

Parameters:
  • log_filename (str, optional) – Path and name of the log file.

  • max_bytes (int, optional) – Maximum size of the log file in bytes. Defaults to hyperlogs.DEFAULT_LOG_MAXBYTES.

  • log_level (int, optional) – Log level to track. Defaults to hyperlogs.DEFAULT_LOG_LEVEL.

  • delete_previous_logs (bool, optional) – Whether to delete previous log file if it exists. Defaults to False.

  • log_stdout (bool, optional) – Whether to log to standard output. Defaults to True.

  • log_format_string (str, optional) – Log formatter object. Defaults to None.

  • keep_previous_handlers (bool, optional) – Whether to keep previous handlers. Defaults to False.

  • todo:: (..) –

    • Test the various optional input combinations

agent0.hyperlogs.logs.close_logging(delete_logs=True) None

Close logging and remove handlers for the test.

Parameters:

delete_logs (bool) – Whether to delete logs before closing logging.

agent0.hyperlogs.logs.prepare_log_path(log_filename: str) tuple[str, str]

Split filename into path and name. Postpend “.log” extension if necessary. Make dir if necessary.

Parameters:

log_filename (str) – Path and name of the log file.

Returns:

tuple[log_dir

log_dir: str

Path of the log file.

log_name: str

Name of the log file.

Return type:

str, log_name: str]

agent0.hyperlogs.logs.create_formatter(log_format_string: str | None = None) logging.Formatter

Create Formatter object from a log format string, applying default settings if log_format_string is None.

Default settings are defined in hyperlogs.DEFAULT_LOG_FORMATTER and hyperlogs.DEFAULT_LOG_DATETIME.

Parameters:

log_format_string (str, optional) – Logging format described in string format.

Returns:

Logging format as a Formatter object, after defaults are applied.

Return type:

logging.Formatter

agent0.hyperlogs.logs.create_log_level(log_level: int | None = None) int

Create log level, applying default hyperlogs.DEFAULT_LOG_LEVEL if log_level is None.

Parameters:

log_level (int, optional) – Logging level to be created. Defaults to hyperlogs.DEFAULT_LOG_LEVEL.

Returns:

Logging level that was created, after defaults are applied.

Return type:

int

agent0.hyperlogs.logs.create_max_bytes(max_bytes: int | None = None) int

Create max bytes, applying default hyperlogs.DEFAULT_LOG_MAXBYTES if max_bytes is None.

Parameters:

max_bytes (int, optional) – Maximum size of the log file in bytes. Defaults to hyperlogs.DEFAULT_LOG_MAXBYTES.

Returns:

Maximum size of the log file in bytes, after defaults are applied.

Return type:

int

agent0.hyperlogs.logs.get_root_logger(root_logger: logging.Logger | None = None) logging.Logger

Retrieve root logger, isolated from other loggers (like pytest).

Parameters:

root_logger (logging.Logger, optional) – Logger to which to add the handler. Defaults to logging.getLogger().

Returns:

Root logger.

Return type:

logging.Logger

agent0.hyperlogs.logs.add_stdout_handler(logger: logging.Logger | None = None, log_format_string: str | None = None, log_level: int | None = logging.INFO, keep_previous_handlers: bool = True) None

Add a stdout handler to the root logger.

Parameters:
  • logger (logging.Logger, optional) – Logger to which to add the handler. Defaults to get_root_logger().

  • log_format_string (str, optional) – Logging format described in string format. Defaults to hyperlogs.DEFAULT_LOG_FORMAT and hyperlogs.DEFAULT_LOG_DATETIME.

  • log_level (int, optional) – Log level to track. Defaults to hyperlogs.DEFAULT_LOG_LEVEL.

  • keep_previous_handlers (bool, optional) – Whether to keep previous handlers. Defaults to True.

agent0.hyperlogs.logs.add_file_handler(log_filename: str, logger: logging.Logger | None = None, delete_previous_logs: bool = False, log_format_string: str | None = None, log_level: int | None = logging.INFO, max_bytes=None, keep_previous_handlers: bool = True)

Add a file handler to the root logger.

Parameters:
  • log_filename (str) – Path and name of the log file.

  • logger (logging.Logger, optional) – Logger to which to add the handler. Defaults to get_root_logger().

  • delete_previous_logs (bool, optional) – Whether to delete previous log file if it exists. Defaults to False.

  • log_format_string (str, optional) – Logging format described in string format.

  • log_level (int, optional) – Log level to track. Defaults to hyperlogs.DEFAULT_LOG_LEVEL.

  • max_bytes (int, optional) – Maximum size of the log file in bytes. Defaults to hyperlogs.DEFAULT_LOG_MAXBYTES.

  • keep_previous_handlers (bool, optional) – Whether to keep previous handlers. Defaults to True.

agent0.hyperlogs.logs.remove_handlers(logger: logging.Logger)

Remove all handlers from the logger.

Parameters:

logger (logging.Logger) – Logger from which to remove handlers.

agent0.hyperlogs.logs.create_file_handler(log_dir: str, log_name: str, log_formatter: logging.Formatter, max_bytes: int, log_level: int) logging.Handler

Create a file handler for the given log file.

Parameters:
  • log_dir (str) – Directory in which to log the file.

  • log_name (str) – File name in which to log.

  • log_formatter (logging.Formatter) – Logging format as a Formatter object.

  • max_bytes (int) – Maximum size of the log file in bytes. Defaults to hyperlogs.DEFAULT_LOG_MAXBYTES.

  • log_level (int) – Log level.

Returns:

The logging handler.

Return type:

logging.Handler