|
1 | 1 | """Main application to be deployed by uvicorn (or equivalent) server""" |
2 | 2 |
|
| 3 | +import logging |
| 4 | +from typing import Final |
| 5 | + |
| 6 | +from common_library.json_serialization import json_dumps |
3 | 7 | from fastapi import FastAPI |
4 | | -from servicelib.logging_utils import setup_loggers |
| 8 | +from servicelib.fastapi.logging_lifespan import setup_logging_shutdown_event |
5 | 9 | from simcore_service_resource_usage_tracker.core.application import create_app |
6 | 10 | from simcore_service_resource_usage_tracker.core.settings import ApplicationSettings |
7 | 11 |
|
8 | | -the_settings = ApplicationSettings.create_from_envs() |
| 12 | +_logger = logging.getLogger(__name__) |
9 | 13 |
|
10 | | -setup_loggers( |
11 | | - log_format_local_dev_enabled=the_settings.RESOURCE_USAGE_TRACKER_LOG_FORMAT_LOCAL_DEV_ENABLED, |
12 | | - logger_filter_mapping=the_settings.RESOURCE_USAGE_TRACKER_LOG_FILTER_MAPPING, |
13 | | - tracing_settings=the_settings.RESOURCE_USAGE_TRACKER_TRACING, |
14 | | - log_base_level=the_settings.log_level, |
15 | | - noisy_loggers=None, |
| 14 | +_NOISY_LOGGERS: Final[tuple[str, ...]] = ( |
| 15 | + "aiobotocore", |
| 16 | + "aio_pika", |
| 17 | + "aiormq", |
| 18 | + "botocore", |
| 19 | + "werkzeug", |
16 | 20 | ) |
17 | 21 |
|
18 | | -# SINGLETON FastAPI app |
19 | | -the_app: FastAPI = create_app(the_settings) |
| 22 | + |
| 23 | +def app_factory() -> FastAPI: |
| 24 | + app_settings = ApplicationSettings.create_from_envs() |
| 25 | + logging_shutdown_event = setup_logging_shutdown_event( |
| 26 | + log_format_local_dev_enabled=app_settings.RESOURCE_USAGE_TRACKER_LOG_FORMAT_LOCAL_DEV_ENABLED, |
| 27 | + logger_filter_mapping=app_settings.RESOURCE_USAGE_TRACKER_LOG_FILTER_MAPPING, |
| 28 | + tracing_settings=app_settings.RESOURCE_USAGE_TRACKER_TRACING, |
| 29 | + log_base_level=app_settings.log_level, |
| 30 | + noisy_loggers=_NOISY_LOGGERS, |
| 31 | + ) |
| 32 | + |
| 33 | + _logger.info( |
| 34 | + "Application settings: %s", |
| 35 | + json_dumps(app_settings, indent=2, sort_keys=True), |
| 36 | + ) |
| 37 | + app = create_app(settings=app_settings) |
| 38 | + app.add_event_handler("shutdown", logging_shutdown_event) |
| 39 | + return app |
0 commit comments