mirror of
https://github.com/Xevion/linkpulse.git
synced 2026-01-31 10:24:47 -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")
|
structlog.stdlib.get_logger("api.error").exception("Uncaught exception")
|
||||||
raise
|
raise
|
||||||
finally:
|
finally:
|
||||||
process_time = time.perf_counter_ns() - start_time
|
process_time_ms = (time.perf_counter_ns() - start_time) / 10 ** 6
|
||||||
self.access_logger.info(
|
|
||||||
|
self.access_logger.debug(
|
||||||
"Request",
|
"Request",
|
||||||
http={
|
http={
|
||||||
"url": str(request.url),
|
"url": str(request.url),
|
||||||
@@ -42,7 +43,7 @@ class LoggingMiddleware(BaseHTTPMiddleware):
|
|||||||
"version": request.scope["http_version"],
|
"version": request.scope["http_version"],
|
||||||
},
|
},
|
||||||
client={"ip": request.client.host, "port": request.client.port} if request.client else None,
|
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)
|
# response.headers["X-Process-Time"] = str(process_time / 10 ** 9)
|
||||||
|
|||||||
@@ -2,10 +2,12 @@ from typing import Optional
|
|||||||
from fastapi import Request
|
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.
|
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 ''
|
return 's' if count != 1 else ''
|
||||||
|
|
||||||
def get_ip(request: Request) -> Optional[str]:
|
def get_ip(request: Request) -> Optional[str]:
|
||||||
|
|||||||
Reference in New Issue
Block a user