Skip to content

harbor_cli.logs

Attributes

logger = logging.getLogger('harbor-cli') module-attribute

Classes

LogLevel

Bases: Enum

Enum for log levels.

Source code in harbor_cli/logs.py
class LogLevel(Enum):
    """Enum for log levels."""

    NOTSET = "NOTSET"
    DEBUG = "DEBUG"
    INFO = "INFO"
    WARN = "WARN"
    WARNING = "WARNING"
    ERROR = "ERROR"
    FATAL = "FATAL"
    CRITICAL = "CRITICAL"

    @classmethod
    def _missing_(cls, value: object) -> LogLevel:
        """Convert string to enum value.

        Raises
        ------
        ValueError
            If the value is not a valid log level.
        """
        if not isinstance(value, str):
            raise TypeError(f"Expected str, got {type(value)}")
        for member in cls:
            if member.value == value.upper():
                return member
        raise ValueError(f"{value} is not a valid log level.")

    def __str__(self) -> str:
        """Return the enum value as a string."""
        return self.value

    def as_int(self) -> int:
        """Return the stdlib log level int corresponding to the level."""
        res = logging.getLevelName(self.value)
        if not isinstance(res, int):
            return logging.NOTSET
        return res

    @classmethod
    def levels(cls) -> dict[LogLevel, int]:
        """Return a dict of all log levels and their corresponding int values."""
        return {level: level.as_int() for level in cls}

Attributes

NOTSET = 'NOTSET' class-attribute instance-attribute
DEBUG = 'DEBUG' class-attribute instance-attribute
INFO = 'INFO' class-attribute instance-attribute
WARN = 'WARN' class-attribute instance-attribute
WARNING = 'WARNING' class-attribute instance-attribute
ERROR = 'ERROR' class-attribute instance-attribute
FATAL = 'FATAL' class-attribute instance-attribute
CRITICAL = 'CRITICAL' class-attribute instance-attribute

Functions

__str__() -> str

Return the enum value as a string.

Source code in harbor_cli/logs.py
def __str__(self) -> str:
    """Return the enum value as a string."""
    return self.value
as_int() -> int

Return the stdlib log level int corresponding to the level.

Source code in harbor_cli/logs.py
def as_int(self) -> int:
    """Return the stdlib log level int corresponding to the level."""
    res = logging.getLevelName(self.value)
    if not isinstance(res, int):
        return logging.NOTSET
    return res
levels() -> dict[LogLevel, int] classmethod

Return a dict of all log levels and their corresponding int values.

Source code in harbor_cli/logs.py
@classmethod
def levels(cls) -> dict[LogLevel, int]:
    """Return a dict of all log levels and their corresponding int values."""
    return {level: level.as_int() for level in cls}

LogLineFormatter

Bases: Formatter

Replaces newlines in log messages with spaces.

Source code in harbor_cli/logs.py
class LogLineFormatter(logging.Formatter):
    """Replaces newlines in log messages with spaces."""

    def format(self, record: logging.LogRecord) -> str:
        """Format the log message."""
        record.msg = record.msg.replace("\n", " ")
        return super().format(record)

Functions

format(record: logging.LogRecord) -> str

Format the log message.

Source code in harbor_cli/logs.py
def format(self, record: logging.LogRecord) -> str:
    """Format the log message."""
    record.msg = record.msg.replace("\n", " ")
    return super().format(record)

Functions

setup_logging(config: LoggingSettings) -> None

Set up file logging.

Source code in harbor_cli/logs.py
def setup_logging(config: LoggingSettings) -> None:
    """Set up file logging."""
    if not logger.disabled:  # prevent re-configuring in REPL
        return
    file_handler = _get_file_handler(config)
    logger.addHandler(file_handler)
    logger.disabled = False

update_logging(config: LoggingSettings) -> None

Update logging configuration.

Source code in harbor_cli/logs.py
def update_logging(config: LoggingSettings) -> None:
    """Update logging configuration."""
    if logger.handlers:
        if not config.enabled:
            disable_logging()
        else:
            file_handler = _get_file_handler(config)
            replace_handler(file_handler)
    else:
        setup_logging(config)

replace_handler(handler: logging.Handler) -> None

Replace the file handler with the given handler.

Source code in harbor_cli/logs.py
def replace_handler(handler: logging.Handler) -> None:
    """Replace the file handler with the given handler."""
    logger.removeHandler(logger.handlers[0])
    logger.addHandler(handler)

disable_logging() -> None

Disable logging.

Source code in harbor_cli/logs.py
def disable_logging() -> None:
    """Disable logging."""
    logger.handlers.clear()
    logger.disabled = True