Skip to content Skip to sidebar Skip to footer

Is There A Way To Change The Filemode For A Logger Object That Is Not Configured Using Basicconfig?

If I create a logger object by using logger = logging.getLogger('Name') I am unable to change the filemode from append('a') to write ('w'). I can if I use the root logger with basi

Solution 1:

Something like:

import sys
import logging

def create_logger():
    # create logger for "Sample App"
    logger = logging.getLogger('automated_testing')
    logger.setLevel(logging.DEBUG)

    # create file handler which logs even debug messages
    fh = logging.FileHandler('results.log', mode='w')
    fh.setLevel(logging.DEBUG)

    # create console handler with a higher log level
    ch = logging.StreamHandler(stream=sys.stdout)
    ch.setLevel(logging.INFO)

    # create formatter and add it to the handlers
    formatter = logging.Formatter('[%(asctime)s] %(levelname)8s --- %(message)s ' +
                                  '(%(filename)s:%(lineno)s)',datefmt='%Y-%m-%d %H:%M:%S')
    fh.setFormatter(formatter)
    ch.setFormatter(formatter)

    # add the handlers to the logger
    logger.addHandler(ch)
    logger.addHandler(fh)

    return logger

logger = create_logger()


logger.log(logging.NOTSET,   "NOTSET   Message - 0")
logger.log(logging.DEBUG,    "DEBUG    Message - 10")
logger.log(logging.INFO,     "INFO     Message - 20")
logger.log(logging.WARNING,  "WARNING  Message - 30")
logger.log(logging.CRITICAL, "CRITICAL Message - 40")

Prints to stdout:

[2015-03-16 17:51:08]     INFO --- INFO     Message - 20 (temp3.py:34)
[2015-03-16 17:51:08]  WARNING --- WARNING  Message - 30 (temp3.py:35)
[2015-03-16 17:51:08] CRITICAL --- CRITICAL Message - 40 (temp3.py:36)

Writes (not appends) to results.log:

[2015-03-16 17:51:08]    DEBUG --- DEBUG    Message - 10 (temp3.py:33)
[2015-03-16 17:51:08]     INFO --- INFO     Message - 20 (temp3.py:34)
[2015-03-16 17:51:08]  WARNING --- WARNING  Message - 30 (temp3.py:35)
[2015-03-16 17:51:08] CRITICAL --- CRITICAL Message - 40 (temp3.py:36)

DEBUG+ are logged in results.txt while only INFO+ are send to stdout.

Note that the NOTSET log entry is passed up to the root logger then, since you don't have any handlers on the root logger, discarded.

Post a Comment for "Is There A Way To Change The Filemode For A Logger Object That Is Not Configured Using Basicconfig?"