Class Slf4jLoggingOutputStream

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

    public class Slf4jLoggingOutputStream
    extends java.io.OutputStream
    An OutputStream that flushes out to a slf4j logger.

    Note that no data is written out to the Category until the stream is flushed or closed.


    Example:
     
     // make sure everything sent to System.err is logged
     System.setErr(new PrintStream(new LoggingOutputStream(LogHandler.WARN));
     // make sure everything sent to System.out is also logged
     System.setOut(new PrintStream(new LoggingOutputStream(LogHandler.INFO));
     
     
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int DEFAULT_BUFFER_LENGTH
      The default number of bytes in the buffer.
      protected com.adaptris.util.stream.LoggingOutputStreamImpl.LogLevel logLevel  
    • Constructor Summary

      Constructors 
      Constructor Description
      Slf4jLoggingOutputStream​(com.adaptris.util.stream.LoggingOutputStreamImpl.LogLevel level)  
      Slf4jLoggingOutputStream​(java.lang.String level)  
      Slf4jLoggingOutputStream​(org.slf4j.Logger log, com.adaptris.util.stream.LoggingOutputStreamImpl.LogLevel level)
      Creates the LoggingOutputStream to flush to the given LogLevel.
      Slf4jLoggingOutputStream​(org.slf4j.Logger log, java.lang.String level)
      Creates the LoggingOutputStream to flush to the given LogLevel.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Closes this output stream and releases any system resources associated with this stream.
      void flush()
      Flushes this output stream and forces any buffered output bytes to be written out.
      protected void log​(com.adaptris.util.stream.LoggingOutputStreamImpl.LogLevel level, java.lang.String s)  
      protected void reset()  
      void write​(int b)
      Writes the specified byte to this output stream.
      • 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

      • DEFAULT_BUFFER_LENGTH

        public static final int DEFAULT_BUFFER_LENGTH
        The default number of bytes in the buffer. =2048
        See Also:
        Constant Field Values
      • logLevel

        protected transient com.adaptris.util.stream.LoggingOutputStreamImpl.LogLevel logLevel
    • Constructor Detail

      • Slf4jLoggingOutputStream

        public Slf4jLoggingOutputStream​(com.adaptris.util.stream.LoggingOutputStreamImpl.LogLevel level)
      • Slf4jLoggingOutputStream

        public Slf4jLoggingOutputStream​(java.lang.String level)
      • Slf4jLoggingOutputStream

        public Slf4jLoggingOutputStream​(org.slf4j.Logger log,
                                        com.adaptris.util.stream.LoggingOutputStreamImpl.LogLevel level)
                                 throws java.lang.IllegalArgumentException
        Creates the LoggingOutputStream to flush to the given LogLevel.
        Parameters:
        log - the Logger to write to
        level - the Level to use when writing to the Logger
        Throws:
        java.lang.IllegalArgumentException - if log == null or level == null
      • Slf4jLoggingOutputStream

        public Slf4jLoggingOutputStream​(org.slf4j.Logger log,
                                        java.lang.String level)
                                 throws java.lang.IllegalArgumentException
        Creates the LoggingOutputStream to flush to the given LogLevel.
        Parameters:
        log - the Logger to write to
        level - the Level to use when writing to the Logger
        Throws:
        java.lang.IllegalArgumentException - if log == null or level == null
    • Method Detail

      • log

        protected void log​(com.adaptris.util.stream.LoggingOutputStreamImpl.LogLevel level,
                           java.lang.String s)
      • close

        public void close()
        Closes this output stream and releases any system resources associated with this stream. The general contract of close is that it closes the output stream. A closed stream cannot perform output operations and cannot be reopened.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.OutputStream
      • write

        public void write​(int b)
                   throws java.io.IOException
        Writes the specified byte to this output stream. The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored.
        Specified by:
        write in class java.io.OutputStream
        Parameters:
        b - the byte to write
        Throws:
        java.io.IOException - if an I/O error occurs. In particular, an IOException may 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 of flush is 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:
        flush in interface java.io.Flushable
        Overrides:
        flush in class java.io.OutputStream
      • reset

        protected void reset()