|
| 1 | +import logging |
| 2 | +from collections.abc import AsyncIterator, Awaitable, Callable |
| 3 | +from contextlib import AsyncExitStack |
| 4 | + |
| 5 | +from fastapi import FastAPI |
| 6 | +from settings_library.tracing import TracingSettings |
| 7 | + |
| 8 | +from ..logging_utils import ( |
| 9 | + LogLevelInt, |
| 10 | + async_loggers, |
| 11 | + log_context, |
| 12 | +) |
| 13 | +from ..logging_utils_filtering import LoggerName, MessageSubstring |
| 14 | +from .lifespan_utils import Lifespan |
| 15 | + |
| 16 | +_logger = logging.getLogger(__name__) |
| 17 | + |
| 18 | + |
| 19 | +def create_logging_lifespan( |
| 20 | + *, |
| 21 | + log_format_local_dev_enabled: bool, |
| 22 | + logger_filter_mapping: dict[LoggerName, list[MessageSubstring]], |
| 23 | + tracing_settings: TracingSettings | None, |
| 24 | + log_base_level: LogLevelInt, |
| 25 | + noisy_loggers: tuple[str, ...] | None, |
| 26 | +) -> Lifespan: |
| 27 | + """Returns a FastAPI-compatible lifespan handler to set up async logging.""" |
| 28 | + exit_stack = AsyncExitStack() |
| 29 | + exit_stack.enter_context( |
| 30 | + async_loggers( |
| 31 | + log_base_level=log_base_level, |
| 32 | + noisy_loggers=noisy_loggers, |
| 33 | + log_format_local_dev_enabled=log_format_local_dev_enabled, |
| 34 | + logger_filter_mapping=logger_filter_mapping, |
| 35 | + tracing_settings=tracing_settings, |
| 36 | + ) |
| 37 | + ) |
| 38 | + |
| 39 | + async def _logging_lifespan(app: FastAPI) -> AsyncIterator[None]: |
| 40 | + assert app is not None, "app must be provided" |
| 41 | + yield |
| 42 | + with log_context(_logger, logging.INFO, "Re-enable Blocking logger"): |
| 43 | + await exit_stack.aclose() |
| 44 | + |
| 45 | + return _logging_lifespan |
| 46 | + |
| 47 | + |
| 48 | +def create_logging_shutdown_event( |
| 49 | + *, |
| 50 | + log_format_local_dev_enabled: bool, |
| 51 | + logger_filter_mapping: dict[LoggerName, list[MessageSubstring]], |
| 52 | + tracing_settings: TracingSettings | None, |
| 53 | + log_base_level: LogLevelInt, |
| 54 | + noisy_loggers: tuple[str, ...] | None, |
| 55 | +) -> Callable[[], Awaitable[None]]: |
| 56 | + """retruns a fastapi-compatible shutdown event handler to be used with old style lifespan |
| 57 | + handlers. This is useful for applications that do not use the new async lifespan |
| 58 | + handlers introduced in fastapi 0.100.0. |
| 59 | +
|
| 60 | + Note: This function is for backwards compatibility only and will be removed in the future. |
| 61 | + setup_logging_lifespan should be used instead for new style lifespan handlers. |
| 62 | + """ |
| 63 | + exit_stack = AsyncExitStack() |
| 64 | + exit_stack.enter_context( |
| 65 | + async_loggers( |
| 66 | + log_base_level=log_base_level, |
| 67 | + noisy_loggers=noisy_loggers, |
| 68 | + log_format_local_dev_enabled=log_format_local_dev_enabled, |
| 69 | + logger_filter_mapping=logger_filter_mapping, |
| 70 | + tracing_settings=tracing_settings, |
| 71 | + ) |
| 72 | + ) |
| 73 | + |
| 74 | + async def _on_shutdown_event() -> None: |
| 75 | + with log_context(_logger, logging.INFO, "Re-enable Blocking logger"): |
| 76 | + await exit_stack.aclose() |
| 77 | + |
| 78 | + return _on_shutdown_event |
0 commit comments