Skip to content

Commit

Permalink
Merge pull request #32 from romain-dartigues/suggestions
Browse files Browse the repository at this point in the history
Suggestions: second are lost in default date serialization + XML test broken
  • Loading branch information
Zakaria Zajac committed Jun 12, 2016
2 parents 679474d + b789183 commit 6425c16
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 11 deletions.
9 changes: 1 addition & 8 deletions src/pythonjsonlogger/jsonlogger.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,8 @@ def __init__(self, *args, **kwargs):
if not self.json_encoder and not self.json_default:
def _default_json_handler(obj):
'''Prints dates in ISO format'''
if isinstance(obj, datetime.datetime):
if obj.year < 1900:
# strftime do not work with date < 1900
return obj.isoformat()
return obj.strftime(self.datefmt or '%Y-%m-%dT%H:%M')
elif isinstance(obj, datetime.date):
if isinstance(obj, (datetime.date, datetime.time)):
return obj.isoformat()
elif isinstance(obj, datetime.time):
return obj.strftime('%H:%M')
elif istraceback(obj):
tb = ''.join(traceback.format_tb(obj))
return tb.strip()
Expand Down
6 changes: 3 additions & 3 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ def testJsonDefaultEncoder(self):
"otherdatetimeagain": datetime.datetime(1900, 1, 1)}
self.logger.info(msg)
logJson = json.loads(self.buffer.getvalue())
self.assertEqual(logJson.get("adate"), "1999-12-31T23:59")
self.assertEqual(logJson.get("adate"), "1999-12-31T23:59:00")
self.assertEqual(logJson.get("otherdate"), "1789-07-14")
self.assertEqual(logJson.get("otherdatetime"), "1789-07-14T23:59:00")
self.assertEqual(logJson.get("otherdatetimeagain"),
"1900-01-01T00:00")
"1900-01-01T00:00:00")

def testJsonCustomDefault(self):
def custom(o):
Expand Down Expand Up @@ -175,7 +175,7 @@ def testExcInfo(self):
if len(sys.argv[1:]) > 0:
if sys.argv[1] == 'xml':
testSuite = unittest.TestLoader().loadTestsFromTestCase(
testJsonLogger)
TestJsonLogger)
xmlrunner.XMLTestRunner(output='reports').run(testSuite)
else:
unittest.main()

0 comments on commit 6425c16

Please sign in to comment.