Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/1.bump-version.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
contents: write
steps:
- name: Checkout
uses: actions/checkout@v6
uses: actions/checkout@v7
- name: Bump version
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/2.build-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
# contents: read
# steps:
# - name: Checkout
# uses: actions/checkout@v6
# uses: actions/checkout@v7
# - name: Set up Python
# uses: actions/setup-python@v6
# with:
Expand All @@ -37,7 +37,7 @@ jobs:
contents: write
steps:
- name: Checkout
uses: actions/checkout@v6
uses: actions/checkout@v7
- name: Get latest version
run: |
git pull origin main
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/3.update-changelog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
contents: write
steps:
- name: Checkout
uses: actions/checkout@v6
uses: actions/checkout@v7
- name: Update changelog
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
contents: write
steps:
- name: Checkout
uses: actions/checkout@v6
uses: actions/checkout@v7
with:
fetch-depth: 0
- name: Set up Python
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ repos:

# --- Markdown ---
- repo: https://github.com/igorshubovych/markdownlint-cli
rev: v0.48.0
rev: v0.49.0
hooks:
- id: markdownlint
name: "📝 markdownlint - Lint markdown files"
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
fastapi>=0.99.1,<1.0.0
beans-logging>=12.0.3,<13.0.0
beans-logging>=12.0.4,<13.0.0
24 changes: 13 additions & 11 deletions src/beans_logging_fastapi/middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ async def dispatch(self, request: Request, call_next: Callable) -> Response:
_http_info["request_id"] = request.headers.get("X-Correlation-ID")

# Set request_id to request state:
request.state.request_id = _http_info["request_id"]
request.state.request_id = _http_info.get("request_id")

_http_info["client_host"] = "0.0.0.0" # nosec: B104
if request.client:
_http_info["client_host"] = request.client.host

Expand All @@ -59,7 +60,7 @@ async def dispatch(self, request: Request, call_next: Callable) -> Response:
)

_http_info["request_port"] = request.url.port
_http_info["http_version"] = request.scope["http_version"]
_http_info["http_version"] = request.scope.get("http_version", "1.1")

if self.has_proxy_headers:
if "X-Real-IP" in request.headers:
Expand Down Expand Up @@ -159,11 +160,11 @@ async def dispatch(self, request: Request, call_next: Callable) -> Response:
if hasattr(request.state, "user_id"):
_http_info["user_id"] = str(request.state.user_id)

_logger = logger.bind(request_id=_http_info["request_id"])
_logger = logger.bind(request_id=_http_info.get("request_id"))

# Set http info to request state:
request.state.logger = _logger
request.state.client_ip = _http_info.get("client_host", "")
request.state.client_host = _http_info.get("client_host")
request.state.http_info = _http_info
response: Response = await call_next(request)
return response
Expand Down Expand Up @@ -204,10 +205,10 @@ async def dispatch(self, request: Request, call_next: Callable) -> Response:
"should be parseable to <float>!"
)
else:
response.headers["X-Process-Time"] = str(_http_info["response_time"])
response.headers["X-Process-Time"] = str(_http_info.get("response_time", 0))

if "X-Request-ID" not in response.headers:
response.headers["X-Request-ID"] = _http_info["request_id"]
response.headers["X-Request-ID"] = _http_info.get("request_id", "")

if hasattr(request.state, "user_id"):
_http_info["user_id"] = str(request.state.user_id)
Expand Down Expand Up @@ -330,19 +331,20 @@ async def dispatch(self, request: Request, call_next) -> Response:
# Http access log:
_LEVEL = "INFO"
_sub_format = self.sub_format
if _http_info["status_code"] < 200:
_status_code = _http_info.get("status_code", 200)
if _status_code < 200:
_LEVEL = "DEBUG"
_sub_format = f'<d>{_sub_format.replace("{status_code}", "<n><b><k>{status_code}</k></b></n>")}</d>'
elif (200 <= _http_info["status_code"]) and (_http_info["status_code"] < 300):
elif (200 <= _status_code) and (_status_code < 300):
_LEVEL = "SUCCESS"
_sub_format = f'<w>{_sub_format.replace("{status_code}", "<lvl>{status_code}</lvl>")}</w>'
elif (300 <= _http_info["status_code"]) and (_http_info["status_code"] < 400):
elif (300 <= _status_code) and (_status_code < 400):
_LEVEL = "INFO"
_sub_format = f'<d>{_sub_format.replace("{status_code}", "<n><b><c>{status_code}</c></b></n>")}</d>'
elif (400 <= _http_info["status_code"]) and (_http_info["status_code"] < 500):
elif (400 <= _status_code) and (_status_code < 500):
_LEVEL = "WARNING"
_sub_format = _sub_format.replace("{status_code}", "<r>{status_code}</r>")
elif 500 <= _http_info["status_code"]:
elif 500 <= _status_code:
_LEVEL = "ERROR"
_sub_format = (
f'{_sub_format.replace("{status_code}", "<n>{status_code}</n>")}'
Expand Down