All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.rt.storage.api.client.util.LoggingOutputStream Maven / Gradle / Ivy

package com.rt.storage.api.client.util;

import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Thread-safe output stream wrapper that forwards all writes to a given output stream, while
 * logging all writes to a {@link LoggingByteArrayOutputStream}.
 *
 * @since 1.9
 * @author Yaniv Inbar
 */
public class LoggingOutputStream extends FilterOutputStream {

  /** Log stream. */
  private final LoggingByteArrayOutputStream logStream;

  /**
   * @param outputStream output stream to forward all writes to
   * @param logger logger
   * @param loggingLevel logging level
   * @param contentLoggingLimit maximum number of bytes to log (may be {@code 0} to avoid logging
   *     content)
   */
  public LoggingOutputStream(
      OutputStream outputStream, Logger logger, Level loggingLevel, int contentLoggingLimit) {
    super(outputStream);
    logStream = new LoggingByteArrayOutputStream(logger, loggingLevel, contentLoggingLimit);
  }

  @Override
  public void write(int b) throws IOException {
    out.write(b);
    logStream.write(b);
  }

  @Override
  public void write(byte[] b, int off, int len) throws IOException {
    out.write(b, off, len);
    logStream.write(b, off, len);
  }

  @Override
  public void close() throws IOException {
    logStream.close();
    super.close();
  }

  /** Returns the log stream. */
  public final LoggingByteArrayOutputStream getLogStream() {
    return logStream;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy