Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python logging setLevel() not taking effect

Tags:

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:

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

And my processing functions look like this:

import custom_logging

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

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

    LOGGER.debug("Doing stuff...")

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?

like image 615
Foxocube Avatar asked Sep 30 '16 14:09

Foxocube


1 Answers

Here is an easy way to create a logger object:

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

Discussion

  • There is no need to convert from "DEBUG" to logging.DEBUG, the logging module understands these strings.
  • Use basicConfig 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.

Update

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

if logging in os.environ:

On this line loggging without quote refers to the logging library package. What you want is:

if 'logging' in os.environ:
like image 160
Hai Vu Avatar answered Sep 25 '22 16:09

Hai Vu