agent0.hyperlogs.logs ===================== .. py:module:: agent0.hyperlogs.logs .. autoapi-nested-parse:: Utility functions for logging. .. !! processed by numpydoc !! Attributes ---------- .. autoapisummary:: agent0.hyperlogs.logs.DEFAULT_LOG_LEVEL agent0.hyperlogs.logs.DEFAULT_LOG_FORMATTER agent0.hyperlogs.logs.DEFAULT_LOG_DATETIME agent0.hyperlogs.logs.DEFAULT_LOG_MAXBYTES Functions --------- .. autoapisummary:: agent0.hyperlogs.logs.setup_logging agent0.hyperlogs.logs.close_logging agent0.hyperlogs.logs.prepare_log_path agent0.hyperlogs.logs.create_formatter agent0.hyperlogs.logs.create_log_level agent0.hyperlogs.logs.create_max_bytes agent0.hyperlogs.logs.get_root_logger agent0.hyperlogs.logs.add_stdout_handler agent0.hyperlogs.logs.add_file_handler agent0.hyperlogs.logs.remove_handlers agent0.hyperlogs.logs.create_file_handler Module Contents --------------- .. py:data:: DEFAULT_LOG_LEVEL :value: 20 .. py:data:: DEFAULT_LOG_FORMATTER :value: Multiline-String .. raw:: html
Show Value .. code-block:: python """ %(asctime)s: %(levelname)s: %(filename)s:%(lineno)s::%(module)s::%(funcName)s: %(message)s""" .. raw:: html
.. py:data:: DEFAULT_LOG_DATETIME :value: '%y-%m-%d %H:%M:%S' .. py:data:: DEFAULT_LOG_MAXBYTES :value: 0 .. py:function:: 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. :param log_filename: Path and name of the log file. :type log_filename: str, optional :param max_bytes: Maximum size of the log file in bytes. Defaults to hyperlogs.DEFAULT_LOG_MAXBYTES. :type max_bytes: int, optional :param log_level: Log level to track. Defaults to hyperlogs.DEFAULT_LOG_LEVEL. :type log_level: int, optional :param delete_previous_logs: Whether to delete previous log file if it exists. Defaults to False. :type delete_previous_logs: bool, optional :param log_stdout: Whether to log to standard output. Defaults to True. :type log_stdout: bool, optional :param log_format_string: Log formatter object. Defaults to None. :type log_format_string: str, optional :param keep_previous_handlers: Whether to keep previous handlers. Defaults to False. :type keep_previous_handlers: bool, optional :param .. todo::: - Test the various optional input combinations .. !! processed by numpydoc !! .. py:function:: close_logging(delete_logs=True) -> None Close logging and remove handlers for the test. :param delete_logs: Whether to delete logs before closing logging. :type delete_logs: bool .. !! processed by numpydoc !! .. py:function:: prepare_log_path(log_filename: str) -> tuple[str, str] Split filename into path and name. Postpend ".log" extension if necessary. Make dir if necessary. :param log_filename: Path and name of the log file. :type log_filename: str :returns: **tuple[log_dir** -- log_dir: str Path of the log file. log_name: str Name of the log file. :rtype: str, log_name: str] .. !! processed by numpydoc !! .. py:function:: 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. :param log_format_string: Logging format described in string format. :type log_format_string: str, optional :returns: Logging format as a Formatter object, after defaults are applied. :rtype: logging.Formatter .. !! processed by numpydoc !! .. py:function:: create_log_level(log_level: int | None = None) -> int Create log level, applying default hyperlogs.DEFAULT_LOG_LEVEL if log_level is None. :param log_level: Logging level to be created. Defaults to hyperlogs.DEFAULT_LOG_LEVEL. :type log_level: int, optional :returns: Logging level that was created, after defaults are applied. :rtype: int .. !! processed by numpydoc !! .. py:function:: create_max_bytes(max_bytes: int | None = None) -> int Create max bytes, applying default hyperlogs.DEFAULT_LOG_MAXBYTES if max_bytes is None. :param max_bytes: Maximum size of the log file in bytes. Defaults to hyperlogs.DEFAULT_LOG_MAXBYTES. :type max_bytes: int, optional :returns: Maximum size of the log file in bytes, after defaults are applied. :rtype: int .. !! processed by numpydoc !! .. py:function:: get_root_logger(root_logger: logging.Logger | None = None) -> logging.Logger Retrieve root logger, isolated from other loggers (like pytest). :param root_logger: Logger to which to add the handler. Defaults to logging.getLogger(). :type root_logger: logging.Logger, optional :returns: Root logger. :rtype: logging.Logger .. !! processed by numpydoc !! .. py:function:: 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. :param logger: Logger to which to add the handler. Defaults to get_root_logger(). :type logger: logging.Logger, optional :param log_format_string: Logging format described in string format. Defaults to hyperlogs.DEFAULT_LOG_FORMAT and hyperlogs.DEFAULT_LOG_DATETIME. :type log_format_string: str, optional :param log_level: Log level to track. Defaults to hyperlogs.DEFAULT_LOG_LEVEL. :type log_level: int, optional :param keep_previous_handlers: Whether to keep previous handlers. Defaults to True. :type keep_previous_handlers: bool, optional .. !! processed by numpydoc !! .. py:function:: 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. :param log_filename: Path and name of the log file. :type log_filename: str :param logger: Logger to which to add the handler. Defaults to get_root_logger(). :type logger: logging.Logger, optional :param delete_previous_logs: Whether to delete previous log file if it exists. Defaults to False. :type delete_previous_logs: bool, optional :param log_format_string: Logging format described in string format. :type log_format_string: str, optional :param log_level: Log level to track. Defaults to hyperlogs.DEFAULT_LOG_LEVEL. :type log_level: int, optional :param max_bytes: Maximum size of the log file in bytes. Defaults to hyperlogs.DEFAULT_LOG_MAXBYTES. :type max_bytes: int, optional :param keep_previous_handlers: Whether to keep previous handlers. Defaults to True. :type keep_previous_handlers: bool, optional .. !! processed by numpydoc !! .. py:function:: remove_handlers(logger: logging.Logger) Remove all handlers from the logger. :param logger: Logger from which to remove handlers. :type logger: logging.Logger .. !! processed by numpydoc !! .. py:function:: 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. :param log_dir: Directory in which to log the file. :type log_dir: str :param log_name: File name in which to log. :type log_name: str :param log_formatter: Logging format as a Formatter object. :type log_formatter: logging.Formatter :param max_bytes: Maximum size of the log file in bytes. Defaults to hyperlogs.DEFAULT_LOG_MAXBYTES. :type max_bytes: int :param log_level: Log level. :type log_level: int :returns: The logging handler. :rtype: logging.Handler .. !! processed by numpydoc !!