mirror of
https://github.com/Xevion/linkpulse.git
synced 2025-12-07 03:15:37 -06:00
Set access logs to debug, millisecond process time, pluralize word option
I'm unsure if it's good to use string notation in the duration, maybe duration_ms to imply the unit would be better?
This commit is contained in:
@@ -30,8 +30,9 @@ class LoggingMiddleware(BaseHTTPMiddleware):
|
||||
structlog.stdlib.get_logger("api.error").exception("Uncaught exception")
|
||||
raise
|
||||
finally:
|
||||
process_time = time.perf_counter_ns() - start_time
|
||||
self.access_logger.info(
|
||||
process_time_ms = (time.perf_counter_ns() - start_time) / 10 ** 6
|
||||
|
||||
self.access_logger.debug(
|
||||
"Request",
|
||||
http={
|
||||
"url": str(request.url),
|
||||
@@ -42,7 +43,7 @@ class LoggingMiddleware(BaseHTTPMiddleware):
|
||||
"version": request.scope["http_version"],
|
||||
},
|
||||
client={"ip": request.client.host, "port": request.client.port} if request.client else None,
|
||||
duration=process_time,
|
||||
duration="{:.2f}ms".format(process_time_ms),
|
||||
)
|
||||
|
||||
# response.headers["X-Process-Time"] = str(process_time / 10 ** 9)
|
||||
|
||||
@@ -2,10 +2,12 @@ from typing import Optional
|
||||
from fastapi import Request
|
||||
|
||||
|
||||
def pluralize(count: int) -> str:
|
||||
def pluralize(count: int, word: Optional[str] = None) -> str:
|
||||
"""
|
||||
Pluralize a word based on count. Returns 's' if count is not 1, '' (empty string) otherwise.
|
||||
"""
|
||||
if word:
|
||||
return word + 's' if count != 1 else word
|
||||
return 's' if count != 1 else ''
|
||||
|
||||
def get_ip(request: Request) -> Optional[str]:
|
||||
|
||||
Reference in New Issue
Block a user