Python logging setLevel() not taking effect
<p>I have a python program that utilizes multiprocessing to increase efficiency, and a function that creates a logger for each process. The logger function looks like this:</p>

<pre><code>import logging
import os

def create_logger(app_name):
    """Create a logging interface"""
    # create a logger
    if logging in os.environ:
        logging_string = os.environ["logging"]
        if logging_string == "DEBUG":
            logging_level = loggin.DEBUG
        else if logging_string == "INFO":
            logging_level = logging.INFO
        else if logging_string == "WARNING":
            logging_level = logging.WARNING
        else if logging_string == "ERROR":
            logging_level = logging.ERROR
        else if logging_string == "CRITICAL":
            logging_level = logging.CRITICAL
    else:
        logging_level = logging.INFO

    logger = logging.getLogger(app_name)
    logger.setLevel(logging_level)

    # Console handler for error output
    console_handler = logging.StreamHandler()
    console_handler.setLevel(logging_level)

    # Formatter to make everything look nice
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    console_handler.setFormatter(formatter)

    # Add the handlers to the logger
    logger.addHandler(console_handler)

    return logger
</code></pre>

<p>And my processing functions look like this:</p>

<pre><code>import custom_logging

def do_capture(data_dict_access):
    """Process data"""

    # Custom logging
    LOGGER = custom_logging.create_logger("processor")

    LOGGER.debug("Doing stuff...")
</code></pre>

<p>However, no matter what the logging environment variable is set to, I still receive debug log messages in the console. Why is my logging level not taking effect, surely the calls to setLevel() should stop the debug messages from being logged?</p>

<p>Here is an easy way to create a logger object:</p>

<pre><code>import logging
import os

def create_logger(app_name):
    """Create a logging interface"""
    logging_level = os.getenv('logging', logging.INFO)
    logging.basicConfig(
        level=logging_level,
        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    logger = logging.getLogger(app_name)
    return logger
</code></pre>

<h1>Discussion</h1>

<ul>
<li>There is no need to convert from <em>"DEBUG"</em> to <code>logging.DEBUG</code>, the <code>logging</code> module understands these strings.</li>
<li>Use <code>basicConfig</code> to ease the pain of setting up a logger. You don't need to create handler, set format, set level, ... This should work for most cases.</li>
</ul>

<h1>Update</h1>

<p>I found out why your code does not work, besides the <code>else if</code>. Consider your line:</p>

<pre><code>if logging in os.environ:
</code></pre>

<p>On this line <code>loggging</code> without quote refers to the <code>logging</code> library package. What you want is:</p>

<pre><code>if 'logging' in os.environ:
</code></pre>

python logging python-multiprocessing