flush log messages right away when logging to stderr or stdout

If the logfile handle is not closed after writing a log message the message can sit in the IO buffers causing delayed logs.  This change calls fflush if we aren't closing the file to flush them out right away.