Python 在WSGI中重写日志类不起作用
我已经为django站点实现了自己的自定义记录器:Python 在WSGI中重写日志类不起作用,python,logging,Python,Logging,我已经为django站点实现了自己的自定义记录器: class SpecialLogger(logging.Logger): def __init__(self, name, level=logging.DEBUG): logging.Logger.__init__(self, name, level) def special(self, msg, *args, **kwargs): self.log(self.level, msg, *args
class SpecialLogger(logging.Logger):
def __init__(self, name, level=logging.DEBUG):
logging.Logger.__init__(self, name, level)
def special(self, msg, *args, **kwargs):
self.log(self.level, msg, *args, **kwargs)
在我的wsgi文件中,我添加了以下行:
import logging
from testbed.utils import SpecialLogger
logging.setLoggerClass(SpecialLogger)
在我的django观点中:
1 import logging
2 logger = logging.getLogger('mylog')
3 print str(logging.getLoggerClass())
4 print str(logger)
5 print dir(logger)
第3行打印testbed.utils.SpecialLogger
第4行打印
第5行(当然)没有显示我的功能special
我做错了什么?为什么第3行和第4行之间存在差异?setLoggerClass函数设置用于在调用返回后实例化的所有记录器的类,但在调用之前创建的任何记录器仍将是logging.Logger。您可以在wsgi文件中附加一行,以确认正在使用正确的类,例如
import logging
from testbed.utils import SpecialLogger
logging.setLoggerClass(SpecialLogger)
logger = logging.getLogger('mylog') # should be a testbed.utils.SpecialLogger
如果这不能产生所需的效果,则在上述代码运行之前,其他一些代码(例如在
settings.py
)必须实例化记录器。3只是打印记录器正在使用的类。4正在打印记录器对象。第五章的内容是什么?您的问题是为什么这些不同,或者为什么特殊功能不可用?基本上,我想知道为什么特殊功能缺失。我通过将setLoggerClass
方法在myapp.wsgi
文件中移高几行来解决我的问题。现在它就像一个符咒。