我会在运行开始时更改默认的python格式化程序。
import logging
logging.Formatter.format = my_format_function
我使用了相同的方法来覆盖
makeRecord
函数以包含更多元数据。
import logging
def myMakeRecord(self, name, level, fn, lno, msg, args, exc_info, func=None, extra=None, another=None):
"""
The normal python handler does not provide all the fields, so this enriches the message and formats it for logz.io.
"""
if extra is None:
extra = {}
extra.update({'message': msg,
'logger': name,
'line_number': lno,
'path_name': fn})
if func is not None:
extra.update({'func_name': func})
msg = json.dumps(extra)
extra = None
return original_makeRecord(self, name, level, fn, lno, msg, args, exc_info, func=func, extra=extra)
logging.Logger.makeRecord = myMakeRecord