Skip to content

Commit

Permalink
Fix empty asctime field in JSON formatted logs (#10515)
Browse files Browse the repository at this point in the history
  • Loading branch information
rgrizzell authored Sep 16, 2020
1 parent eff1525 commit 2aec99c
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 @@ -40,6 +40,9 @@ def __init__(self, fmt=None, datefmt=None, style='%', json_fields=None, extras=N
self.json_fields = json_fields
self.extras = extras

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

def format(self, record):
super().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 @@ -37,6 +37,15 @@ def test_json_formatter_is_not_none(self):
json_fmt = JSONFormatter()
self.assertIsNotNone(json_fmt)

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 2aec99c

Please sign in to comment.