Skip to content

🟑 unaiverse.networking.p2p.mylogger

What this module does 🟑

Logging utility configuring a per-module timed-rotating file logger for the P2P networking layer.

mylogger

β–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–‘β–‘β–ˆβ–ˆβ–ˆ β–‘β–‘β–ˆβ–ˆβ–ˆ β–‘β–‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–‘β–‘β–ˆβ–ˆβ–ˆ β–‘β–‘β–ˆβ–ˆβ–ˆ β–‘β–‘β–ˆβ–ˆβ–ˆ β–‘β–‘β–ˆβ–ˆβ–ˆ β–‘β–‘β–ˆβ–ˆβ–ˆβ–‘β–‘β–‘β–‘β–‘β–ˆβ–‘β–‘β–ˆβ–ˆβ–ˆβ–‘β–‘β–‘β–‘β–‘β–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–‘β–‘β–‘β–‘β–‘β–ˆβ–ˆβ–ˆβ–‘β–‘β–ˆβ–ˆβ–ˆβ–‘β–‘β–‘β–‘β–‘β–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆβ–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–ˆ β–‘ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–‘β–‘ β–‘β–ˆβ–ˆβ–ˆ β–ˆ β–‘ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆβ–‘β–‘β–ˆβ–ˆβ–ˆβ–‘β–ˆβ–ˆβ–ˆ β–‘β–‘β–‘β–‘β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–‘β–‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ
β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–‘β–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆβ–‘β–‘β–ˆ β–‘β–ˆβ–ˆβ–ˆβ–‘β–‘β–‘β–‘β–‘β–ˆβ–ˆβ–ˆ β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆβ–‘β–‘β–ˆ
β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–‘β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–‘β–‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‘ β–‘β–ˆβ–ˆβ–ˆ β–‘ β–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘β–ˆβ–ˆβ–ˆ β–‘ β–ˆ β–‘β–‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–‘β–‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‘β–‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–‘β–‘β–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–‘β–‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘ β–‘β–‘β–‘β–‘β–‘ β–‘β–‘β–‘β–‘β–‘ β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘ β–‘β–‘β–‘β–‘β–‘ β–‘β–‘β–‘ β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘ β–‘β–‘β–‘β–‘β–‘ β–‘β–‘β–‘β–‘β–‘ β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘ β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘ A Collectionless AI Project (https://collectionless.ai) Registration/Login: https://unaiverse.io Code Repositories: https://github.com/collectionlessai/ Main Developers: Stefano Melacci (Project Leader), Christian Di Maio, Tommaso Guidi

LOG_FOLDER module-attribute

LOG_FOLDER = 'unaiverse/networking/p2p/logs'

setup_logger

setup_logger(module_name: str, when: str = 'midnight', backup_count: int = 7) -> Logger

Return a configured logging.Logger for the given module name.

Creates or retrieves the logging.Logger identified by module_name and attaches a TimedRotatingFileHandler that writes UTF-8 log records under LOG_FOLDER. The log file is named <module_name>.log and rotated at the interval specified by when. Up to backup_count rotated files are retained before the oldest is deleted.

Log records use the format::

YYYY-MM-DD HH:MM:SS [LEVEL] filename:lineno - message

with UTC timestamps. Propagation to the root logger is disabled so that records from this logger do not appear twice when a root handler is configured.

Note

The internal do_log flag is currently hardcoded to False. When it is False, any existing handlers are cleared and a NullHandler is attached instead, so no file I/O occurs and LOG_FOLDER is never created. This behaviour is intentional for production use; enabling file logging requires setting do_log = True in the source.

If the logger for module_name already has handlers attached (i.e., it was previously configured), no duplicate handler is added.

Parameters:

Name Type Description Default
module_name str

The name used to identify the logger and to derive the log file name (<LOG_FOLDER>/<module_name>.log).

required
when str

Rotation interval passed directly to TimedRotatingFileHandler. Accepted values include 'S' (seconds), 'M' (minutes), 'H' (hours), 'D' (days), and 'midnight'. Defaults to 'midnight'.

'midnight'
backup_count int

Number of rotated log files to keep before the oldest is deleted. Defaults to 7.

7

Returns:

Type Description
Logger

A logging.Logger instance set to DEBUG level, configured either with

Logger

a TimedRotatingFileHandler (when logging is enabled) or a

Logger

NullHandler (when logging is disabled).

Source code in unaiverse/networking/p2p/mylogger.py
def setup_logger(module_name: str, when: str = 'midnight', backup_count: int = 7) -> logging.Logger:
    """Return a configured ``logging.Logger`` for the given module name.

    Creates or retrieves the ``logging.Logger`` identified by ``module_name`` and
    attaches a ``TimedRotatingFileHandler`` that writes UTF-8 log records under
    ``LOG_FOLDER``. The log file is named ``<module_name>.log`` and rotated at the
    interval specified by ``when``. Up to ``backup_count`` rotated files are retained
    before the oldest is deleted.

    Log records use the format::

        YYYY-MM-DD HH:MM:SS [LEVEL] filename:lineno - message

    with UTC timestamps. Propagation to the root logger is disabled so that records
    from this logger do not appear twice when a root handler is configured.

    Note:
        The internal ``do_log`` flag is currently hardcoded to ``False``. When it is
        ``False``, any existing handlers are cleared and a ``NullHandler`` is attached
        instead, so no file I/O occurs and ``LOG_FOLDER`` is never created. This
        behaviour is intentional for production use; enabling file logging requires
        setting ``do_log = True`` in the source.

        If the logger for ``module_name`` already has handlers attached (i.e., it was
        previously configured), no duplicate handler is added.

    Args:
        module_name: The name used to identify the logger and to derive the log file
            name (``<LOG_FOLDER>/<module_name>.log``).
        when: Rotation interval passed directly to ``TimedRotatingFileHandler``.
            Accepted values include ``'S'`` (seconds), ``'M'`` (minutes), ``'H'``
            (hours), ``'D'`` (days), and ``'midnight'``. Defaults to ``'midnight'``.
        backup_count: Number of rotated log files to keep before the oldest is
            deleted. Defaults to ``7``.

    Returns:
        A ``logging.Logger`` instance set to ``DEBUG`` level, configured either with
        a ``TimedRotatingFileHandler`` (when logging is enabled) or a
        ``NullHandler`` (when logging is disabled).
    """
    do_log = False  # Stefano

    # Ensure log folder exists
    if do_log:
        os.makedirs(LOG_FOLDER, exist_ok=True)

    # Log file base path (without date suffix)
    log_base_filename = os.path.join(LOG_FOLDER, f"{module_name}.log")

    # Create logger
    logger = logging.getLogger(module_name)
    logger.setLevel(logging.DEBUG)

    if do_log and not logger.handlers:

        # Create rotating file handler
        handler = TimedRotatingFileHandler(
            log_base_filename,
            when=when,
            interval=1,
            backupCount=backup_count,
            encoding='utf-8',
            utc=True  # Set to False if you want local time
        )

        formatter = logging.Formatter(
            fmt="%(asctime)s [%(levelname)s] %(filename)s:%(lineno)d - %(message)s",
            datefmt="%Y-%m-%d %H:%M:%S"
        )
        handler.setFormatter(formatter)
        logger.addHandler(handler)

        # Avoid logging to root handlers
        logger.propagate = False

    if not do_log:
        logger.handlers.clear()
        logger.addHandler(logging.NullHandler())

    return logger