Add flush of output buffers for logging

This modification should ensure that logs are completely written, and not left in an incomplete state with things left in the write buffer, if the job crashes or has to be killed.