Class OutputStreamManager
- java.lang.Object
-
- org.apache.logging.log4j.core.appender.AbstractManager
-
- org.apache.logging.log4j.core.appender.OutputStreamManager
-
- All Implemented Interfaces:
java.lang.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.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.apache.logging.log4j.core.appender.AbstractManager
AbstractManager.AbstractFactoryData
-
-
Field Summary
Fields Modifier and Type Field Description protected java.nio.ByteBufferbyteBufferprotected Layout<?>layoutprivate java.io.OutputStreamoutputStreamprivate booleanskipFooter-
Fields inherited from class org.apache.logging.log4j.core.appender.AbstractManager
count, LOGGER
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedOutputStreamManager(java.io.OutputStream os, java.lang.String streamName, Layout<?> layout, boolean writeHeader)protectedOutputStreamManager(java.io.OutputStream os, java.lang.String streamName, Layout<?> layout, boolean writeHeader, int bufferSize)protectedOutputStreamManager(java.io.OutputStream os, java.lang.String streamName, Layout<?> layout, boolean writeHeader, java.nio.ByteBuffer byteBuffer)Deprecated.protectedOutputStreamManager(LoggerContext loggerContext, java.io.OutputStream os, java.lang.String streamName, boolean createOnDemand, Layout<? extends java.io.Serializable> layout, boolean writeHeader, java.nio.ByteBuffer byteBuffer)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected booleancloseOutputStream()protected java.io.OutputStreamcreateOutputStream()java.nio.ByteBufferdrain(java.nio.ByteBuffer buf)Drains the ByteBufferDestination's buffer into the destination.voidflush()Flushes any buffers.protected voidflushBuffer(java.nio.ByteBuffer buf)Drains the ByteBufferDestination's buffer into the destination.protected voidflushDestination()Callsflush()on the underlying output stream.java.nio.ByteBuffergetByteBuffer()Returns thisByteBufferDestination's buffer.static <T> OutputStreamManagergetManager(java.lang.String name, T data, ManagerFactory<? extends OutputStreamManager,T> factory)Creates a Manager.protected java.io.OutputStreamgetOutputStream()booleanhasOutputStream()booleanisOpen()Returns the status of the stream.booleanreleaseSub(long timeout, java.util.concurrent.TimeUnit timeUnit)Default hook to write footer during close.protected voidsetOutputStream(java.io.OutputStream os)voidskipFooter(boolean skipFooter)Indicate whether the footer should be skipped or not.protected voidwrite(byte[] bytes)Some output streams synchronize writes while others do not.protected voidwrite(byte[] bytes, boolean immediateFlush)Some output streams synchronize writes while others do not.protected voidwrite(byte[] bytes, int offset, int length)Some output streams synchronize writes while others do not.protected voidwrite(byte[] bytes, int offset, int length, boolean immediateFlush)Some output streams synchronize writes while others do not.voidwriteBytes(byte[] data, int offset, int length)Writes the given data to this ByteBufferDestination.voidwriteBytes(java.nio.ByteBuffer data)Writes the given data to this ByteBufferDestination entirely.protected voidwriteFooter()Writes the footer.protected voidwriteHeader(java.io.OutputStream os)protected voidwriteToDestination(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, getManagerCount, getName, getStrSubstitutor, hasManager, log, logDebug, logError, logger, logWarn, narrow, release, stop, updateData
-
-
-
-
Field Detail
-
layout
protected final Layout<?> layout
-
byteBuffer
protected java.nio.ByteBuffer byteBuffer
-
outputStream
private volatile java.io.OutputStream outputStream
-
skipFooter
private boolean skipFooter
-
-
Constructor Detail
-
OutputStreamManager
protected OutputStreamManager(java.io.OutputStream os, java.lang.String streamName, Layout<?> layout, boolean writeHeader)
-
OutputStreamManager
protected OutputStreamManager(java.io.OutputStream os, java.lang.String streamName, Layout<?> layout, boolean writeHeader, int bufferSize)
-
OutputStreamManager
@Deprecated protected OutputStreamManager(java.io.OutputStream os, java.lang.String streamName, Layout<?> layout, boolean writeHeader, java.nio.ByteBuffer byteBuffer)Deprecated.- Since:
- 2.6
-
OutputStreamManager
protected OutputStreamManager(LoggerContext loggerContext, java.io.OutputStream os, java.lang.String streamName, boolean createOnDemand, Layout<? extends java.io.Serializable> layout, boolean writeHeader, java.nio.ByteBuffer byteBuffer)
- Since:
- 2.7
-
-
Method Detail
-
getManager
public static <T> OutputStreamManager getManager(java.lang.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 java.io.OutputStream createOutputStream() throws java.io.IOException- Throws:
java.io.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, java.util.concurrent.TimeUnit timeUnit)Default hook to write footer during close.- Overrides:
releaseSubin classAbstractManager- Parameters:
timeout- timeouttimeUnit- timeout time unit- Returns:
- true if all resources were closed normally, false otherwise.
-
writeHeader
protected void writeHeader(java.io.OutputStream os)
-
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 java.io.OutputStream getOutputStream() throws java.io.IOException- Throws:
java.io.IOException
-
setOutputStream
protected void setOutputStream(java.io.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:ByteBufferDestinationWrites 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:
writeBytesin 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(java.nio.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 java.nio.ByteBuffer getByteBuffer()
Returns thisByteBufferDestination's buffer.- Specified by:
getByteBufferin interfaceByteBufferDestination- Returns:
- the buffer
- Since:
- 2.6
-
drain
public java.nio.ByteBuffer drain(java.nio.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,
MemoryMappedFileManagerwill 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:
drainin interfaceByteBufferDestination- Parameters:
buf- the buffer whose contents to write the destination- Returns:
- the specified buffer
- Since:
- 2.6
-
writeBytes
public void writeBytes(java.nio.ByteBuffer data)
Description copied from interface:ByteBufferDestinationWrites 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:
writeBytesin interfaceByteBufferDestination
-
-