Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Buffered Loggers (Java)

I'm toying around with the idea of building a logging system that pushes log statements to an internal buffer until it reaches a pre-defined capacity, and then dumps (flushes) the whole buffer at once.

This is because I like to sprinkle lots of TRACE statements all throughout my methods (so I can see what's going on every few lines; makes it easier to debug, at least for me). And I'm afraid that with (potentially) hundreds/thousands of log statements firing all over the place that such a large I/O demand will bog my programs down.

A "buffered" logger solution might alleviate this.

Three questions:

  1. Does something like this already exist? Hate to reinvent the wheel here, but online searches didn't turn back anything.
  2. I was thinking about the fact that I could very well lose log statements whenever the program halts unexpectedly (runtime exceptions, etc.) and the logger hasn't been flushed. In that case I'd like the logger to override finalize() so that if the program terminates with items still in its buffer, it can flush (publish) them prior to exiting. Thoughts?
  3. Is this a terrible idea? If so, why!
like image 346
IAmYourFaja Avatar asked Nov 16 '11 21:11

IAmYourFaja


1 Answers

Don't reinvent this particular wheel if you can possibly avoid it. Look at Log4j or better slf4j.

Log4j and slf4j are both very performant if you're not tracing, so in the production system you can turn down the level of logging and still have good performance.

Both log4j and slf4j write immediately to the logfiles and flush, don't do buffering by default, for the very good reason that you want to see in the logfile the exception which caused your crash. If you really want to add buffering you can do so (FileAppender#bufferedIO)

As far as finalize() is concerned, it is not guaranteed to be called on exit. From System#runFinalizersOnExit.

Deprecated. This method is inherently unsafe. It may result in finalizers being called on live objects while other threads are concurrently manipulating those objects, resulting in erratic behavior or deadlock. Enable or disable finalization on exit; doing so specifies that the finalizers of all objects that have finalizers that have not yet been automatically invoked are to be run before the Java runtime exits. By default, finalization on exit is disabled.

My emphasis. So, no it seems like a buffered logger would have inherent problems.

like image 116
Matthew Farwell Avatar answered Sep 19 '22 08:09

Matthew Farwell