Package com.jalios.jcms.logging
Class Log4jOutputStream
java.lang.Object
java.io.OutputStream
com.jalios.jcms.logging.Log4jOutputStream
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
An OutputStream that flushes out to a log4j Category.
Note that no data is written out to the Category until the stream is flushed or closed.
Example:// make sure everything sent to System.err is logged System.setErr(new PrintStream(new Log4jOutputStream(Category.getRoot(), Priority.WARN), true)); // make sure everything sent to System.out is also logged System.setOut(new PrintStream(new Log4jOutputStream(Category.getRoot(), Priority.INFO), true));
- Author:
- Jim Moore
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected byte[]The internal buffer where data is stored.protected intThe number of valid bytes in the buffer.static final intThe default number of bytes in the buffer.protected booleanUsed to maintain the contract of #close()}.protected org.apache.log4j.LevelThe priority to use when writing to the Category.protected org.apache.log4j.LoggerThe category to write to. -
Constructor Summary
ConstructorsConstructorDescriptionLog4jOutputStream(org.apache.log4j.Logger log, org.apache.log4j.Level level) Creates the Log4jOutputStream to flush to the given Category. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes this output stream and releases any system resources associated with this stream.voidflush()Flushes this output stream and forces any buffered output bytes to be written out.voidwrite(int b) Writes the specified byte to this output stream.Methods inherited from class java.io.OutputStream
nullOutputStream, write, write
-
Field Details
-
hasBeenClosed
protected boolean hasBeenClosedUsed to maintain the contract of #close()}. -
buf
protected byte[] bufThe internal buffer where data is stored. -
count
protected int countThe number of valid bytes in the buffer. This value is always in the range0throughbuf.length; elementsbuf[0]throughbuf[count-1]contain valid byte data. -
DEFAULT_BUFFER_LENGTH
public static final int DEFAULT_BUFFER_LENGTHThe default number of bytes in the buffer. =2048- See Also:
-
logger
protected org.apache.log4j.Logger loggerThe category to write to. -
level
protected org.apache.log4j.Level levelThe priority to use when writing to the Category.
-
-
Constructor Details
-
Log4jOutputStream
public Log4jOutputStream(org.apache.log4j.Logger log, org.apache.log4j.Level level) throws IllegalArgumentException Creates the Log4jOutputStream to flush to the given Category.- Parameters:
log- the Logger to write tolevel- the Level to use when writing to the Logger- Throws:
IllegalArgumentException- if cat == null or priority == null
-
-
Method Details
-
close
public void close()Closes this output stream and releases any system resources associated with this stream. The general contract ofcloseis that it closes the output stream. A closed stream cannot perform output operations and cannot be reopened.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classOutputStream
-
write
Writes the specified byte to this output stream. The general contract forwriteis that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argumentb. The 24 high-order bits ofbare ignored.- Specified by:
writein classOutputStream- Parameters:
b- thebyteto write- Throws:
IOException- if an I/O error occurs. In particular, anIOExceptionmay be thrown if the output stream has been closed.
-
flush
public void flush()Flushes this output stream and forces any buffered output bytes to be written out. The general contract offlushis that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the output stream, such bytes should immediately be written to their intended destination.- Specified by:
flushin interfaceFlushable- Overrides:
flushin classOutputStream
-