Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Logback: the reliable, generic, fast and flexible logging framework. Copyright (C) 1999-2011, QOS.ch. All rights reserved. This program and the accompanying materials are dual-licensed under either the terms of the Eclipse Public License v1.0 as published by the Eclipse Foundation or (per the licensee's choosing) under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation.
 
 package ch.qos.logback.core;
 
 import static ch.qos.logback.core.CoreConstants.CODES_URL;
 
 
OutputStreamAppender appends events to a java.io.OutputStream. This class provides basic services that other appenders build upon. For more information about this appender, please refer to the online manual at http://logback.qos.ch/manual/appenders.html#OutputStreamAppender

Author(s):
Ceki Gülcü
 
 public class OutputStreamAppender<E> extends UnsynchronizedAppenderBase<E> {

  
  
It is the encoder which is ultimately responsible for writing the event to an java.io.OutputStream.
 
   protected Encoder<E> encoder;

  
All synchronization in this class is done via the lock object.
 
   protected LogbackLock lock = new LogbackLock();

  
This is the outputStream where output will be written.
 
   private OutputStream outputStream;

  
The underlying output stream used by this appender.

Returns:
 
   public OutputStream getOutputStream() {
     return ;
   }

  
Checks that requires parameters are set and if everything is in order, activates this appender.
 
   public void start() {
     int errors = 0;
     if (this. == null) {
       addStatus(new ErrorStatus("No encoder set for the appender named \""
           +  + "\"."this));
       errors++;
     }
 
     if (this. == null) {
       addStatus(new ErrorStatus(
           "No output stream set for the appender named \"" +  + "\"."this));
       errors++;
     }
     // only error free appenders should be activated
     if (errors == 0) {
       super.start();
     }
   }
 
   public void setLayout(Layout<E> layout) {
     addWarn("This appender no longer admits a layout as a sub-component, set an encoder instead.");
     addWarn("To ensure compatibility, wrapping your layout in LayoutWrappingEncoder.");
     addWarn("See also "++"#layoutInsteadOfEncoder for details");
     LayoutWrappingEncoder<E> lwe = new LayoutWrappingEncoder<E>();
     lwe.setLayout(layout);
     lwe.setContext();
     this. = lwe;
   }
 
   @Override
   protected void append(E eventObject) {
     if (!isStarted()) {
      return;
    }
    subAppend(eventObject);
  }

  
Stop this appender instance. The underlying stream or writer is also closed.

Stopped appenders cannot be reused.

  public void stop() {
    synchronized () {
      closeOutputStream();
      super.stop();
    }
  }

  
Close the underlying java.io.OutputStream.
  protected void closeOutputStream() {
    if (this. != null) {
      try {
        // before closing we have to output out layout's footer
        encoderClose();
        this..close();
        this. = null;
      } catch (IOException e) {
        addStatus(new ErrorStatus(
            "Could not close output stream for OutputStreamAppender."thise));
      }
    }
  }
  void encoderInit() {
    if ( != null && this. != null) {
      try {
        .init();
      } catch (IOException ioe) {
        this. = false;
        addStatus(new ErrorStatus(
            "Failed to initialize encoder for appender named [" +  + "].",
            thisioe));
      }
    }
  }
  void encoderClose() {
    if ( != null && this. != null) {
      try {
        .close();
      } catch (IOException ioe) {
        this. = false;
        addStatus(new ErrorStatus("Failed to write footer for appender named ["
            +  + "]."thisioe));
      }
    }
  }

  

Sets the

Parameters:
outputStream An already opened OutputStream.
Link:
OutputStream} where the log output will go. The specified OutputStream must be opened by the user and be writable. The OutputStream will be closed when the appender instance is closed.
  public void setOutputStream(OutputStream outputStream) {
    synchronized () {
      // close any previously opened output stream
      closeOutputStream();
      this. = outputStream;
      if ( == null) {
        addWarn("Encoder has not been set. Cannot invoke its init method.");
        return;
      }
      encoderInit();
    }
  }
  protected void writeOut(E eventthrows IOException {
    this..doEncode(event);
  }

  
Actual writing occurs here.

Most subclasses of WriterAppender will need to override this method.

Since:
0.9.0
  protected void subAppend(E event) {
    if (!isStarted()) {
      return;
    }
    try {
      // this step avoids LBCLASSIC-139
      if (event instanceof DeferredProcessingAware) {
      }
      // the synchronization prevents the OutputStream from being closed while we
      // are writing. It also prevents multiple threads from entering the same
      // converter. Converters assume that they are in a synchronized block.
      synchronized () {
        writeOut(event);
      }
    } catch (IOException ioe) {
      // as soon as an exception occurs, move to non-started state
      // and add a single ErrorStatus to the SM.
      this. = false;
      addStatus(new ErrorStatus("IO failure in appender"thisioe));
    }
  }
  public Encoder<E> getEncoder() {
    return ;
  }
  public void setEncoder(Encoder<E> encoder) {
    this. = encoder;
  }
  
New to GrepCode? Check out our FAQ X