Saltar al contenido

Referencia para hub_sdk/helpers/logger.py

Nota

Este archivo est谩 disponible en https://github.com/ultralytics/hub-sdk/blob/main/hub_sdk/helpers/logger .py. Si detectas alg煤n problema, por favor, ayuda a solucionarlo contribuyendo con una Pull Request 馃洜锔. 隆Gracias 馃檹!



hub_sdk.helpers.logger.Logger

Representa la configuraci贸n de un registrador para gestionar los mensajes de registro.

Atributos:

Nombre Tipo Descripci贸n
logger_name str

Nombre del registrador. Por defecto es el nombre del m贸dulo llamante.

log_format str

Formato de los mensajes de registro. Por defecto es el valor de la variable de entorno 'LOGGER_FORMAT' o '%(asctime)s - %(name)s - %(levelname)s - %(message)s'.

log_level str

Nivel de registro para el registrador. Por defecto es el valor de la variable de entorno 'LOGGER_LEVEL' o 'INFO'.

logger Logger

La instancia del registrador configurada.

C贸digo fuente en hub_sdk/helpers/logger.py
class Logger:
    """
    Represents a logger configuration for handling log messages.

    Attributes:
        logger_name (str): Name of the logger. Defaults to the name of the calling module.
        log_format (str): Format for log messages. Defaults to the value of 'LOGGER_FORMAT'
                         environment variable or '%(asctime)s - %(name)s - %(levelname)s - %(message)s'.
        log_level (str): Log level for the logger. Defaults to the value of 'LOGGER_LEVEL'
                        environment variable or 'INFO'.
        logger (logging.Logger): The configured logger instance.
    """

    def __init__(self, logger_name=None, log_format=None, log_level=None):
        """
        Initialize a Logger instance.

        Args:
            logger_name (str): Name of the logger. If not provided, defaults to the root logger.
            log_format (str): Format for log messages. Defaults to the value of 'LOGGER_FORMAT'
                             environment variable or '%(asctime)s - %(levelname)s - %(message)s'.
            log_level (str): Log level for the logger. Defaults to the value of 'LOGGER_LEVEL'
                            environment variable or 'INFO'.
        """
        self.log_format = log_format or os.environ.get(
            "LOGGER_FORMAT", "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
        )
        self.log_level = log_level or os.environ.get("LOGGER_LEVEL", "INFO")
        self.logger_name = logger_name or __name__

        self.logger = self._configure_logger()

    def _configure_logger(self) -> logging.Logger:
        """
        Configure the logger with the provided settings.

        Returns:
            (logging.Logger): A configured logger instance.
        """
        logger = logging.getLogger(self.logger_name)
        logger.setLevel(self.log_level)

        formatter = logging.Formatter(self.log_format)

        handler = logging.StreamHandler()
        handler.setFormatter(formatter)

        logger.addHandler(handler)
        return logger

    def get_logger(self) -> logging.Logger:
        """
        Get the configured logger instance.

        Returns:
            (logging.Logger): The configured logger instance.
        """
        return self.logger

__init__(logger_name=None, log_format=None, log_level=None)

Inicializa una instancia de Logger.

Par谩metros:

Nombre Tipo Descripci贸n Por defecto
logger_name str

Nombre del registrador. Si no se proporciona, por defecto es el registrador ra铆z.

None
log_format str

Formato de los mensajes de registro. Por defecto es el valor de la variable de entorno 'LOGGER_FORMAT' o '%(asctime)s - %(levelname)s - %(message)s'.

None
log_level str

Nivel de registro para el registrador. Por defecto es el valor de la variable de entorno 'LOGGER_LEVEL' o 'INFO'.

None
C贸digo fuente en hub_sdk/helpers/logger.py
def __init__(self, logger_name=None, log_format=None, log_level=None):
    """
    Initialize a Logger instance.

    Args:
        logger_name (str): Name of the logger. If not provided, defaults to the root logger.
        log_format (str): Format for log messages. Defaults to the value of 'LOGGER_FORMAT'
                         environment variable or '%(asctime)s - %(levelname)s - %(message)s'.
        log_level (str): Log level for the logger. Defaults to the value of 'LOGGER_LEVEL'
                        environment variable or 'INFO'.
    """
    self.log_format = log_format or os.environ.get(
        "LOGGER_FORMAT", "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
    )
    self.log_level = log_level or os.environ.get("LOGGER_LEVEL", "INFO")
    self.logger_name = logger_name or __name__

    self.logger = self._configure_logger()

get_logger()

Obt茅n la instancia de registrador configurada.

Devuelve:

Tipo Descripci贸n
Logger

La instancia del registrador configurada.

C贸digo fuente en hub_sdk/helpers/logger.py
def get_logger(self) -> logging.Logger:
    """
    Get the configured logger instance.

    Returns:
        (logging.Logger): The configured logger instance.
    """
    return self.logger