Skip to content

logging_config

Structured logging configuration.

Functions#

setup_logging #

setup_logging(default_level: str = 'INFO') -> None

Configure logging for the entire application.

PARAMETER DESCRIPTION
default_level

Default logging level.

TYPE: str DEFAULT: 'INFO'

The logging level can be overridden by setting the LOG_LEVEL_SRC environment. For example:

LOG_LEVEL_SRC=DEBUG python -m <your_module>
pytest -o log_cli_level=DEBUG tests/
Source code in src/src_method/utils/logging_config.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def setup_logging(default_level: str = "INFO") -> None:
    """Configure logging for the entire application.

    Args:
        default_level: Default logging level.

    The logging level can be overridden by setting the `LOG_LEVEL_SRC`
    environment. For example:

    ```bash
    LOG_LEVEL_SRC=DEBUG python -m <your_module>
    pytest -o log_cli_level=DEBUG tests/
    ```
    """
    # Configure Python's standard logging
    level_name = os.environ.get("LOG_LEVEL_SRC", default_level)
    level = logging.getLevelName(level_name.upper())
    logging.basicConfig(
        level=level,
        format="%(message)s",
        stream=sys.stdout,
    )

    # Remove colors for slurm logs but not for pytest
    use_colors = "SLURM_JOB_ID" not in os.environ

    # Configure structlog to use the standard logger
    structlog.configure(
        processors=[
            structlog.contextvars.merge_contextvars,
            structlog.processors.add_log_level,
            structlog.processors.StackInfoRenderer(),
            structlog.dev.set_exc_info,
            structlog.processors.TimeStamper(fmt="%Y-%m-%d %H:%M:%S", utc=False),
            structlog.dev.ConsoleRenderer(colors=use_colors),
        ],
        logger_factory=structlog.stdlib.LoggerFactory(),
        wrapper_class=structlog.stdlib.BoundLogger,
        cache_logger_on_first_use=True,
    )