We love logging, and we use it all around our code. (it’s the standard/built-in/batteries included)
We have those hugh json objects flying around, we want to log them.
pprint.pformat comes handy for those cases:
but when used with complex formatter the output goes like that:
^14/11/06 00:58:39 !ERROR <t:MainThread T:root M:log.py F:<module> L:74 > Mary
had
a
little
lamb
^14/11/06 00:58:39 !ERROR <t:MainThread T:root M:log.py F:<module> L:75 > [{'data': 'my large inforamtion',
'data1': 'dicts, dicts, dicts,',
'data3': {'a': '??', 'b': 2, 'c': 3, 'd': '***'}},
{'data': 'my large inforamtion',
'data1': 'how we love big dicts ?',
'data3': {'a': '??', 'b': 2, 'c': 3, 'd': '***'}},
{'data': 'my large inforamtion',
'data1': "how we love big dicts ? don't we ?,",
'data3': {'a': '???', 'b': 2, 'c': 3, 'd': '***'}}]
^14/11/06 00:58:39 !ERROR <t:MainThread T:root M:log.py F:<module> L:76 > [{'data': 'my large inforamtion',
'data1': 'dicts, dicts, dicts,',
'data3': {'a': '??', 'b': 2, 'c': 3, 'd': '***'}},
{'data': 'my large inforamtion',
'data1': 'how we love big dicts ?',
'data3': {'a': '??', 'b': 2, 'c': 3, 'd': '***'}},
{'data': 'my large inforamtion',
'data1': "how we love big dicts ? don't we ?,",
'data3': {'a': '???', 'b': 2, 'c': 3, 'd': '***'}}]
Kinda, not really helpful:
- confusing - and you loose track of where this print came from really fast (keep in mind I’m talking about humongous json object)
- filtering is (almost) impossible (yeah, not without some extra regex kung-fu)
Skimmed through the logging documentations, google it a bit think someone already solved those things and I could copy paste, came out empty handed.
So here how it can be fixed:
and now our output is much much clearer:
^14/11/06 00:54:51 !ERROR <t:MainThread T:root M:log.py F:<module> L:74 > Mary
^14/11/06 00:54:51 !ERROR <t:MainThread T:root M:log.py F:<module> L:74 > had
^14/11/06 00:54:51 !ERROR <t:MainThread T:root M:log.py F:<module> L:74 > a
^14/11/06 00:54:51 !ERROR <t:MainThread T:root M:log.py F:<module> L:74 > little
^14/11/06 00:54:51 !ERROR <t:MainThread T:root M:log.py F:<module> L:74 > lamb
^14/11/06 00:54:51 !ERROR <t:MainThread T:root M:log.py F:<module> L:75 > [{'data': 'my large inforamtion',
^14/11/06 00:54:51 !ERROR <t:MainThread T:root M:log.py F:<module> L:75 > 'data1': 'dicts, dicts, dicts,',
^14/11/06 00:54:51 !ERROR <t:MainThread T:root M:log.py F:<module> L:75 > 'data3': {'a': '??', 'b': 2, 'c': 3, 'd': '***'}},
^14/11/06 00:54:51 !ERROR <t:MainThread T:root M:log.py F:<module> L:75 > {'data': 'my large inforamtion',
^14/11/06 00:54:51 !ERROR <t:MainThread T:root M:log.py F:<module> L:75 > 'data1': 'how we love big dicts ?',
^14/11/06 00:54:51 !ERROR <t:MainThread T:root M:log.py F:<module> L:75 > 'data3': {'a': '??', 'b': 2, 'c': 3, 'd': '***'}},
^14/11/06 00:54:51 !ERROR <t:MainThread T:root M:log.py F:<module> L:75 > {'data': 'my large inforamtion',
^14/11/06 00:54:51 !ERROR <t:MainThread T:root M:log.py F:<module> L:75 > 'data1': "how we love big dicts ? don't we ?,",
^14/11/06 00:54:51 !ERROR <t:MainThread T:root M:log.py F:<module> L:75 > 'data3': {'a': '???', 'b': 2, 'c': 3, 'd': '***'}}]
^14/11/06 00:54:51 !ERROR <t:MainThread T:root M:log.py F:<module> L:76 > [{'data': 'my large inforamtion',
^14/11/06 00:54:51 !ERROR <t:MainThread T:root M:log.py F:<module> L:76 > 'data1': 'dicts, dicts, dicts,',
^14/11/06 00:54:51 !ERROR <t:MainThread T:root M:log.py F:<module> L:76 > 'data3': {'a': '??', 'b': 2, 'c': 3, 'd': '***'}},
^14/11/06 00:54:51 !ERROR <t:MainThread T:root M:log.py F:<module> L:76 > {'data': 'my large inforamtion',
^14/11/06 00:54:51 !ERROR <t:MainThread T:root M:log.py F:<module> L:76 > 'data1': 'how we love big dicts ?',
^14/11/06 00:54:51 !ERROR <t:MainThread T:root M:log.py F:<module> L:76 > 'data3': {'a': '??', 'b': 2, 'c': 3, 'd': '***'}},
^14/11/06 00:54:51 !ERROR <t:MainThread T:root M:log.py F:<module> L:76 > {'data': 'my large inforamtion',
^14/11/06 00:54:51 !ERROR <t:MainThread T:root M:log.py F:<module> L:76 > 'data1': "how we love big dicts ? don't we ?,",
^14/11/06 00:54:51 !ERROR <t:MainThread T:root M:log.py F:<module> L:76 > 'data3': {'a': '???', 'b': 2, 'c': 3, 'd': '***'}}]
Disclaimer: use with caution, written past midnight :)
blog comments powered by Disqus