-
Notifications
You must be signed in to change notification settings - Fork 176
Improve logging #235
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
Merged
Merged
Improve logging #235
Changes from 7 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e4342e8
WIP: improve logging
stellasia 3033ef8
Remove unused code
stellasia 80708e4
Cut long string, configure via env vars, restructure utils folder
stellasia 37f964a
ruff
stellasia 67174eb
Fix tests
stellasia 741a103
Update changelog
stellasia 2f47d00
There is no reason not to test that
stellasia b919720
Rename
stellasia 516b220
Add tests
stellasia ea0d0da
Update log message
stellasia decd3b8
Ruff
stellasia 9fa0974
Ruff again
stellasia d395b38
Spelling
stellasia 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
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
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
Empty file.
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,80 @@ | ||
| # Copyright (c) "Neo4j" | ||
| # Neo4j Sweden AB [https://neo4j.com] | ||
| # # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| from typing import Any | ||
|
|
||
| from pydantic import BaseModel | ||
|
|
||
| DEFAULT_MAX_LIST_LENGTH: int = 5 | ||
| DEFAULT_MAX_STRING_LENGTH: int = 200 | ||
|
|
||
|
|
||
| class Prettyfier: | ||
alexthomas93 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """Prettyfy any object for logging. | ||
| I.e.: truncate long lists and strings, even nested. | ||
| Max list and string length can be configured using env variables: | ||
| - LOGGING__MAX_LIST_LENGTH (int) | ||
| - LOGGING__MAX_STRING_LENGTH (int) | ||
| """ | ||
|
|
||
| def __init__(self) -> None: | ||
| self.max_list_length = int( | ||
| os.environ.get("LOGGING__MAX_LIST_LENGTH", DEFAULT_MAX_LIST_LENGTH) | ||
| ) | ||
| self.max_string_length = int( | ||
| os.environ.get("LOGGING__MAX_STRING_LENGTH", DEFAULT_MAX_STRING_LENGTH) | ||
| ) | ||
|
|
||
| def _prettyfy_dict(self, value: dict[Any, Any]) -> dict[Any, Any]: | ||
| return { | ||
| k: self(v) # prettyfy each value | ||
| for k, v in value.items() | ||
| } | ||
|
|
||
| def _prettyfy_list(self, value: list[Any]) -> list[Any]: | ||
| items = [ | ||
| self(v) # prettify each item | ||
| for v in value[: self.max_list_length] | ||
| ] | ||
| remaining_items = len(value) - len(items) | ||
| if remaining_items > 0: | ||
| items.append(f"... ({remaining_items} items)") | ||
| return items | ||
|
|
||
| def _prettyfy_str(self, value: str) -> str: | ||
| new_value = value[: self.max_string_length] | ||
| remaining_chars = len(value) - len(new_value) | ||
| if remaining_chars > 0: | ||
| new_value += f"... ({remaining_chars} chars)" | ||
| return new_value | ||
|
|
||
| def __call__(self, value: Any) -> Any: | ||
| """Takes any value and returns a prettified version for logging.""" | ||
| if isinstance(value, dict): | ||
| return self._prettyfy_dict(value) | ||
| if isinstance(value, BaseModel): | ||
| return self(value.model_dump()) | ||
| if isinstance(value, list): | ||
| return self._prettyfy_list(value) | ||
| if isinstance(value, str): | ||
| return self._prettyfy_str(value) | ||
| return value | ||
|
|
||
|
|
||
| prettyfier = Prettyfier() | ||
alexthomas93 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
File renamed without changes.
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
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.
Uh oh!
There was an error while loading. Please reload this page.