gue*_*tli 2 python logging httprequest
有没有办法对属于一个 Web 请求的 Python Web 应用程序的日志进行分组?
例子:
2015-02-11 13:06:32 myapp.middleware.MYAPPMiddleware: INFO Login of user foo was successful
2015-02-11 13:06:32 myapp.middleware.MYAPPMiddleware: INFO Login of user bar failed
2015-02-11 13:06:32 myapp.send_mails: INFO failed to send mail to someone@example.com
Run Code Online (Sandbox Code Playgroud)
以上日志行彼此无关。
你怎么能用pythonic的方式解决这个问题?
日志条目本质上被设计为彼此独立。
将它们连接在一起的正确方法是在条目中包含一些上下文信息,以便稍后查看日志时进行过滤。
以下是包含此类信息的 Sharepoint 日志记录示例:
Timestamp Process TID Area Category EventID Level Message Correlation
02/26/2015 17:49:19.65 w3wp.exe (0x1F40) 0x2358 SharePoint Foundation Logging Correlation Data xmnv Medium Name=Request (POST:http://reserver2:80/pest/_vti_bin/sitedata.asmx) d1e2b688-e0b2-481e-98ce-497a11acab44
Run Code Online (Sandbox Code Playgroud)
在 Pythonlogging文档中,向日志输出中添加上下文信息推荐了两种方法之一:使用 aLoggerAdapter或 a Filter。
LoggerAdapter 像这样使用(示例基于文档中的示例):
class AddConnIdAdapter(logging.LoggerAdapter):
def process(self, msg, kwargs):
return <augment_message(msg,arbitrary_info)>, kwargs
la = AddConnIdAdapter(<logger>,extra=<parameters, saved in self.extra>)
<...>
la.info(<message>)
Run Code Online (Sandbox Code Playgroud)
Filter 是这样使用的:
#Either all messages should have custom fields
# or the Formatter used should support messages
# both with and without custom fields
logging.basicConfig(<...>,format='%(asctime)-15s %(name)-5s %(levelname)-8s IP: %(ip)-15s User: %(user)-8s %(message)s')
class AddClientInfo(logging.Filter):
#override __init__ or set attributes to specify parameters
def filter(self, record):
record.ip = <get_client_ip()>
record.user = <get_client_name()>
return True #do not filter out anything
l=<logger()>
l.addFilter(AddClientInfo()) #can attach to either loggers or handlers
<...>
l.info('message')
Run Code Online (Sandbox Code Playgroud)
如您所见,不同之处LoggerAdapter在于不透明而Filter透明。在示例中,前者修改消息文本,后者设置自定义属性(实际上编写它们需要使用的配合Formatter)但实际上两者都可以。
因此,如果您只需要向某些消息添加上下文,则前者更有用,而后者更适合扩充所有或大部分记录的消息。