JavaSa
JavaSa

Reputation: 6242

logging how to control the times in which flush to log file

I've to use logging module and wonder if there is a way to log into existing log file , by appending my data to the existing file and more important if I can control the times I flush into the file.

Currently I need to be able to flush all the time to the file because, there are cases in which the script running the logic with the logging can crash, so I need to know exactly where my program stopped.

Any ideas and code examples? Thanks

Upvotes: 2

Views: 7191

Answers (1)

Brendan Abel
Brendan Abel

Reputation: 37549

If you use logging.FileHandler and choose an existing log file, by default it will append to that file. The method that actually writes the log record is the emit() method on logging handlers. If you look at the source code for the FileHandler, it flush's after every write, so it should be doing what you want by default.

import logging

log = logging.getLogger()
handler = logging.FileHandler('/path/to/log.txt')
log.addHandler(handler)

log.warning('This is a message')

Upvotes: 6

Related Questions