12

I have the following logging configuration in in my Django settings.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format':
              '%(levelname)s|%(asctime)s|%(name)s>> %(message)s',
        },
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose',
        }
    },
    'loggers': {
         '': {
            'handlers': ['console'],
            'level': 'ERROR',
            'propagate': True,
         },
        'apps': {
            'handlers': ['console'],
            'level': 'DEBUG',
        },
    }
}

With this configuration I expect my 'apps' to log at DEBUG level and any other modules to log only ERROR and above. But I see DEBUG messages from other modules. How do I fix it?

2 Answers 2

11

Are you using an empty string key in LOGGING['loggers'] to match the root logger? If so, you could try this instead.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format':
              '%(levelname)s|%(asctime)s|%(name)s>> %(message)s',
        },
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose',
        }
    },
    'loggers': {
        'apps': {
            'handlers': ['console'],
            'level': 'DEBUG',
        }
    },
    'root': {
       'handlers': ['console'],
       'level': 'ERROR'
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

I tried this but it did not work. Still seeing DEBUG messages from other modules.
@Chamindu did you try changing '' to 'root' instead of moving it out of loggers? (I'm a bit late to the party, sorry!)
I did not try it. This was from a long time back I don't even remember where i tried to do this :)
I set the root and it helped. But actually I cannot explain to myself why does root has to be configured? I mean what's its purpose.
I had the opposite problem: using 'root' it showed only WARNING and above, while setting it to the empty string showed also DEBUG and INFO messages.
|
1

I had a similar issue, with root logger configured a level at INFO but seeing DEBUG log message.

Turns out I should not set 'propagate': True for my other logger which is at level DEBUG, since that those logs will be passed to the root logger no matter what level root at.

So my guess here to the original question is that there might be some other modules' logger with propagate turned on. set disable_existing_loggers to True maybe solve this.

Comments

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.