Python日志记录模块在“上引发KeyError异常”;“附加组件”;属性

Python日志记录模块在“上引发KeyError异常”;“附加组件”;属性,python,logging,Python,Logging,我有一个问题,我试图在Python CGI脚本上记录一些附加属性(用户ID和连接主机IP)。这是在RHEL 5系统上的Python2.6.8下运行的。下面是扩展基本日志字典中属性的文档,如下所示: from __future__ import print_function import logging import os import sys LOG_DIR = '/apps/log' LOG_PAGE = re.sub(r'\/(.*)\/.*$', r'\1', os.environ['R

我有一个问题,我试图在Python CGI脚本上记录一些附加属性(用户ID和连接主机IP)。这是在RHEL 5系统上的Python2.6.8下运行的。下面是扩展基本日志字典中属性的文档,如下所示:

from __future__ import print_function
import logging
import os
import sys

LOG_DIR = '/apps/log'
LOG_PAGE = re.sub(r'\/(.*)\/.*$', r'\1', os.environ['REQUEST_URI'])
#
# The log format should be 
# <date stamp>  <level>   <remote user> <remote IP>: <message>
#
LOG_FORMAT = '%(asctime)-19s %(levelname)-9s %(user)-7s %(clientip)-15s - %(message)s'
LOG_DATE = '%Y-%m-%d %H:%M:%S'
orig_user = os.environ['REMOTE_USER']
orig_ip = os.environ['REMOTE_ADDR']
xtras = { 'user': orig_user, 'clientip': orig_ip }
#
# Set up logging
#
LOG_FILE = LOG_DIR + '/' + LOG_PAGE
logging.basicConfig(format=LOG_FORMAT, datefmt=LOG_DATE, level=logging.DEBUG, filename=LOG_FILE)
logit = logging.getLogger('groups')
每次记录消息时,web服务器错误日志中将记录3个KeyError异常:

s = self._fmt % record.__dict__, referer: https://its-serv-dev.case.edu/googlegroups/index.cgi
KeyError: 'user', referer: https://its-serv-dev.case.edu/googlegroups/index.cgi
Traceback (most recent call last):, referer: https://its-serv-dev.case.edu/googlegroups/index.cgi
File "/usr/lib64/python2.6/logging/__init__.py", line 776, in emit, referer: https://its-serv-dev.case.edu/googlegroups/index.cgi
msg = self.format(record), referer: https://its-serv-dev.case.edu/googlegroups/index.cgi
File "/usr/lib64/python2.6/logging/__init__.py", line 654, in format, referer: https://its-serv-dev.case.edu/googlegroups/index.cgi
return fmt.format(record), referer: https://its-serv-dev.case.edu/googlegroups/index.cgi
File "/usr/lib64/python2.6/logging/__init__.py", line 439, in format, referer: https://its-serv-dev.case.edu/googlegroups/index.cgi
s = self._fmt % record.__dict__, referer: https://its-serv-dev.case.edu/googlegroups/index.cgi
KeyError: 'user', referer: https://its-serv-dev.case.edu/googlegroups/index.cgi
奇怪的是,KeyError异常只为一个“额外”字典项生成,并且信息正确地记录到文件中。我尝试了各种组合来删除两个额外的组件(错误只会转移到剩下的部分),除非我完全删除额外的信息,否则似乎没有什么可以阻止抛出异常


我想我可以将信息作为消息的一部分包含在一个格式字符串中,但这似乎是在重新发明轮子。

我明白了这里发生了什么:


我还导入了Google的oauth2client.client模块,该模块也在使用日志模块。由于oauth2cleint.client模块被视为我页面的“子”模块,日志被传递到我的日志对象,并且由于Google模块在其调用中不包括额外的日志字典,所以Google模块是生成KeyError异常的项,而不是我自己的代码。目前,我已经解决了这个问题,将额外的项目作为消息的一部分,并且需要进一步深入日志模块,看看是否有更好的方法来防止Google OAuth2Clean.client模块日志记录与页面日志记录冲突。

面临类似的问题。我猜当使用的记录器没有添加过滤器(在实例化记录器时添加额外属性),但仍然使用与这些属性对应的格式将记录传递给格式化程序时,就会发生错误


通过调用
logging.basicConfig
,您实际上更改了
RootLogger
格式化程序,它是所有
记录器的祖先。然后,它将影响其他
记录器


您可以从中获得更多详细信息。

您好,关于您的问题的任何更新。如果您找到解决方案,请分享您的答案:)
s = self._fmt % record.__dict__, referer: https://its-serv-dev.case.edu/googlegroups/index.cgi
KeyError: 'user', referer: https://its-serv-dev.case.edu/googlegroups/index.cgi
Traceback (most recent call last):, referer: https://its-serv-dev.case.edu/googlegroups/index.cgi
File "/usr/lib64/python2.6/logging/__init__.py", line 776, in emit, referer: https://its-serv-dev.case.edu/googlegroups/index.cgi
msg = self.format(record), referer: https://its-serv-dev.case.edu/googlegroups/index.cgi
File "/usr/lib64/python2.6/logging/__init__.py", line 654, in format, referer: https://its-serv-dev.case.edu/googlegroups/index.cgi
return fmt.format(record), referer: https://its-serv-dev.case.edu/googlegroups/index.cgi
File "/usr/lib64/python2.6/logging/__init__.py", line 439, in format, referer: https://its-serv-dev.case.edu/googlegroups/index.cgi
s = self._fmt % record.__dict__, referer: https://its-serv-dev.case.edu/googlegroups/index.cgi
KeyError: 'user', referer: https://its-serv-dev.case.edu/googlegroups/index.cgi