|
28 | 28 | import litestar |
29 | 29 | from litestar.config.app import AppConfig |
30 | 30 | from litestar.config.cors import CORSConfig |
| 31 | + from litestar.logging.config import StructLoggingConfig |
31 | 32 | from litestar.openapi import OpenAPIConfig |
32 | 33 | from litestar.openapi.plugins import SwaggerRenderPlugin |
33 | 34 | from litestar.plugins.prometheus import PrometheusConfig, PrometheusController |
| 35 | + from litestar.plugins.structlog import StructlogConfig, StructlogPlugin |
34 | 36 | from litestar.static_files import create_static_files_router |
35 | 37 |
|
36 | 38 | if import_checker.is_litestar_opentelemetry_installed: |
|
42 | 44 | if import_checker.is_opentelemetry_installed: |
43 | 45 | from opentelemetry.trace import get_tracer_provider |
44 | 46 |
|
| 47 | +if import_checker.is_structlog_installed: |
| 48 | + import structlog |
| 49 | + |
45 | 50 |
|
46 | 51 | def build_span_name(method: str, route: str) -> str: |
47 | 52 | if not route: |
@@ -144,6 +149,25 @@ def bootstrap(self) -> None: |
144 | 149 | class LitestarLoggingInstrument(LoggingInstrument): |
145 | 150 | bootstrap_config: LitestarConfig |
146 | 151 |
|
| 152 | + def bootstrap(self) -> None: |
| 153 | + self._unset_handlers() |
| 154 | + if import_checker.is_structlog_installed and import_checker.is_litestar_installed: |
| 155 | + self.bootstrap_config.application_config.plugins.append( |
| 156 | + StructlogPlugin( |
| 157 | + config=StructlogConfig( |
| 158 | + structlog_logging_config=StructLoggingConfig( |
| 159 | + processors=self.structlog_processors, |
| 160 | + logger_factory=self.memory_logger_factory, |
| 161 | + wrapper_class=structlog.stdlib.BoundLogger, |
| 162 | + cache_logger_on_first_use=True, |
| 163 | + pretty_print_tty=False, |
| 164 | + standard_lib_logging_config=None, |
| 165 | + ), |
| 166 | + ), |
| 167 | + ) |
| 168 | + ) |
| 169 | + self._configure_foreign_loggers() |
| 170 | + |
147 | 171 |
|
148 | 172 | @dataclasses.dataclass(kw_only=True, frozen=True) |
149 | 173 | class LitestarOpenTelemetryInstrument(OpenTelemetryInstrument): |
|
0 commit comments