Class OutputStreamManager
- java.lang.Object
-
- org.apache.logging.log4j.core.appender.AbstractManager
-
- org.apache.logging.log4j.core.appender.OutputStreamManager
-
- All Implemented Interfaces:
AutoCloseable
,ByteBufferDestination
- Direct Known Subclasses:
AbstractSocketManager
,FileManager
,MemoryMappedFileManager
,RandomAccessFileManager
public class OutputStreamManager extends AbstractManager implements ByteBufferDestination
Manages an OutputStream so that it can be shared by multiple Appenders and will allow appenders to reconfigure without requiring a new stream.
-
-
Field Summary
Fields Modifier and Type Field Description protected ByteBuffer
byteBuffer
protected Layout<?>
layout
-
Fields inherited from class org.apache.logging.log4j.core.appender.AbstractManager
count, LOGGER
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
OutputStreamManager(OutputStream os, String streamName, Layout<?> layout, boolean writeHeader)
protected
OutputStreamManager(OutputStream os, String streamName, Layout<?> layout, boolean writeHeader, int bufferSize)
protected
OutputStreamManager(OutputStream os, String streamName, Layout<?> layout, boolean writeHeader, ByteBuffer byteBuffer)
Deprecated.protected
OutputStreamManager(LoggerContext loggerContext, OutputStream os, String streamName, boolean createOnDemand, Layout<? extends Serializable> layout, boolean writeHeader, ByteBuffer byteBuffer)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected boolean
closeOutputStream()
protected OutputStream
createOutputStream()
ByteBuffer
drain(ByteBuffer buf)
Drains the ByteBufferDestination's buffer into the destination.void
flush()
Flushes any buffers.protected void
flushBuffer(ByteBuffer buf)
Drains the ByteBufferDestination's buffer into the destination.protected void
flushDestination()
Callsflush()
on the underlying output stream.ByteBuffer
getByteBuffer()
Returns thisByteBufferDestination
's buffer.static <T> OutputStreamManager
getManager(String name, T data, ManagerFactory<? extends OutputStreamManager,T> factory)
Creates a Manager.protected OutputStream
getOutputStream()
boolean
hasOutputStream()
boolean
isOpen()
Returns the status of the stream.boolean
releaseSub(long timeout, TimeUnit timeUnit)
Default hook to write footer during close.protected void
setOutputStream(OutputStream os)
void
skipFooter(boolean skipFooter)
Indicate whether the footer should be skipped or not.protected void
write(byte[] bytes)
Some output streams synchronize writes while others do not.protected void
write(byte[] bytes, boolean immediateFlush)
Some output streams synchronize writes while others do not.protected void
write(byte[] bytes, int offset, int length)
Some output streams synchronize writes while others do not.protected void
write(byte[] bytes, int offset, int length, boolean immediateFlush)
Some output streams synchronize writes while others do not.void
writeBytes(byte[] data, int offset, int length)
Writes the given data to this ByteBufferDestination.void
writeBytes(ByteBuffer data)
Writes the given data to this ByteBufferDestination entirely.protected void
writeFooter()
Writes the footer.protected void
writeToDestination(byte[] bytes, int offset, int length)
Writes the specified section of the specified byte array to the stream.-
Methods inherited from class org.apache.logging.log4j.core.appender.AbstractManager
close, getContentFormat, getCount, getLoggerContext, getManager, getName, hasManager, log, logDebug, logError, logger, logWarn, narrow, release, stop, updateData
-
-
-
-
Field Detail
-
layout
protected final Layout<?> layout
-
byteBuffer
protected ByteBuffer byteBuffer
-
-
Constructor Detail
-
OutputStreamManager
protected OutputStreamManager(OutputStream os, String streamName, Layout<?> layout, boolean writeHeader)
-
OutputStreamManager
protected OutputStreamManager(OutputStream os, String streamName, Layout<?> layout, boolean writeHeader, int bufferSize)
-
OutputStreamManager
@Deprecated protected OutputStreamManager(OutputStream os, String streamName, Layout<?> layout, boolean writeHeader, ByteBuffer byteBuffer)
Deprecated.- Since:
- 2.6
-
OutputStreamManager
protected OutputStreamManager(LoggerContext loggerContext, OutputStream os, String streamName, boolean createOnDemand, Layout<? extends Serializable> layout, boolean writeHeader, ByteBuffer byteBuffer)
- Since:
- 2.7
-
-
Method Detail
-
getManager
public static <T> OutputStreamManager getManager(String name, T data, ManagerFactory<? extends OutputStreamManager,T> factory)
Creates a Manager.- Type Parameters:
T
- The type of the OutputStreamManager.- Parameters:
name
- The name of the stream to manage.data
- The data to pass to the Manager.factory
- The factory to use to create the Manager.- Returns:
- An OutputStreamManager.
-
createOutputStream
protected OutputStream createOutputStream() throws IOException
- Throws:
IOException
-
skipFooter
public void skipFooter(boolean skipFooter)
Indicate whether the footer should be skipped or not.- Parameters:
skipFooter
- true if the footer should be skipped.
-
releaseSub
public boolean releaseSub(long timeout, TimeUnit timeUnit)
Default hook to write footer during close.- Overrides:
releaseSub
in classAbstractManager
- Parameters:
timeout
- timeouttimeUnit
- timeout time unit- Returns:
- true if all resources were closed normally, false otherwise.
-
writeFooter
protected void writeFooter()
Writes the footer.
-
isOpen
public boolean isOpen()
Returns the status of the stream.- Returns:
- true if the stream is open, false if it is not.
-
hasOutputStream
public boolean hasOutputStream()
-
getOutputStream
protected OutputStream getOutputStream() throws IOException
- Throws:
IOException
-
setOutputStream
protected void setOutputStream(OutputStream os)
-
write
protected void write(byte[] bytes)
Some output streams synchronize writes while others do not.- Parameters:
bytes
- The serialized Log event.- Throws:
AppenderLoggingException
- if an error occurs.
-
write
protected void write(byte[] bytes, boolean immediateFlush)
Some output streams synchronize writes while others do not.- Parameters:
bytes
- The serialized Log event.immediateFlush
- If true, flushes after writing.- Throws:
AppenderLoggingException
- if an error occurs.
-
writeBytes
public void writeBytes(byte[] data, int offset, int length)
Description copied from interface:ByteBufferDestination
Writes the given data to this ByteBufferDestination. Call of this method should *not* be protected with synchronized on this ByteBufferDestination instance. ByteBufferDestination implementations should synchronize themselves inside this method, if needed.This method should behave identically to
writeBytes(ByteBuffer.wrap(data, offset, length)
. It is provided to allow callers not to generate extra garbage.This method is called writeBytes() to avoid clashing with
write(byte[], int, int)
, which might be overridden in user-defined subclasses as protected, hence adding it to interface and requiring the method to be public breaks source compatibility.- Specified by:
writeBytes
in interfaceByteBufferDestination
-
write
protected void write(byte[] bytes, int offset, int length)
Some output streams synchronize writes while others do not. Synchronizing here insures that log events won't be intertwined.- Parameters:
bytes
- The serialized Log event.offset
- The offset into the byte array.length
- The number of bytes to write.- Throws:
AppenderLoggingException
- if an error occurs.
-
write
protected void write(byte[] bytes, int offset, int length, boolean immediateFlush)
Some output streams synchronize writes while others do not. Synchronizing here insures that log events won't be intertwined.- Parameters:
bytes
- The serialized Log event.offset
- The offset into the byte array.length
- The number of bytes to write.immediateFlush
- flushes immediately after writing.- Throws:
AppenderLoggingException
- if an error occurs.
-
writeToDestination
protected void writeToDestination(byte[] bytes, int offset, int length)
Writes the specified section of the specified byte array to the stream.- Parameters:
bytes
- the array containing dataoffset
- from where to writelength
- how many bytes to write- Since:
- 2.6
-
flushDestination
protected void flushDestination()
Callsflush()
on the underlying output stream.- Since:
- 2.6
-
flushBuffer
protected void flushBuffer(ByteBuffer buf)
Drains the ByteBufferDestination's buffer into the destination. By default this callswrite(byte[], int, int, boolean)
with the buffer contents. The underlying stream is not flushed.- Since:
- 2.6
- See Also:
flushDestination()
-
flush
public void flush()
Flushes any buffers.
-
closeOutputStream
protected boolean closeOutputStream()
-
getByteBuffer
public ByteBuffer getByteBuffer()
Returns thisByteBufferDestination
's buffer.- Specified by:
getByteBuffer
in interfaceByteBufferDestination
- Returns:
- the buffer
- Since:
- 2.6
-
drain
public ByteBuffer drain(ByteBuffer buf)
Drains the ByteBufferDestination's buffer into the destination. By default this callsflushBuffer(ByteBuffer)
with the specified buffer. Subclasses may override.Do not call this method lightly! For some subclasses this is a very expensive operation. For example,
MemoryMappedFileManager
will assume this method was called because the end of the mapped region was reached during a text encoding operation and will remap its buffer.To just flush the buffered contents to the underlying stream, call
flushBuffer(ByteBuffer)
directly instead.- Specified by:
drain
in interfaceByteBufferDestination
- Parameters:
buf
- the buffer whose contents to write the the destination- Returns:
- the specified buffer
- Since:
- 2.6
-
writeBytes
public void writeBytes(ByteBuffer data)
Description copied from interface:ByteBufferDestination
Writes the given data to this ByteBufferDestination entirely. Call of this method should *not* be protected with synchronized on this ByteBufferDestination instance. ByteBufferDestination implementations should synchronize themselves inside this method, if needed.- Specified by:
writeBytes
in interfaceByteBufferDestination
-
-