展开全部
extra是用户自定义的dict. 这些key/value在格式化的时候可以直接引用。
extra可以用来传递额外的日志信息,尤其是上下32313133353236313431303231363533e78988e69d8331333339656531文信息。
例如:FORMAT = '%(asctime)-15s %(clientip)s %(user)-8s %(message)s'
logging.basicConfig(format=FORMAT)
d = { 'clientip' : '192.168.0.1', 'user' : 'fbloggs' }
logger = logging.getLogger('tcpserver')
logger.warning('Protocol problem: %s', 'connection reset', extra=d)
这里除了protocol错误描述外,还附加了客户IP和用户名信息。
如果配置了一些非文本格式的handler,结构化的数据会更容易存储和查询。
例如,Sentry的logging handler允许用户用extra.data来传递任意信息,并自动记录到web界面。logger.error('There was some crazy error', exc_info=True, extra={
'culprit': 'my.view.name',
'fingerprint': [...],
'data': {
# You may specify any values here and Sentry will log and output them
'username': request.user.username,
}
})