Class LogOutputStream

java.lang.Object
java.io.OutputStream
java.io.ByteArrayOutputStream
eu.cloudnetservice.utils.base.io.LogOutputStream
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public final class LogOutputStream extends ByteArrayOutputStream
A special implementation of an output stream specifically made to log all the written content to it to a provided logger instance.
Since:
4.0
  • Field Details

    • DEFAULT_AUTO_FLUSH

      public static final boolean DEFAULT_AUTO_FLUSH
      See Also:
    • DEFAULT_CHARSET

      public static final Charset DEFAULT_CHARSET
    • level

      private final org.slf4j.event.Level level
    • logger

      private final org.slf4j.Logger logger
    • flushLock

      private final Lock flushLock
  • Constructor Details

    • LogOutputStream

      private LogOutputStream(@NonNull @NonNull org.slf4j.event.Level level, @NonNull @NonNull org.slf4j.Logger logger)
      Creates a new log output stream instance.
      Parameters:
      level - the level which should be used when logging messages.
      logger - the logger to which all messages should get logged.
      Throws:
      NullPointerException - if the given level or logger is null.
  • Method Details

    • forWarn

      @NonNull public static @NonNull LogOutputStream forWarn(@NonNull @NonNull org.slf4j.Logger logger)
      Creates a new log output stream instance logging all messages to the given logger with the severe log level.
      Parameters:
      logger - the logger to log all messages to.
      Returns:
      a new log output stream instance for the given logger.
      Throws:
      NullPointerException - if the given logger is null.
    • forInfo

      @NonNull public static @NonNull LogOutputStream forInfo(@NonNull @NonNull org.slf4j.Logger logger)
      Creates a new log output stream instance logging all messages to the given logger with the info log level.
      Parameters:
      logger - the logger to log all messages to.
      Returns:
      a new log output stream instance for the given logger.
      Throws:
      NullPointerException - if the given logger is null.
    • newInstance

      @NonNull public static @NonNull LogOutputStream newInstance(@NonNull @NonNull org.slf4j.Logger logger, @NonNull @NonNull org.slf4j.event.Level level)
      Creates a new log output stream instance logging all messages to the given logger using the given log level.
      Parameters:
      logger - the logger to log all messages to.
      level - the level which the messages should have when logging them.
      Returns:
      a new log output stream instance for the given logger and level.
      Throws:
      NullPointerException - if the given logger or level is null.
    • toPrintStream

      @NonNull public @NonNull PrintStream toPrintStream()
      Wraps this log output stream into a print stream which automatically flushes and uses the UTF-8 charset encoding.
      Returns:
      a print stream wrapping this logging output stream.
    • toPrintStream

      @NonNull public @NonNull PrintStream toPrintStream(boolean autoFlush, @NonNull @NonNull Charset charset)
      Wraps this log output stream in a print stream using the given auto flush and charset settings.
      Parameters:
      autoFlush - if this buffer should be flushed automatically if content was written to the print stream.
      charset - the charset to use when flushing out buffer content.
      Returns:
      a print stream wrapping this output stream.
      Throws:
      NullPointerException - if the given charset is null.
    • flush

      public void flush() throws IOException
      Specified by:
      flush in interface Flushable
      Overrides:
      flush in class OutputStream
      Throws:
      IOException