Skip to content
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

Add Traceback in LogRecord in JSONFormatter #15414

Merged
merged 1 commit into from
Apr 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions airflow/utils/log/json_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,16 @@ def usesTime(self):
def format(self, record):
super().format(record)
record_dict = {label: getattr(record, label, None) for label in self.json_fields}
if "message" in self.json_fields:
msg = record_dict["message"]
if record.exc_text:
if msg[-1:] != "\n":
msg = msg + "\n"
msg = msg + record.exc_text
if record.stack_info:
if msg[-1:] != "\n":
msg = msg + "\n"
msg = msg + self.formatStack(record.stack_info)
Comment on lines +47 to +55
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

record_dict["message"] = msg
merged_record = merge_dicts(record_dict, self.extras)
return json.dumps(merged_record)
18 changes: 18 additions & 0 deletions tests/utils/log/test_json_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
Module for all tests airflow.utils.log.json_formatter.JSONFormatter
"""
import json
import sys
import unittest
from logging import makeLogRecord

Expand Down Expand Up @@ -63,3 +64,20 @@ def test_format_with_extras(self):
json_fmt = JSONFormatter(json_fields=["label"], extras={'pod_extra': 'useful_message'})
# compare as a dicts to not fail on sorting errors
assert json.loads(json_fmt.format(log_record)) == {"label": "value", "pod_extra": "useful_message"}

def test_format_with_exception(self):
"""
Test exception is included in the message when using JSONFormatter
"""
try:
raise RuntimeError("message")
except RuntimeError:
exc_info = sys.exc_info()

log_record = makeLogRecord({"exc_info": exc_info, "message": "Some msg"})
json_fmt = JSONFormatter(json_fields=["message"])

log_fmt = json.loads(json_fmt.format(log_record))
assert "message" in log_fmt
assert "Traceback (most recent call last)" in log_fmt["message"]
assert 'raise RuntimeError("message")' in log_fmt["message"]