Fix Python – Set logging levels


Asked By – Luis Ramon Ramirez Rodriguez

I’m trying to use the standard library to debug my code:

This works fine:

import logging
logger = logging.getLogger(__name__)'message')

I can’t make work the logger for the lower levels:

logger = logging.getLogger(__name__)'message')

logger = logging.getLogger(__name__)

I don’t get any response for neither of those.

Now we will see solution for issue: Set logging levels


What Python version? That works for me in 3.4. But note that basicConfig() won’t affect the root handler if it’s already setup:

This function does nothing if the root logger already has handlers configured for it.

To set the level on root explicitly do logging.getLogger().setLevel(logging.DEBUG). But ensure you’ve called basicConfig() before hand so the root logger initially has some setup. I.e.:

import logging

Also note that “Loggers” and their “Handlers” both have distinct independent log levels. So if you’ve previously explicitly loaded some complex logger config in you Python script, and that has messed with the root logger’s handler(s), then this can have an effect, and just changing the loggers log level with logging.getLogger().setLevel(..) may not work. This is because the attached handler may have a log level set independently. This is unlikely to be the case and not something you’d normally have to worry about.

This question is answered By – spinkus

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0