Skip to content

Commit

Permalink
Merge pull request #70 from community-of-python/allow-turning-off-log…
Browse files Browse the repository at this point in the history
…ging-middleware

allow turning off logging middleware
  • Loading branch information
lesnik512 authored Feb 5, 2025
2 parents c242038 + 2ebb39f commit 8f6510c
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ class YourSettings(BaseServiceSettings):
logging_unset_handlers: list[str] = ["uvicorn", "uvicorn.access"]
logging_extra_processors: list[typing.Any] = []
logging_exclude_endpoints: list[str] = []
logging_turn_off_middleware: bool = False
```

Parameters description:
Expand All @@ -407,6 +408,7 @@ Parameters description:
- `logging_unset_handlers` - Unset logger handlers.
- `logging_extra_processors` - Set additional structlog processors if needed.
- `logging_exclude_endpoints` - Exclude logging on specific endpoints.
- `logging_turn_off_middleware` - Turning off logging middleware.

### CORS

Expand Down
7 changes: 4 additions & 3 deletions microbootstrap/bootstrappers/fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,10 @@ def bootstrap_after(self, application: ApplicationT) -> ApplicationT:
@FastApiBootstrapper.use_instrument()
class FastApiLoggingInstrument(LoggingInstrument):
def bootstrap_after(self, application: ApplicationT) -> ApplicationT:
application.add_middleware( # type: ignore[call-arg]
build_fastapi_logging_middleware(self.instrument_config.logging_exclude_endpoints), # type: ignore[arg-type]
)
if not self.instrument_config.logging_turn_off_middleware:
application.add_middleware( # type: ignore[call-arg]
build_fastapi_logging_middleware(self.instrument_config.logging_exclude_endpoints), # type: ignore[arg-type]
)
return application


Expand Down
3 changes: 3 additions & 0 deletions microbootstrap/bootstrappers/litestar.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ def bootstrap_before(self) -> dict[str, typing.Any]:
@LitestarBootstrapper.use_instrument()
class LitestarLoggingInstrument(LoggingInstrument):
def bootstrap_before(self) -> dict[str, typing.Any]:
if self.instrument_config.logging_turn_off_middleware:
return {}

return {"middleware": [build_litestar_logging_middleware(self.instrument_config.logging_exclude_endpoints)]}


Expand Down
1 change: 1 addition & 0 deletions microbootstrap/instruments/logging_instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class LoggingConfig(BaseInstrumentConfig):
default_factory=lambda: ["uvicorn", "uvicorn.access"],
)
logging_exclude_endpoints: list[str] = pydantic.Field(default_factory=list)
logging_turn_off_middleware: bool = False


class LoggingInstrument(Instrument[LoggingConfig]):
Expand Down
8 changes: 6 additions & 2 deletions tests/bootstrappers/test_litestar.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from unittest.mock import MagicMock

import litestar
import pytest
from litestar import status_codes
from litestar.middleware.base import MiddlewareProtocol
from litestar.testing import AsyncTestClient
Expand All @@ -13,11 +14,14 @@
from microbootstrap.bootstrappers.fastapi import FastApiBootstrapper # noqa: F401


async def test_litestar_configure_instrument() -> None:
@pytest.mark.parametrize("logging_turn_off_middleware", [True, False])
async def test_litestar_configure_instrument(logging_turn_off_middleware: bool) -> None:
test_metrics_path: typing.Final = "/test-metrics-path"

application: typing.Final = (
LitestarBootstrapper(LitestarSettings())
LitestarBootstrapper(
LitestarSettings(logging_turn_off_middleware=logging_turn_off_middleware, service_debug=False)
)
.configure_instrument(
LitestarPrometheusConfig(prometheus_metrics_path=test_metrics_path),
)
Expand Down

0 comments on commit 8f6510c

Please sign in to comment.