I'm writing some code that uses the python logging
system. The idea is that if the LOG
doesn't already exist create the log but if it does then get the log and resume logging to that file. Here is my code:
import logging
import os
log_filename='Transactions.log')
if os.path.isfile(log_filename)!=True:
LOG = logging.getLogger('log_filename')
LOG.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
fh = logging.FileHandler('log_filename')
fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# create formatter and add it to the handlers
formatter = logging.Formatter('-->%(asctime)s - %(name)s:%(levelname)s - %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
# add the handlers to the logger
LOG.addHandler(fh)
LOG.addHandler(ch)
else:
LOG=logging.getLogger()
I suspect the problem is with my else
block but I don't know how to fix. Could anybody shed some light on this situation.
LOG
from any place you want, no need to check if the log file exists – Rudie