I have a Python 3.4 application that uses logging extensively. I have two FileHandler
s and a StreamHandler
registered. Everything works as expected except that sometimes, and it seems to happen after the requests
library throws an exception, the log files lose all the accumulated messages and start with new messages. I'm assuming that for some reason the FileHandler
s reopened the files with mode='w'
, but I don't understand why. Any ideas?
The main program sets up the loggers as follows:
# Set up root logger - two handlers logging to files
fh_debug = logging.FileHandler('Syncer.debug', mode='w', encoding='utf-8')
fh_debug.setLevel(logging.DEBUG)
fh_log = logging.FileHandler('Syncer.log', mode='w', encoding='utf-8')
fh_log.setLevel(logging.INFO)
fh_formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
fh_debug.setFormatter(fh_formatter)
fh_log.setFormatter(fh_formatter)
logging.getLogger().addHandler(fh_debug)
logging.getLogger().addHandler(fh_log)
# Add console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
ch_formatter = logging.Formatter('%(message)s')
ch.setFormatter(ch_formatter)
logging.getLogger().addHandler(ch)
# Need to set the logging level of the logger as well as the handlers
logging.getLogger().setLevel(logging.DEBUG)
# Set up the logger for this module
logger = logging.getLogger("Syncer")
logger.debug('Logger started.')
The modules simply contain:
logger = logging.getLogger(__name__)