|
15 | 15 |
|
16 | 16 | def init_logger(config: AppConfig) -> None: |
17 | 17 | """ |
18 | | - Configure structlog and stdlib logging with shared handler and formatter. |
| 18 | + Function to initialize logging configuration using `structlog` and Python's standard |
| 19 | + logging module. It supports dynamic log level adjustment, shared processors for |
| 20 | + structlog and standard loggers, and tailored configurations for different environments |
| 21 | + (local, test, or production). Ensures consistent formatting across application logs |
| 22 | + and integrates handlers for OpenTelemetry logs if present. |
| 23 | +
|
| 24 | + Args: |
| 25 | + config (AppConfig): Configuration object containing application-wide settings |
| 26 | + such as DEBUG flag and environment status. |
| 27 | +
|
| 28 | + Raises: |
| 29 | + None |
| 30 | +
|
| 31 | + Returns: |
| 32 | + None |
19 | 33 |
|
20 | | - :param config: The app configuration |
21 | | - :type config: AppConfig |
22 | | - :return: |
23 | 34 | """ |
24 | 35 | # Strongly inspired by https://gist.github.com/nymous/f138c7f06062b7c43c060bf03759c29e |
25 | 36 |
|
@@ -83,10 +94,9 @@ def init_logger(config: AppConfig) -> None: |
83 | 94 | # In case there's a OTEL handler we keep it but remove all the others, |
84 | 95 | # in case this function is called multiple times. |
85 | 96 | # NOTE all the processors are not applied to OTEL logs! |
86 | | - for l in stdlib_logger.handlers: |
87 | | - if not isinstance(l, LoggingHandler): |
88 | | - stdlib_logger.removeHandler(l) |
89 | | - |
| 97 | + for handler in stdlib_logger.handlers: |
| 98 | + if not isinstance(handler, LoggingHandler): |
| 99 | + stdlib_logger.removeHandler(handler) |
90 | 100 |
|
91 | 101 | # Use structlog to format logs coming from stdlib logger |
92 | 102 | stdlib_logger.addHandler(stdlib_handler) |
|
0 commit comments