Skip to content

logging_config

get_logging_format()

Get valid logging format from environment variable or default value.

Source code in src/fractal_task_tools/logging_config.py
21
22
23
24
25
26
27
28
29
def get_logging_format() -> str:
    """
    Get valid logging format from environment variable or default value.
    """
    # Use default value if the env variable is unset or set to an empty string
    log_format = os.getenv("FRACTAL_TASK_LOG_FORMAT") or DEFAULT_LOG_FORMAT
    # Validate `log_format`
    logging.PercentStyle(fmt=log_format).validate()
    return log_format

get_logging_level()

Get valid logging level from environment variable or default value.

Source code in src/fractal_task_tools/logging_config.py
32
33
34
35
36
37
38
39
40
41
42
43
44
def get_logging_level() -> ValidLoggingLevel:
    """
    Get valid logging level from environment variable or default value.
    """
    # Use default value if the env variable is unset or set to an empty string
    log_level = os.getenv("FRACTAL_TASK_LOG_LEVEL") or DEFAULT_LOG_LEVEL
    # Validate `log_level`
    if log_level not in ALLOWED_LOGGING_LEVELS:
        raise ValueError(
            f"Invalid FRACTAL_TASK_LOG_LEVEL={log_level} environment "
            f"variable. Allowed values: {ALLOWED_LOGGING_LEVELS}."
        )
    return log_level

setup_logging_config()

Configure root logging handler.

Note that calling logging.basicConfig with force=True removes all existing handlers of the root logger and creates a new handler.

Source code in src/fractal_task_tools/logging_config.py
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
def setup_logging_config() -> None:
    """
    Configure root logging handler.

    Note that calling `logging.basicConfig` with `force=True` removes all
    existing handlers of the `root` logger and creates a new handler.
    """
    FRACTAL_TASK_LOG_LEVEL = get_logging_level()
    FRACTAL_TASK_LOG_FORMAT = get_logging_format()
    logging.basicConfig(
        level=FRACTAL_TASK_LOG_LEVEL,
        format=FRACTAL_TASK_LOG_FORMAT,
        force=True,
    )
    task_wrapper_logger = logging.getLogger(WRAPPER_LOGGER_NAME)
    task_wrapper_logger.debug(f"Logging level: {FRACTAL_TASK_LOG_LEVEL=}")
    task_wrapper_logger.debug(f"Logging format: {FRACTAL_TASK_LOG_FORMAT=}")