Take the 2-minute tour ×

I am writing a Java Logging framework to write log messages asynchronously to a file system. For this I am using MemoryHandler which finally writes to a File using FileHandler. I don't want to write log message to the file as soon as it is logged, instead I want to flush all the contents of Memory through a external class push when the buffer reaches certain threshould. Can anybody point me to a good example to write this external class to call push method of MemoryHandler explicitly? Thanks in advance.

share|improve this question
Why can't MemoryHandler flush itself, since it can (presumably)tell when it hits the threshold? –  jpm Mar 27 '12 at 21:13
Thanks for the feedback. It can flush itself, but I wanted to avoid the IO wait for the online application by calling this from a external class (may be a thread). –  user1296594 Mar 27 '12 at 21:39
add comment

1 Answer

In the method that adds messages to be logged (we'll call it log(String msg)), you can kick off a thread to flush, like so:

public void log(String msg) {
  if (buffer.length() > threshold) {
    new Thread() {
      public void run() {

private void flush() {
  // ...

This will keep the flush asynchronous, and avoid creating a whole class to call one method, not to mention needing to expose flush, which you might not want to do.

EDIT: Alternately, if you have a thread pool that is already set up, you could create a new Runnable instead, and pass it off for execution. The concept is the same, though.

share|improve this answer
Thank you so much for the feedback. This is working fine. Other question I have is, while flush is going on using a separate thread, what will happen to incoming logs from the application. Will application has to wait until this thread finishes flushing existing buffer or how will it work? This is little confusing. Thanks. –  user1296594 Apr 2 '12 at 18:39
One option to resolve that concern would be have flush() begin by copying pending messages to a temp buffer and clearing the original. If you put those operations inside a synchronized block, new log operations that happen to come in at just that moment would only have to wait for a memory operation to complete, then proceed to write into a fresh buffer. Meanwhile, the flushing thread would be working from a snapshot of when it was called. This should guarantee that every message is written to file exactly once. –  jpm Apr 2 '12 at 22:26
A final consideration would be to find a way to ensure that pending messages are flushed upon an unexpected exit. Unfortunately, I'm not intimately familiar with what tools are available for this, so I'll let you research what options you have in that regard. –  jpm Apr 2 '12 at 22:27
Thank you for the insight. I did some research and did not find any way on how to read contents of "buffer" of MemoryHandler to a temporary buffer as you suggested. Any idea on this would be appreciated. Thanks. –  user1296594 Apr 3 '12 at 14:32
It depends on how your buffer is stored. If you're storing it in an ArrayList or LinkedList (or a class derived from one of those), you should be able to use the clone() method on the list (remembering, of course, to make the proper cast). If you're just using an array, I believe the Arrays.copyOf method should suit you nicely. –  jpm Apr 4 '12 at 16:31
add comment

Your Answer


