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 basicConfig, but then I get a lot of system debug messages being logged when all I want is my own messages beginning at the DEBUG level.
I am hoping to either (1) change the filemode for my own logger object to 'w' or (2) add a filter to the root logger. Is it even possible to filter out these debug messages from the root logger?
def create_log():
# 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')
fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler(stream=sys.stdout)
ch.setLevel(logging.DEBUG)
# 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