skip to Main Content

Under Ubuntu I experiencetd that the usage of different debug levels for loggers to stdout and to file is not possible. In my Windows environment (Spyder with Anacosna) it works as expected. Please see my example code. Anyone has an idea what’s wrong here?

This is my example code:

import logging
#from logging.handlers import RotatingFileHandler
logfile = "log.log"
levels =('NOTSET', 'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL')
dateformat = '%y-%m-%d %H:%M:%S'
strformat = '%(asctime)s %(levelname)s in %(funcName)s at %(lineno)s: %(message)s'
formatter = logging.Formatter(strformat,datefmt=dateformat)

for handler in logging.root.handlers[:]:
    logging.root.removeHandler(handler)

logging.basicConfig(
    format = strformat,
    datefmt = dateformat,
    force = True,
    level = logging.WARNING,
    )
if logfile:
    print(f"Creating {logfile}")
    fh = logging.FileHandler(logfile)
    #fh = RotatingFileHandler(logfile, maxBytes=(1000), backupCount=1)
    fh.setLevel(logging.DEBUG)
    fh.setFormatter(formatter)
    logging.getLogger('').addHandler(fh)
logging.debug(f"logging level is set to {levels[logging.root.level//10]}")
logging.warning("level warning test")

I would expect to see the debug level message in the file but this is not the case.
I see the warning in both, stdout and the file.

What am I doing wrong?

Do you need the logging module versions? How can I display the version?

2

Answers


  1. Chosen as BEST ANSWER

    I changed the code according to @Vinay's answer. Now I find the expected behavior

    import logging
    from logging.handlers import RotatingFileHandler
    logfile = "log.log"
    levels =('NOTSET', 'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL')
    dateformat = '%y-%m-%d %H:%M:%S'
    strformat = '%(asctime)s %(levelname)s in %(funcName)s:%(lineno)s: %(message)s'
    formatter = logging.Formatter(strformat,datefmt=dateformat)
    
    logging.basicConfig(
            format = strformat,
            datefmt = dateformat,
            force = True,
            level = logging.DEBUG,
            )
    
    logger = logging.getLogger('test_app')
    for handler in logger.root.handlers[:]:
            logger.root.removeHandler(handler)
    
    ch = logging.StreamHandler()
    ch.setLevel(logging.WARNING)
    ch.setFormatter(formatter)
    logger.addHandler(ch)
    if logfile:
            #fh = logging.FileHandler(logfile)
            fh = RotatingFileHandler(logfile, maxBytes=(1000), backupCount=3)
            fh.setLevel(logging.DEBUG)
            fh.setFormatter(formatter)
            logger.addHandler(fh)
    
    logger.debug(f"logging level is set to {levels[logging.root.level//10]}")
    logger.debug("in logfile only")
    logger.warning("In both, console and logfile")
    logger.error("in both")
    
    logger2=logging.getLogger('test_app')
    logger2.debug("logger2 in logfile only")
    logger2.warning("logger2 in both, console and logfile")
    
    
    

  2. You need to look at the flow of information in logging, documented here. Since the root logger’s level is set to WARNING, it will not process DEBUG or INFO events, no matter what the levels are for any of the handlers attached to it.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search