I use the logging module to output what was used for printing in my console script and log file. However, every time I run the script, the output seems to be added to what was printed in the console and the log, rather than overwriting what was there. For example, the first time I run the script, I get this output in my console and the log file:
This print statement is going to both the console and the log The year is: 2015
The second time I run the script, I get this in the console:
This print statement is going to both the console and the log This print statement is going to both the console and the log The year is: 2015 The year is: 2015
The third time:
This print statement is going to both the console and the log This print statement is going to both the console and the log This print statement is going to both the console and the log The year is: 2015 The year is: 2015 The year is: 2015
etc., and the log file continues adding, so I get:
This print statement is going to both the console and the log The year is: 2015 This print statement is going to both the console and the log This print statement is going to both the console and the log The year is: 2015 The year is: 2015 This print statement is going to both the console and the log This print statement is going to both the console and the log This print statement is going to both the console and the log The year is: 2015 The year is: 2015 The year is: 2015
I want both the log file and the console to provide me with only the following: no matter how many times I restart the script:
This print statement is going to both the console and the log The year is: 2015
Note. The only way I can currently get what I want is to close Python between script runs, but this is not a practical solution.
I experimented using flush () and tried to do something like this:
logger = logging.basicConfig(filemode='w', level=logging.DEBUG)
but I failed to get it to work with my code. Can someone help? Here is my code:
import inspect import logging import datetime def getDate(): now = datetime.datetime.now() m = now.month d = now.day y = str(now.year) if m < 10: m = "0"+str(m) else: m = str(m) if d < 10: d = "0"+str(d) else: d = str(d) y = y[2:] formatted_date = m+d+y return formatted_date def function_logger(file_level, console_level = None): function_name = inspect.stack()[1][3] logger = logging.getLogger(function_name) logger.setLevel(logging.DEBUG)