重写WSGI中的日志类

2024-04-25 08:57:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经为我的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, **kwargs)

在我的wsgi文件中,我添加了以下行:

^{pr2}$

在我的django看来:

1  import logging
2  logger = logging.getLogger('mylog')
3  print str(logging.getLoggerClass())
4  print str(logger)
5  print dir(logger)

第三行打印testbed.utils.SpecialLogger

第4行打印<logging.Logger instance at 0x21aadcac>

第5行(当然)没有显示我的函数special

我做错了什么?为什么第3行和第4行之间存在差异?在


Tags: djangonameselfinitloggingdefargsmsg
1条回答
网友
1楼 · 发布于 2024-04-25 08:57:07

setLoggerClass函数设置在调用返回后实例化的所有记录器所使用的类,但在调用之前创建的任何记录器仍将日志记录。记录器. 您可以在wsgi文件中附加一行来确认使用的类是正确的,例如

import logging
from testbed.utils import SpecialLogger
logging.setLoggerClass(SpecialLogger)
logger = logging.getLogger('mylog') # should be a testbed.utils.SpecialLogger

如果这不能产生所需的效果,那么在运行上述代码之前,其他一些代码(例如,settings.py)必须实例化记录器。在

相关问题 更多 >