|
1 | 1 | import logging |
| 2 | +from typing import Final |
2 | 3 |
|
3 | 4 | from common_library.json_serialization import json_dumps |
4 | 5 | from fastapi import FastAPI, HTTPException, status |
5 | 6 | from fastapi.exceptions import RequestValidationError |
6 | 7 | from fastapi_lifespan_manager import LifespanManager |
7 | 8 | from servicelib.fastapi.lifespan_utils import Lifespan |
| 9 | +from servicelib.fastapi.logging_lifespan import setup_logging_shutdown_event |
8 | 10 | from servicelib.fastapi.openapi import ( |
9 | 11 | get_common_oas_options, |
10 | 12 | override_fastapi_openapi_method, |
|
51 | 53 |
|
52 | 54 | _logger = logging.getLogger(__name__) |
53 | 55 |
|
| 56 | +_NOISY_LOGGERS: Final[tuple[str, ...]] = ( |
| 57 | + "aio_pika", |
| 58 | + "aiormq", |
| 59 | + "httpcore", |
| 60 | + "httpx", |
| 61 | +) |
| 62 | + |
54 | 63 |
|
55 | 64 | def _set_exception_handlers(app: FastAPI): |
56 | 65 | app.add_exception_handler(HTTPException, http_error_handler) |
@@ -104,26 +113,44 @@ def create_app_lifespan(logging_lifespan: Lifespan | None = None) -> LifespanMan |
104 | 113 |
|
105 | 114 |
|
106 | 115 | def create_base_app( |
107 | | - settings: AppSettings | None = None, |
| 116 | + app_settings: AppSettings | None = None, |
108 | 117 | ) -> FastAPI: |
109 | | - if settings is None: |
110 | | - settings = AppSettings.create_from_envs() |
111 | | - assert settings # nosec |
| 118 | + if app_settings is None: |
| 119 | + app_settings = AppSettings.create_from_envs() |
| 120 | + |
| 121 | + logging_shutdown_event = setup_logging_shutdown_event( |
| 122 | + log_format_local_dev_enabled=app_settings.DIRECTOR_V2_LOG_FORMAT_LOCAL_DEV_ENABLED, |
| 123 | + logger_filter_mapping=app_settings.DIRECTOR_V2_LOG_FILTER_MAPPING, |
| 124 | + tracing_settings=app_settings.DIRECTOR_V2_TRACING, |
| 125 | + log_base_level=app_settings.log_level, |
| 126 | + noisy_loggers=_NOISY_LOGGERS, |
| 127 | + ) |
112 | 128 |
|
113 | | - assert settings.SC_BOOT_MODE # nosec |
| 129 | + _logger.info( |
| 130 | + "Application settings: %s", |
| 131 | + json_dumps(app_settings, indent=2, sort_keys=True), |
| 132 | + ) |
| 133 | + |
| 134 | + assert app_settings # nosec |
| 135 | + |
| 136 | + assert app_settings.SC_BOOT_MODE # nosec |
114 | 137 | app = FastAPI( |
115 | | - debug=settings.SC_BOOT_MODE.is_devel_mode(), |
| 138 | + debug=app_settings.SC_BOOT_MODE.is_devel_mode(), |
116 | 139 | title=PROJECT_NAME, |
117 | 140 | description=SUMMARY, |
118 | 141 | version=API_VERSION, |
119 | 142 | openapi_url=f"/api/{API_VTAG}/openapi.json", |
120 | | - **get_common_oas_options(is_devel_mode=settings.SC_BOOT_MODE.is_devel_mode()), |
| 143 | + **get_common_oas_options( |
| 144 | + is_devel_mode=app_settings.SC_BOOT_MODE.is_devel_mode() |
| 145 | + ), |
121 | 146 | ) |
122 | 147 | override_fastapi_openapi_method(app) |
123 | | - app.state.settings = settings |
| 148 | + app.state.settings = app_settings |
124 | 149 |
|
125 | 150 | app.include_router(api_router) |
126 | 151 |
|
| 152 | + app.add_event_handler("shutdown", logging_shutdown_event) |
| 153 | + |
127 | 154 | return app |
128 | 155 |
|
129 | 156 |
|
|
0 commit comments