开发者

django/python logging

I have a pretty weird problem with my logging facility i use inside django/python. The logging is not working anymore since i upgraded to django 1.3. It seems to be related to the logging level and the 'debug=' setting in the settings.py file.

1) When i log INFO messages and debug=False, the logging won't happen, my file doesn't get appended. 2) When i log WARNING messages and debug=False, the logging works perfectly like i want it to, the file gets appended 3) When i log INFO messages and debug=True, the logging seems to work, the file get appended.

How could i log INFO messages with debug=False? It worked before django 1.3... is there somewhere a mysterious setting which do the trick? Underneath there is a sample code:

views.py:

import logging

logging.basicConfig(level=logging.INFO,
                format='%(asctime)s %(levelname)s %(message)s',
                filename='/opt/general.log',
                filemode='a')


def create_log_file(filename, log_name, level=logging.I开发者_运维百科NFO):
    handler = logging.handlers.TimedRotatingFileHandler(filename, 'midnight', 7, backupCount=10)
    handler.setLevel(level)
    formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s', '%a, %Y-%m-%d %H:%M:%S')
    handler.setFormatter(formatter)
    logging.getLogger(log_name).addHandler(handler)

create_log_file('/opt/test.log', 'testlog')

logger_test = logging.getLogger('testlog')

logger_test.info('testing the logging functionality')

With this code the logging does not work in Django 1.3 with debug set to False in the settings.py file. When i should do like this:

logger_test.warning('testing the logging functionality')

This works perfectly when debug is set to False. The levels DEBUG and INFO aint logging but WARNING,ERROR and CRITICAL are doing their job...

Does anyone have an idea?


Since Django 1.3 contains its own logging configuration, you need to ensure that anything you're doing doesn't clash with it. For example, if the root logger has handlers already configured by Django by the time your module first gets imported, your basicConfig() call won't have any effect.

What you're describing is the normal logging situation - WARNINGs and above get handled, while INFO and DEBUG are suppressed by default. It looks as if your basicConfig() is not having any effect; You should consider replacing your basicConfig() call with the appropriate logging configuration in settings.py, or at least investigate the root logging level and what handlers are attached to it, at the time of your basicConfig() call.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜