0

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.INFO):
    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?

3
  • Does it work when using higher severities? Commented Jun 16, 2011 at 6:29
  • Yes it's like i say, when i use warning or error for example it does work, but i want for certain messages the INFO to work... i don't get it anymore... Commented Jun 16, 2011 at 6:38
  • Look, only the INFO is missing: Thu, 2011-06-16 08:45:03 test WARNING test warning Thu, 2011-06-16 08:45:03 test ERROR test error Thu, 2011-06-16 08:45:03 test CRITICAL test critical Commented Jun 16, 2011 at 6:45

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

indeed, it's related to what you say. The solution inside the views.py is as followed: create_log_file('/opt/test.log', 'testlog') logger_test = logging.getLogger('testlog') logger_test.setLevel(logging.INFO)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.