其實質上的日誌條目被設計爲彼此獨立。
將它們連接在一起的正確方法是在條目中包含一些上下文信息,以便稍後查看日誌時進行過濾。
下面是與這樣的信息一個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
在Python logging
文檔,Adding contextual information to your logging output建議兩種方法:使用LoggerAdapter
或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>)
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')
正如你可以看到,不同的是LoggerAdapter
是不透明,而Filter
是透明的。在這些例子中,前者修改了消息文本,而後者設置了自定義屬性(並且實際上編寫它們需要使用Formatter
的合作),但事實上,兩者都可以同時執行。
因此,前者更有用,如果您只需要將上下文添加到某些消息,而後者更適合擴充所有或大部分被記錄的消息。
這種方法通常很有用,而且在您記錄到多個獨立日誌時很有必要,例如, Python日誌框架加上Web服務器日誌。如果沒有這些,很難在日誌間進行關聯。 – 2015-03-06 14:37:59