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 Details

    • layout

      protected final Layout<?> layout
    • byteBuffer

      protected ByteBuffer byteBuffer
  • Constructor Details

  • Method Details

    • 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 class AbstractManager
      Parameters:
      timeout - timeout
      timeUnit - timeout time unit
      Returns:
      true if all resources were closed normally, false otherwise.
    • writeHeader

      protected void writeHeader(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 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 interface ByteBufferDestination
    • 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 data
      offset - from where to write
      length - how many bytes to write
      Since:
      2.6
    • flushDestination

      protected void flushDestination()
      Calls flush() 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 calls write(byte[], int, int, boolean) with the buffer contents. The underlying stream is not flushed.
      Since:
      2.6
      See Also:
    • flush

      public void flush()
      Flushes any buffers.
    • closeOutputStream

      protected boolean closeOutputStream()
    • getByteBuffer

      public ByteBuffer getByteBuffer()
      Returns this ByteBufferDestination's buffer.
      Specified by:
      getByteBuffer in interface ByteBufferDestination
      Returns:
      the buffer
      Since:
      2.6
    • drain

      public ByteBuffer drain(ByteBuffer buf)
      Drains the ByteBufferDestination's buffer into the destination. By default this calls flushBuffer(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 interface ByteBufferDestination
      Parameters:
      buf - the buffer whose contents to write 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 interface ByteBufferDestination