Skip to content

Commit

Permalink
Fix empty asctime field in JSON formatted logs (apache#10515)
Browse files Browse the repository at this point in the history
(cherry picked from commit 2aec99c)
  • Loading branch information
rgrizzell authored and kaxil committed Nov 27, 2020
1 parent aa13576 commit 21a9bd0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions airflow/utils/log/json_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ def __init__(self, fmt=None, datefmt=None, json_fields=None, extras=None):
self.json_fields = json_fields
self.extras = extras

def usesTime(self):
return self.json_fields.count('asctime') > 0

def format(self, record):
super(JSONFormatter, self).format(record)
record_dict = {label: getattr(record, label, None)
Expand Down
9 changes: 9 additions & 0 deletions tests/utils/log/test_json_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ def test_merge_dicts_recursive_right_only(self):
merged = merge_dicts(dict1, dict2)
self.assertDictEqual(merged, {'a': 1, 'r': {'b': 0, 'c': 3}})

def test_uses_time(self):
"""
Test usesTime method from JSONFormatter
"""
json_fmt_asctime = JSONFormatter(json_fields=["asctime", "label"])
json_fmt_no_asctime = JSONFormatter(json_fields=["label"])
self.assertTrue(json_fmt_asctime.usesTime())
self.assertFalse(json_fmt_no_asctime.usesTime())

def test_format(self):
"""
Test format method from JSONFormatter
Expand Down

0 comments on commit 21a9bd0

Please sign in to comment.