Class LoggingOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- com.openindex.openestate.tool.utils.LoggingOutputStream
-
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
public class LoggingOutputStream extends OutputStream
An OutputStream that flushes out to a Logger.Note that no data is written out to the Logger until the stream is flushed or closed.
Example:
// make sure everything sent to System.err is logged System.setErr(new PrintStream(new LoggingOutputStream(LoggerFactory.getLogger(this.getClass()), Priority.WARN), true)); // make sure everything sent to System.out is also logged System.setOut(new PrintStream(new LoggingOutputStream(LoggerFactory.getLogger(this.getClass()), Priority.INFO), true));
- Author:
- Jim Moore <Jim.Moore@rocketmail.com>, Andreas Rudolph
- See Also:
Logger
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classLoggingOutputStream.Priority
-
Field Summary
Fields Modifier and Type Field Description protected byte[]bufThe internal buffer where data is stored.protected intcountThe number of valid bytes in the buffer.static intDEFAULT_BUFFER_LENGTHThe default number of bytes in the buffer.protected booleanhasBeenClosedUsed to maintain the contract ofclose().protected static StringLINE_SEPARATORprotected org.slf4j.LoggerloggerThe logger to write to.protected Stringprefixprotected LoggingOutputStream.PrioritypriorityThe priority to use when writing to the Logger.
-
Constructor Summary
Constructors Constructor Description LoggingOutputStream(org.slf4j.Logger logger, LoggingOutputStream.Priority priority)Creates the LoggingOutputStream to flush to the given Logger.LoggingOutputStream(org.slf4j.Logger logger, LoggingOutputStream.Priority priority, String prefix)Creates the LoggingOutputStream to flush to the given Logger.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()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 Detail
-
LINE_SEPARATOR
protected static final String LINE_SEPARATOR
-
hasBeenClosed
protected boolean hasBeenClosed
Used to maintain the contract ofclose().
-
buf
protected byte[] buf
The internal buffer where data is stored.
-
count
protected int count
The number of valid bytes in the buffer. This value is always in the range 0 through buf.length; elements buf[0] through buf[count-1] contain valid byte data.
-
DEFAULT_BUFFER_LENGTH
public static final int DEFAULT_BUFFER_LENGTH
The default number of bytes in the buffer. =2048- See Also:
- Constant Field Values
-
logger
protected org.slf4j.Logger logger
The logger to write to.
-
priority
protected LoggingOutputStream.Priority priority
The priority to use when writing to the Logger.
-
prefix
protected String prefix
-
-
Constructor Detail
-
LoggingOutputStream
public LoggingOutputStream(org.slf4j.Logger logger, LoggingOutputStream.Priority priority) throws IllegalArgumentExceptionCreates the LoggingOutputStream to flush to the given Logger.- Parameters:
logger- the Logger to write topriority- the Priority to use when writing to the Logger- Throws:
IllegalArgumentException- if logger == null or priority == null
-
LoggingOutputStream
public LoggingOutputStream(org.slf4j.Logger logger, LoggingOutputStream.Priority priority, String prefix) throws IllegalArgumentExceptionCreates the LoggingOutputStream to flush to the given Logger.- Parameters:
logger- the Logger to write topriority- the Priority to use when writing to the Loggerprefix- the prefix is written before any new log line, maybe null- Throws:
IllegalArgumentException- if logger == null or priority == null
-
-
Method Detail
-
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
public void write(int b) throws IOExceptionWrites 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
-
-