I'm total noob & troubleshooting a Python program I inherited from a previous engineer. I'm using latest Anaconda (Anaconda3-2019.03-Windows) and always running the program from within Spyder 3.3.3.
The program uses the logging.info function at various points.
Logging is displayed on the IPython console, and at conclusion or program, also writes the log to a text file.
The 1st time I run program, the logging text file contains a faithful copy of what was displayed in the Console.
After a couple minutes, if I run the program a 2nd time, the console correctly displays the new log. A new text file is also created, but it is Empty!
Same behavior on 3rd, 4th, etc , running of the program: Console displays OK, but new text log files are always Empty .
If I clear the IPython console ("close tab"), then run the program again, the 1st output log text file is OK. Subsequent runs keep creating empty text log files.
--> How do I fix the empty text log file problem?
I don't want to append to the text log. I want a new text log file created every time the program run
Here's the section of code:
starting_time = datetime.datetime.now()
logfilename = ('SomeLog'
+ datetime.datetime.now().strftime("%Y-%m-%d-%H-%M") + '.txt')
level = logging.INFO
format = ' %(message)s'
handlers = [logging.FileHandler(logfilename), logging.StreamHandler()]
logging.basicConfig(level=level, format=format, handlers=handlers)
# Bunch of math processing.
logging.info('Data written succesfully to: ' + sqlite_table)
logging.info('Elapsed time to finish SQL write: '
+ str((datetime.datetime.now() - starting_time).seconds) + ' seconds')
# More math processing.
logging.info("Total elapsed time: "
+ str((datetime.datetime.now() - starting_time).seconds) + ' seconds')
logging.shutdown() # last line of python program.
I also tried explictly setting the mode = 'w', but had no effect.
logging.basicConfig(level=level, format=format, handlers=handlers, mode='w')