def emit(self, record):
"""Pretty-print and syntax highlight a log statement if all these conditions are met:
* This is a DEBUG message
* We're outputting to a terminal
* The log message args is a dict containing keys starting with 'xml_' and values as bytes
"""
if record.levelno == logging.DEBUG and self.is_tty() and isinstance(record.args, dict):
for key, value in record.args.items():
if not key.startswith('xml_'):
continue
if not isinstance(value, bytes):
continue
if not is_xml(value[:10].decode('utf-8', errors='ignore')):
continue
try:
if PY2:
record.args[key] = self.highlight_xml(self.prettify_xml(value)).encode('utf-8')
else:
record.args[key] = self.highlight_xml(self.prettify_xml(value))
except Exception:
# Something bad happened, but we don't want to crash the program just because logging failed
pass
return super(PrettyXmlHandler, self).emit(record)
评论列表
文章目录