Do not reinvent this wheel if it can be avoided. Take a look at Log4j or better slf4j .
Log4j and slf4j are very effective if you are not tracking, so on a production system you can turn off the logging level and still have good performance.
Both log4j and slf4j are immediately written to the log files and hidden, do not perform buffering by default, for the very good reason that you want to see an exception in the log file that caused your failure. If you really want to add buffering, you can do it ( FileAppender # bufferedIO )
As for finalize (), it cannot be called on exit. From System # runFinalizersOnExit .
Outdated. This method is inherently unsafe. This can cause finalizers to invoke live objects, while other threads are simultaneously manipulating these objects, leading to unstable behavior or a deadlock. Enable or disable finalization on exit; to do this indicates that finalizers of all objects that have finalizers that have not yet been automatically called must be run before Java run time. By default, exit finalization is disabled .
My emphasis. Thus, no, it seems that a buffered logger would have inherent problems.
source share