-
Notifications
You must be signed in to change notification settings - Fork 34
Added structured logging using Python logging to mimic GCP logs #2640
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
Open
v-lerie
wants to merge
8
commits into
master
Choose a base branch
from
new_issue2475
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
af70b66
Added structured logging using Python logging to mimic GCP logs
v-lerie c0a7488
Added structured logging using Python logging to mimic GCP logs
v-lerie 4408030
Added test_logger.py
v-lerie 69e71fc
Added test_logger.py
v-lerie d01e035
Added test_logger.py
v-lerie f7ad1e5
Added test_logger.py
v-lerie 26fa549
Changed log_text to log_struct in simulation_api.py
v-lerie f1a73c9
Changed log_text to log_struct in simulation_api.py
v-lerie 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 |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| - bump: patch | ||
| changes: | ||
| added: | ||
| - Implemented structured logging in policyengine_api/utils/logger.py to produce GCP-compatible JSON logs. |
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,49 @@ | ||
| import logging | ||
| import json | ||
| import sys | ||
| from datetime import datetime, timezone | ||
|
|
||
|
|
||
| class Logger: | ||
| def __init__(self, name="policyengine-api", level=logging.INFO): | ||
| self.logger = logging.getLogger(name) | ||
| self.logger.setLevel(level) | ||
|
|
||
| if not self.logger.handlers: | ||
| handler = logging.StreamHandler(sys.stdout) | ||
| handler.setFormatter(logging.Formatter("%(message)s")) | ||
| self.logger.addHandler(handler) | ||
|
|
||
| def log_struct( | ||
| self, payload: dict, severity: str = "INFO", message: str = None | ||
| ): | ||
| log_entry = { | ||
| "severity": severity.upper(), | ||
| "timestamp": datetime.now(timezone.utc) | ||
| .isoformat(timespec="milliseconds") | ||
| .replace("+00:00", "Z"), | ||
| } | ||
|
|
||
| if message: | ||
| log_entry["message"] = message | ||
|
|
||
| if isinstance(payload, dict): | ||
| log_entry.update(payload) | ||
|
|
||
| json_log = json.dumps(log_entry, ensure_ascii=False) | ||
| self._emit(severity, json_log) | ||
|
|
||
| def _emit(self, severity: str, msg: str): | ||
| level = severity.upper() | ||
| if level == "DEBUG": | ||
| self.logger.debug(msg) | ||
| elif level == "INFO": | ||
| self.logger.info(msg) | ||
| elif level == "WARNING": | ||
| self.logger.warning(msg) | ||
| elif level == "ERROR": | ||
| self.logger.error(msg) | ||
| elif level == "CRITICAL": | ||
| self.logger.critical(msg) | ||
| else: | ||
| self.logger.info(msg) | ||
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,3 @@ | ||
| from policyengine_api.utils.logger import Logger | ||
|
|
||
| logger = Logger("policyengine-api") |
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,14 @@ | ||
| from policyengine_api.utils.logger import Logger | ||
|
|
||
|
|
||
| def test_log_struct(capsys): | ||
| logger = Logger("test-logger") | ||
| logger.log_struct( | ||
| {"event": "test_event", "user_id": "abc123"}, | ||
| severity="INFO", | ||
| message="Logging from test", | ||
| ) | ||
| captured = capsys.readouterr() | ||
| assert '"severity": "INFO"' in captured.out | ||
| assert '"event": "test_event"' in captured.out | ||
| assert '"message": "Logging from test"' in captured.out |
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.
question, blocking: How did you test this?
My concern here is that we're looking to emit JSON to GCP, if possible. I'm not sure this code does that.