开发者

Overriding the logging class in WSGI does not work

I have implemented my own custom Logger for my django site:

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)

In my wsgi file, I have added the lines:

import logging
from testbed.utils import SpecialLogger
logging.setLoggerClass(SpecialLogger)

In my django views:

1  import logging
2  logger = logging.getLogger('mylog')
3  print str(logging.getLoggerClass())
4  print str(logg开发者_高级运维er)
5  print dir(logger)

Line #3 prints testbed.utils.SpecialLogger

Line #4 prints <logging.Logger instance at 0x21aadcac>

And Line #5 (of course) does not show my function special

What have I done wrong ? Why is there a discrepancy between lines 3 & 4 ?


The setLoggerClass function sets the class to be used for all loggers instantiated after the call returns, but any loggers created before the call will still be logging.Logger. You could append a line in your wsgi file to confirm that the correct class is being used, e.g.

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

If this does not produce the desired effect, some other code (e.g. in settings.py) must be instantiating loggers before the above code runs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜