-
Notifications
You must be signed in to change notification settings - Fork 429
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
fix: Setting LOG_FORMAT: json
does not write stack traces to logs
#4040
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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,28 +1,70 @@ | ||
import json | ||
import logging | ||
import os | ||
|
||
import pytest | ||
|
||
from util.logging import JsonFormatter | ||
|
||
|
||
def test_json_formatter__outputs_expected(): | ||
@pytest.fixture | ||
def inspecting_handler() -> logging.Handler: | ||
class InspectingHandler(logging.Handler): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.messages = [] | ||
|
||
def handle(self, record): | ||
self.messages.append(self.format(record)) | ||
|
||
return InspectingHandler() | ||
|
||
|
||
@pytest.mark.freeze_time("2023-12-08T06:05:47.320000+00:00") | ||
def test_json_formatter__outputs_expected(inspecting_handler: logging.Handler) -> None: | ||
# Given | ||
json_formatter = JsonFormatter() | ||
|
||
log_record = logging.LogRecord( | ||
name="test_logger", | ||
level=logging.INFO, | ||
pathname="test.py", | ||
lineno=42, | ||
msg="This is a test message with args: %s and %s", | ||
args=("arg1", "arg2"), | ||
exc_info=None, | ||
inspecting_handler.setFormatter(json_formatter) | ||
logger = logging.getLogger("test_json_formatter__outputs_expected") | ||
logger.addHandler(inspecting_handler) | ||
logger.setLevel(logging.INFO) | ||
|
||
expected_pid = os.getpid() | ||
expected_tb_string = ( | ||
"Traceback (most recent call last):\n" | ||
' File "/Users/kgustyr/dev/flagsmith/flagsmith/api/tests/unit/util/test_logging.py",' | ||
" line 43, in _log_traceback\n" | ||
" raise Exception()\nException" | ||
) | ||
formatted_message = json_formatter.format(log_record) | ||
json_message = json.loads(formatted_message) | ||
|
||
assert "levelname" in json_message | ||
assert "message" in json_message | ||
assert "timestamp" in json_message | ||
assert "logger_name" in json_message | ||
assert "process_id" in json_message | ||
assert "thread_name" in json_message | ||
assert json_message["message"] == "This is a test message with args: arg1 and arg2" | ||
|
||
def _log_traceback() -> None: | ||
try: | ||
raise Exception() | ||
except Exception as exc: | ||
logger.error("this is an error", exc_info=exc) | ||
|
||
# When | ||
logger.info("hello %s, %d", "arg1", 22.22) | ||
_log_traceback() | ||
|
||
# Then | ||
assert [json.loads(message) for message in inspecting_handler.messages] == [ | ||
{ | ||
"levelname": "INFO", | ||
"message": "hello arg1, 22", | ||
"timestamp": "2023-12-08 06:05:47,319", | ||
"logger_name": "test_json_formatter__outputs_expected", | ||
"process_id": expected_pid, | ||
"thread_name": "MainThread", | ||
}, | ||
{ | ||
"levelname": "ERROR", | ||
"message": "this is an error", | ||
"timestamp": "2023-12-08 06:05:47,319", | ||
"logger_name": "test_json_formatter__outputs_expected", | ||
"process_id": expected_pid, | ||
"thread_name": "MainThread", | ||
"exc_info": expected_tb_string, | ||
}, | ||
] |
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
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.
Looks like you're going to need to fix the
$HOME
and work part of the filepath.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.
Thanks! Done.