0

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')
2
  • above, I did not show the code before the line "starting-time=..." . Prior to that line, are all the "import" statements required. Also 8-10 function definitions are above that line. Commented May 19, 2019 at 1:13
  • From reading some of the answers here it looks like iPython configures its own logging, and this may lead to unexpected results. Your code seems to work fine if run from the command line. Commented May 19, 2019 at 8:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.