-
Notifications
You must be signed in to change notification settings - Fork 429
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Issue 166 json formatted logs (#3376)
Co-authored-by: Alek Jouharyan <[email protected]>
- Loading branch information
1 parent
aa1d6bb
commit c666d29
Showing
4 changed files
with
102 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import json | ||
import logging | ||
|
||
from util.logging import JsonFormatter | ||
|
||
|
||
def test_json_formatter(): | ||
json_formatter = JsonFormatter() | ||
|
||
log_record = logging.LogRecord( | ||
name="test_logger", | ||
level=logging.INFO, | ||
pathname="test.py", | ||
lineno=42, | ||
msg="This is a test.", | ||
args=(), | ||
exc_info=None, | ||
) | ||
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 | ||
|
||
|
||
def test_json_formatter_with_old_style_placeholders(): | ||
json_formatter = JsonFormatter() | ||
|
||
log_record = logging.LogRecord( | ||
name="test_logger", | ||
level=logging.INFO, | ||
pathname="example.py", | ||
lineno=42, | ||
msg="This is a test with old-style placeholders: %s and %s", | ||
args=("arg1", "arg2"), | ||
exc_info=None, | ||
) | ||
|
||
formatted_message = json_formatter.format(log_record) | ||
parsed_json = json.loads(formatted_message) | ||
assert ( | ||
parsed_json["message"] | ||
== "This is a test with old-style placeholders: arg1 and arg2" | ||
) | ||
|
||
|
||
def test_json_formatter_arguments_with_new_style_placeholders(): | ||
json_formatter = JsonFormatter() | ||
log_record = logging.LogRecord( | ||
name="test_logger", | ||
level=logging.INFO, | ||
pathname="example.py", | ||
lineno=42, | ||
msg="This is a test with new-style placeholders: {} and {}", | ||
args=("arg1", "arg2"), | ||
exc_info=None, | ||
) | ||
|
||
formatted_message = json_formatter.format(log_record) | ||
parsed_json = json.loads(formatted_message) | ||
assert ( | ||
parsed_json["message"] | ||
== "This is a test with new-style placeholders: arg1 and arg2" | ||
) |
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,27 @@ | ||
import json | ||
import logging | ||
|
||
|
||
class JsonFormatter(logging.Formatter): | ||
"""Custom formatter for json logs.""" | ||
|
||
def format(self, record): | ||
""" | ||
%s is replaced with {} because legacy string formatting | ||
conventions in django-axes module prevent correct | ||
interpolation of arguments when using this formatter. | ||
""" | ||
try: | ||
log_message = record.msg.replace("%s", "{}") | ||
formatted_message = log_message.format(*record.args) | ||
log_record = { | ||
"levelname": record.levelname, | ||
"message": formatted_message, | ||
"timestamp": self.formatTime(record, self.datefmt), | ||
"logger_name": record.name, | ||
"process_id": record.process, | ||
"thread_name": record.threadName, | ||
} | ||
return json.dumps(log_record) | ||
except (ValueError, TypeError) as e: | ||
return f"Error formatting log record: {str(e)}" |
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