-
-
Notifications
You must be signed in to change notification settings - Fork 52
Add logging configuration and setup #306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
+135
−12
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
b6ae097
Logging but request id context is not bound properly
PGijsbers 346c97f
Fix middleware order
PGijsbers 186d196
Fully transition to Loguru and make it configurable
PGijsbers ee38e33
ignore log files
PGijsbers 964e55c
Add function to setup loguru sinks based on configuration
PGijsbers 48a73ef
Move logging middleware to logging module
PGijsbers dd806b1
Remove automated logging of client ip address
PGijsbers 37e974a
Disable logging to file with pytest
PGijsbers c62218c
Use a different configuration for the tests
PGijsbers 2659219
Do not log the request body
PGijsbers f7c6ff6
Disable loguru
PGijsbers 38d1d8d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1f1fa55
Add back import statements
PGijsbers 9c81a9a
Startup logging
PGijsbers 6b276de
Add back the context middleware
PGijsbers 9f58623
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 8e5c561
Add back in logging, but with placeholder messages
PGijsbers 4e5d3da
Add back in full logging of requests
PGijsbers File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| docker/mysql/data | ||
| *.log | ||
| logs/ | ||
| .DS_Store | ||
|
|
||
| # Byte-compiled / optimized / DLL files | ||
|
|
||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ classifiers = [ | |
| ] | ||
| dependencies = [ | ||
| "fastapi", | ||
| "loguru", | ||
| "cryptography", | ||
| "pydantic", | ||
| "uvicorn", | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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,56 @@ | ||
| """Utility functions for logging.""" | ||
|
|
||
| import sys | ||
| import uuid | ||
| from collections.abc import Awaitable, Callable | ||
| from pathlib import Path | ||
|
|
||
| from loguru import logger | ||
| from starlette.requests import Request | ||
| from starlette.responses import Response | ||
|
|
||
| from config import load_configuration | ||
|
|
||
|
|
||
| def setup_log_sinks(configuration_file: Path | None = None) -> None: | ||
| """Configure loguru based on app configuration.""" | ||
| configuration = load_configuration(configuration_file) | ||
| for nickname, sink_configuration in configuration.get("logging", {}).items(): | ||
| logger.info("Configuring sink", nickname=nickname, **sink_configuration) | ||
| sink = sink_configuration.pop("sink") | ||
PGijsbers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if sink == "sys.stderr": | ||
| sink = sys.stderr | ||
| logger.add(sink, serialize=True, **sink_configuration) | ||
|
|
||
|
|
||
| async def add_request_context_to_log( | ||
| request: Request, | ||
| call_next: Callable[[Request], Awaitable[Response]], | ||
| ) -> Response: | ||
| """Add a unique request id to each log call.""" | ||
| identifier = uuid.uuid4().hex | ||
| with logger.contextualize(request_id=identifier): | ||
| return await call_next(request) | ||
|
|
||
|
|
||
| async def request_response_logger( | ||
| request: Request, | ||
| call_next: Callable[[Request], Awaitable[Response]], | ||
| ) -> Response: | ||
| """Log the incoming request and outgoing response.""" | ||
| logger.info( | ||
| "request", | ||
| url=request.url, | ||
| headers=request.headers, | ||
| cookies=request.cookies, | ||
| path_params=request.path_params, | ||
| query_params=request.query_params, | ||
| ) | ||
| response: Response = await call_next(request) | ||
| logger.info( | ||
| "response", | ||
| status_code=response.status_code, | ||
| headers=response.headers, | ||
| media_type=response.media_type, | ||
| ) | ||
| return response | ||
This file contains hidden or 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 hidden or 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,28 @@ | ||
| arff_base_url="https://test.openml.org" | ||
| minio_base_url="https://openml1.win.tue.nl" | ||
|
|
||
| [development] | ||
| allow_test_api_keys=true | ||
|
|
||
| [logging.develop] | ||
| sink="sys.stderr" | ||
| level="DEBUG" | ||
|
|
||
| [fastapi] | ||
| root_path="" | ||
|
|
||
| [databases.defaults] | ||
| host="database" | ||
| port="3306" | ||
| # SQLAlchemy `dialect` and `driver`: https://docs.sqlalchemy.org/en/20/dialects/index.html | ||
| drivername="mysql+aiomysql" | ||
|
|
||
| [databases.expdb] | ||
| database="openml_expdb" | ||
|
|
||
| [databases.openml] | ||
| database="openml" | ||
|
|
||
| [routing] | ||
| minio_url="http://minio:9000/" | ||
| server_url="http://php-api:80/" |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major
Missing caching - delegates to uncached file read.
load_configurationduplicates thetomllib.loads(file.read_text())logic from_load_configuration(line 49-50) but lacks the@functools.cachedecorator. Each call will re-read and re-parse the file.Delegate to
_load_configurationto benefit from caching and reduce duplication:♻️ Proposed fix
def load_configuration(file: Path | None = None) -> TomlTable: file = file or _config_file - return tomllib.loads(file.read_text()) + return _load_configuration(file)🤖 Prompt for AI Agents