Did some tests and writing to file is too performance expensive. I profiled a quite long process and it logged 100-200 debug messages. And it took 15 seconds when I logged to a file. When disabled, it took 7 seconds to run the process. So the write to file cost me 8 seconds.
Viewing post in Add catch_error_add_debug_message for error log?
See if using this extension and rewinding-writing-flushing (flush may not be needed) the file works better?