11

For some reason, logging.basicConfig is ignoring the logger level that I'm giving it. The following code is in a script of mine:

# main.py print logging.getLogger().getEffectiveLevel() print logger_level logging.basicConfig(format=logger_format, level=logger_level, ) print logging.getLogger().getEffectiveLevel() 

The output is:

30 10 30 

So, it's not actually setting the logger level. However, if I execute this exact code in an interactive interpreter, I get the following output:

30 10 10 

Which means I'm setting the logging level just fine. Even more confusing, this is a recent occurrence – I don't know what I did that caused this behaviour. Can anyone provide some insight?

Edit: In case of related questions, I've tried running main.py both from my IDE (Sublime Text) as well as from the command line, both with the same result. I'm working in a conda-based virtual environment, and I've tried running the code both in the environment and not (but only from the interpreter - the script can't handle the general environment) with the same result.

0

2 Answers 2

19

From the python logging module docs,

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

My assumption is that you called basicConfig() prior, which installs handlers.

Try setting the loglevel directly on the root logger.

logging.getLogger().setLevel(logger_level) 

If you want to change the formatting, then you'll need to give the handler a new formatter. The following might work, assuming a handler has already been added to the root logger.

logging.getLogger().handlers[0].setFormatter(logging.Formatter(logger_format)) 
Sign up to request clarification or add additional context in comments.

3 Comments

logging.getLogger().setLevel(logger_level) definitely worked. I must be inadvertently calling logging.info or something similar in some other file before main.py loads. Thanks!
I read that there's a force parameter on basicConfig. However it doesn't seem to work. The docs seem to indicate that it would force the configuration on the root logger. Oh I just realized that force is only in Python 3.8 and not in Python 3.7.
In my case, I was using the tornado.log module in addition to python's logging. It was overwriting the level setting, so I rearranged my code and it works now.
11

An issue I (stupidly) had was not putting logging.basicConfig() at the top of the main file.

From the docs:

The above module-level convenience functions (debug, info, warn, error, fatal), which delegate to the root logger, call basicConfig() to ensure that at least one handler is available.

This function (basicConfig()) does nothing if the root logger already has handlers configured, unless the keyword argument force is set to True.

I called a function which logged a message before calling logging.BasicConfig() in my script, so the logger's level was set to the default, rather than the level I wanted.

If it's not possible to move the logger higher in the file, you have two options:

Python 3.8 and above:

logging.basicConfig(level=logging.INFO, force=True) 

Python 2.x and Python 3.x:

logging.getLogger().setLevel(logger_level) 

To see basicConfig() overwriting in action, try this:

import logging logging.info("Can't see me!") logging.warn("Peekabo") logging.basicConfig(level=logging.INFO) logging.info("Hidden again!") logging.basicConfig(level=logging.INFO, force=True) logging.info("You found me!") 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.