You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by James House <ja...@medibuy.com> on 2001/01/09 19:49:52 UTC
Interested in contributing.
Hi,
I'm interested in contributing to (and mildly influencing) the log4j
project.
I've created a few logging mechanisms in the past, and have some ideas that
I'd like you all to consider for addition to the log4j system. I had been
looking
at the log4j source, and working out how to propose fitting my ideas in, but
with the changes of last night's new release, I'll have to do some
rethinking.
In the mean time, below is some code that I think would make a good
contribution to either the org.apache.log4j.varia or
org.apache.log4j.helpers
package.
I've been using Java for over 5 years. Let me know how (and if) you'd like
me
to get involved.
James House
BufferedAutoFlushWriter.java
========================================================
import java.io.Writer;
import java.io.BufferedWriter;
import java.io.IOException;
/**
* A BufferedAutoFlushWriter is a BufferedWriter that flushes itself on a
given
* interval.
*
* <p>It provides a balance between the pros and cons of a
BufferedOutputStream/
* BufferedWriter or a OutputStream/Writer with flush() called after every
* write.</p>
*
* <p>Buffered streams provide a great performance boost because there are
fewer
* flushes (writes at the system level) - which are very costly. The
negatives
* to using buffered streams are: 1- Output to the file/socket/whatever is
* delayed until the buffer is full, you can't see the output right away. 2-
* Because the output is held in a buffer for a period of time, if your
* application or system crashes, the output is most likely lost.</p>
*
* <p>OuputStreams/Writers don't have the problems of buffered output, but
are
* significantly worse performing, because flushing happens so
frequently.</p>
*
* <p>Using a BufferedAutoFlushWriter gives you the best (and worst) of both
* worlds, but in practice, it has been found to be very useful within
logging
* mechanisms.</p>
*
* <p>If you create a BufferedAutoFlushWriter as follows:<BR>
* <code>
* logFile = new PrintWriter(
* BufferedAutoFlushWriter = new BufferedAutoFlushWriter(
* new FileWriter(fileName, append), 3000));
* </code>
*
* The created/appended file will be wrapped with a BufferedAutoFlushWriter
* that flushes (writes to the file) whenever the default buffer size (of a
* BUfferedWriter) is reached, or 3 seconds passes. This is ideal for when
* you are doing active debugging - you want to watch log output as it is
* written (or very shortly thereafter), but you don't want the performance
loss
* of flushing after every write to the log file. This is the idea that
* inspired the creation of this class - I was using buffered output for
logging
* performance, but couldn't always wait for the buffer to fill before
seeing
* the output in the file.</p>
*
* <p>For a 'production' system you would want to consider settings
like:<BR>
* <code>
* logFile = new PrintWriter(
* BufferedAutoFlushWriter = new BufferedAutoFlushWriter(
* new FileWriter(fileName, append),
* (20 * 1024), 30000));
* </code>
*
* This creates a large (20kb) buffer, but ensures that the file is written
to
* at least every 30 seconds (one tenth of the flushing in the previous
* example).</p>
*
* <p>Choose your buffer size and flush interval based upon your situation:
The
* more verbose your output, the larger buffer you need, the more critical
to
* get your logging into the output file, the smaller the flush interval
should
* be.</p>
*
*
* @author James House
*/
public
class BufferedAutoFlushWriter
extends BufferedWriter
{
private FlushThread flushThread = null;
private static int threadNum = 0;
/**
* Create a buffered character-output stream that uses a default-sized
* output buffer.
*
* @param out A Writer
* @param milliSeconds How often to flush the stream, 0 = no autoflushing.
*
* @exception IllegalArgumentException If milliSeconds < 0
*/
public
BufferedAutoFlushWriter(Writer out, long milliSeconds)
{
super(out);
if(milliSeconds < 0)
throw new IllegalArgumentException("Flush interval must be >= 0");
flushThread = new FlushThread(this, milliSeconds, threadNum++);
}
/**
* Create a new buffered character-output stream that uses an output
* buffer of the given size.
*
* @param out A Writer
* @param sz Output-buffer size, a positive integer
* @param milliSeconds How often to flush the stream, 0 = no autoflushing.
*
* @exception IllegalArgumentException If sz is <= 0 or milliSeconds < 0
*/
public
BufferedAutoFlushWriter(Writer out, int sz, long milliSeconds)
{
super(out, sz);
if(milliSeconds < 0)
throw new IllegalArgumentException("Flush interval must be >= 0");
flushThread = new FlushThread(this, milliSeconds, threadNum++);
}
/**
* Close the stream.
*
* @exception IOException If an I/O error occurs
*/
public
void close()
throws IOException
{
// signal flush thread to die...
flushThread.setFlushInterval(-1);
super.close();
}
/**
* Set the time interval at which the stream will be flushed,
* regardless of whether or not the buffer is full.
*
* <P>Set the interval to 0 for no auto-flushing.</P>
*
* @exception IllegalArgumentException If milliSeconds < 0
*/
public
void setAutoFlushInterval(long milliSeconds)
{
if(milliSeconds < 0)
throw new IllegalArgumentException("Flush interval must be >= 0");
flushThread.setFlushInterval(milliSeconds);
}
public
long getAutoFlushInterval()
{
return flushThread.getFlushInterval();
}
/***************************************************************************
*
*
* This thread handles the auto-flushing of the buffered stream
*
****************************************************************************
/
class FlushThread
extends Thread
{
private long flushInterval = 0; // milliSeconds
private BufferedAutoFlushWriter br;
public
FlushThread(BufferedAutoFlushWriter flushWriter,
long milliSeconds, int threadNum)
{
flushInterval = milliSeconds;
br = flushWriter;
setName("FlushThread_" + threadNum);
setDaemon(true);
setPriority(Thread.NORM_PRIORITY - 1);
start();
}
public
void setFlushInterval(long milliSeconds)
{
if(milliSeconds == 0)
milliSeconds = Long.MAX_VALUE;
flushInterval = milliSeconds;
synchronized(this) {
this.notify();
}
}
public
long getFlushInterval()
{
return flushInterval;
}
public
void run()
{
// loop until signalled that the stream is closed
while(flushInterval > 0) {
try {
synchronized(this) {
br.flush();
this.wait(flushInterval);
}
}
catch(Exception ignore) {}
}
// drop handle to writer (signalled to die)
br = null;
}
}
}