Class LoggingOutputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.AutoCloseable

    public class LoggingOutputStream
    extends java.io.OutputStream
    Implements an OutputStream that forwards lines written to it to a Logger at a specified level. Bytes are accumulated by this OutputStream until a System.lineSeparator() value is found; once the line ending is found, the line is flushed to the logger.

    Bytes written to a LoggingOutputStream instance must have been encoded using UTF-8. This affects proper recognition of line separators and re-assembly of logged strings.

    • Constructor Summary

      Constructors 
      Constructor Description
      LoggingOutputStream​(org.slf4j.Logger logger, org.slf4j.event.Level level)
      Creates a new LoggingOutputStream writing to the provided logger and the specified level.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private void appendByte​(byte b)  
      private void checkOpen()  
      void close()  
      void flush()  
      private void flushInternal()  
      private static java.lang.invoke.MethodHandle getHandle​(org.slf4j.event.Level level)  
      private void log​(java.lang.String line)  
      void write​(int b)  
      • Methods inherited from class java.io.OutputStream

        nullOutputStream, write, write
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • LOGGER

        private static final org.slf4j.Logger LOGGER
      • LOG_METHODS

        private static final java.util.Map<org.slf4j.event.Level,​java.lang.invoke.MethodHandle> LOG_METHODS
      • handle

        private final java.lang.invoke.MethodHandle handle
      • twoByteLineSeparator

        private final boolean twoByteLineSeparator
      • eol

        private final byte eol
      • eolLeader

        private final byte eolLeader
      • closed

        private volatile boolean closed
      • buffer

        private byte[] buffer
      • bufferSize

        private int bufferSize
      • byteCount

        private int byteCount
      • haveLeader

        private boolean haveLeader
    • Constructor Detail

      • LoggingOutputStream

        public LoggingOutputStream​(org.slf4j.Logger logger,
                                   org.slf4j.event.Level level)
        Creates a new LoggingOutputStream writing to the provided logger and the specified level.

        Bytes written to this LoggingOutputStream must be UTF-8 encoded characters.

        Parameters:
        logger - the SLF4J Logger instance to which the stream should write
        level - the SLF4J level at which the stream is recorded to logger
    • Method Detail

      • write

        public void write​(int b)
                   throws java.io.IOException
        Specified by:
        write in class java.io.OutputStream
        Throws:
        java.io.IOException
      • flush

        public void flush()
                   throws java.io.IOException
        Specified by:
        flush in interface java.io.Flushable
        Overrides:
        flush in class java.io.OutputStream
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.OutputStream
        Throws:
        java.io.IOException
      • flushInternal

        private void flushInternal()
                            throws java.io.IOException
        Throws:
        java.io.IOException
      • log

        private void log​(java.lang.String line)
                  throws java.io.IOException
        Throws:
        java.io.IOException
      • appendByte

        private void appendByte​(byte b)
      • checkOpen

        private void checkOpen()
                        throws java.io.IOException
        Throws:
        java.io.IOException
      • getHandle

        private static java.lang.invoke.MethodHandle getHandle​(org.slf4j.event.Level level)