-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from stuartcampbell/improve-logging
Small improvements to logging
- Loading branch information
Showing
12 changed files
with
162 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from nsls2api.main import app | ||
from asgi_correlation_id import correlation_id | ||
from fastapi import HTTPException, Request | ||
from fastapi.exception_handlers import http_exception_handler | ||
from fastapi.responses import JSONResponse | ||
|
||
|
||
# This is to make sure we add the request ID to the response headers for the case | ||
# of unhandled server errors. | ||
@app.exception_handler(Exception) | ||
async def unhandled_exception_handler(request: Request, exc: Exception) -> JSONResponse: | ||
return await http_exception_handler( | ||
request, | ||
HTTPException( | ||
500, | ||
"Internal server error - unhandled exception", | ||
headers={"X-Request-ID": correlation_id.get() or ""}, | ||
), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import logging | ||
|
||
import nsls2api | ||
|
||
# Grab the uvicorn logger so we can generate logs to the application log | ||
logger = logging.getLogger(f"uvicorn.error.{nsls2api.__name__}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
version: 1 | ||
disable_existing_loggers: False | ||
filters: | ||
correlation_id: | ||
"()": asgi_correlation_id.CorrelationIdFilter | ||
uuid_length: 16 | ||
default_value: '-' | ||
formatters: | ||
default: | ||
"()": uvicorn.logging.DefaultFormatter | ||
datefmt: "%Y-%m-%dT%H:%M:%S" | ||
format: '[%(asctime)s.%(msecs)03dZ] [%(correlation_id)s] %(levelprefix)s %(message)s' | ||
use_colors: True | ||
access: | ||
"()": uvicorn.logging.AccessFormatter | ||
datefmt: "%Y-%m-%dT%H:%M:%S" | ||
format: '[%(asctime)s.%(msecs)03dZ] [%(correlation_id)s] %(levelprefix)s %(client_addr)s - "%(request_line)s" %(status_code)s' | ||
handlers: | ||
default: | ||
formatter: default | ||
class: logging.StreamHandler | ||
filters: | ||
- correlation_id | ||
stream: ext://sys.stderr | ||
access: | ||
formatter: access | ||
class: logging.StreamHandler | ||
filters: | ||
- correlation_id | ||
stream: ext://sys.stdout | ||
loggers: | ||
uvicorn.error: | ||
level: INFO | ||
handlers: | ||
- default | ||
propagate: no | ||
uvicorn.access: | ||
level: INFO | ||
handlers: | ||
- access | ||
propagate: no |