add some logging and fix potential running out of file handler