Zum Inhalt springen

Referenz fĂźr hub_sdk/helpers/logger.py

Hinweis

Diese Datei ist verfügbar unter https://github.com/ultralytics/ hub-sdk/blob/main/ hub_sdk/helpers/logger .py. Wenn du ein Problem entdeckst, hilf bitte mit, es zu beheben, indem du einen Pull Request 🛠️ einreichst. Vielen Dank 🙏!



hub_sdk.helpers.logger.Logger

Stellt eine Logger-Konfiguration fĂźr die Behandlung von Logmeldungen dar.

Attribute:

Name Typ Beschreibung
logger_name str

Name des Loggers. Standardmäßig wird der Name des aufrufenden Moduls verwendet.

log_format str

Format für Logmeldungen. Standardmäßig wird der Wert der 'LOGGER_FORMAT' Umgebungsvariable oder '%(asctime)s - %(name)s - %(levelname)s - %(message)s'.

log_level str

Loglevel für den Logger. Standardmäßig wird der Wert der 'LOGGER_LEVEL' Umgebungsvariable oder 'INFO'.

logger Logger

Die konfigurierte Logger-Instanz.

Quellcode in 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)

Initialisiere eine Logger-Instanz.

Parameter:

Name Typ Beschreibung Standard
logger_name str

Name des Loggers. Wenn er nicht angegeben wird, wird standardmäßig der Root-Logger verwendet.

None
log_format str

Format für Logmeldungen. Standardmäßig wird der Wert der 'LOGGER_FORMAT' Umgebungsvariable oder '%(asctime)s - %(levelname)s - %(message)s'.

None
log_level str

Loglevel für den Logger. Standardmäßig wird der Wert der 'LOGGER_LEVEL' Umgebungsvariable oder 'INFO'.

None
Quellcode in 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()

Hole die konfigurierte Logger-Instanz.

Retouren:

Typ Beschreibung
Logger

Die konfigurierte Logger-Instanz.

Quellcode in 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