--log file object getting closed prematurely (Issue #219)

fix for #219 with tests.