agent0.hyperlogs

Logging setup and defaults

Submodules

Attributes

DEFAULT_LOG_DATETIME

DEFAULT_LOG_FORMATTER

DEFAULT_LOG_LEVEL

DEFAULT_LOG_MAXBYTES

Classes

ExtendedJSONEncoder

Custom encoder for JSON string dumps.

Functions

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

Add a file handler to the root logger.

add_stdout_handler(→ None)

Add a stdout handler to the root logger.

close_logging(→ None)

Close logging and remove handlers for the test.

get_root_logger(→ logging.Logger)

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

setup_logging(→ None)

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

Package Contents

class agent0.hyperlogs.ExtendedJSONEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)

Bases: json.JSONEncoder

Custom encoder for JSON string dumps.

default(o: Any) Any

Override default behavior.

Parameters:

o (Any) – The object to be converted to JSON.

Returns:

The corresponding object ready to be serialized to JSON.

Return type:

Any

agent0.hyperlogs.DEFAULT_LOG_DATETIME = '%y-%m-%d %H:%M:%S'
agent0.hyperlogs.DEFAULT_LOG_FORMATTER = Multiline-String
Show Value
"""
%(asctime)s: %(levelname)s: %(filename)s:%(lineno)s::%(module)s::%(funcName)s:
%(message)s"""
agent0.hyperlogs.DEFAULT_LOG_LEVEL = 20
agent0.hyperlogs.DEFAULT_LOG_MAXBYTES = 0
agent0.hyperlogs.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.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.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.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.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