Skip to content

logger

Logger configuration.

ErrorHandler

ErrorHandler()

Bases: StreamHandler

Custom error handler for logging.

Source code in src/images_upload_cli/logger.py
12
13
14
15
def __init__(self: "ErrorHandler") -> None:
    """Init."""
    super().__init__()
    self.error_occurred = False

emit

emit(record: LogRecord) -> None

Emit a record.

Source code in src/images_upload_cli/logger.py
17
18
19
20
def emit(self: "ErrorHandler", record: logging.LogRecord) -> None:
    """Emit a record."""
    if record.levelno >= logging.ERROR:
        self.error_occurred = True

has_error_occurred

has_error_occurred() -> bool

Check if an error has occurred.

Source code in src/images_upload_cli/logger.py
22
23
24
def has_error_occurred(self: "ErrorHandler") -> bool:
    """Check if an error has occurred."""
    return self.error_occurred

setup_logger

setup_logger(log_level: str) -> ErrorHandler

Configure logger.

Parameters:

Name Type Description Default
log_level str

The log level to set for the logger.

required

Returns:

Name Type Description
ErrorHandler ErrorHandler

The error handler associated with the logger.

Source code in src/images_upload_cli/logger.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
def setup_logger(log_level: str) -> ErrorHandler:
    """Configure logger.

    Args:
        log_level: The log level to set for the logger.

    Returns:
        ErrorHandler: The error handler associated with the logger.
    """
    logger.remove()
    # Console handler
    logger.add(
        sink=RichHandler(log_time_format="[%X]", rich_tracebacks=True),
        level=log_level,
        format=lambda _: "{message}",
    )
    # Error handler
    error_handler = ErrorHandler()
    logger.add(sink=error_handler, level="ERROR")

    return error_handler