View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.logging.log4j.io;
19  
20  import java.io.FilterOutputStream;
21  import java.io.IOException;
22  import java.io.OutputStream;
23  import java.nio.charset.Charset;
24  
25  import org.apache.logging.log4j.Level;
26  import org.apache.logging.log4j.Marker;
27  import org.apache.logging.log4j.spi.ExtendedLogger;
28  
29  /**
30   * Logs each line written to a pre-defined level. Can also be configured with a Marker. This class provides an interface
31   * that follows the {@link java.io.OutputStream} methods in spirit, but doesn't require output to any external stream.
32   * This class should <em>not</em> be used as a stream for an underlying logger unless it's being used as a bridge.
33   * Otherwise, infinite loops may occur!
34   * 
35   * @since 2.1
36   */
37  public class LoggerFilterOutputStream extends FilterOutputStream {
38      private static final String FQCN = LoggerFilterOutputStream.class.getName();
39  
40      private final ByteStreamLogger logger;
41      private final String fqcn;
42  
43      protected LoggerFilterOutputStream(final OutputStream out, final Charset charset, final ExtendedLogger logger,
44                                         final String fqcn, final Level level, final Marker marker) {
45          super(out);
46          this.logger = new ByteStreamLogger(logger, level, marker, charset);
47          this.fqcn = fqcn == null ? FQCN : fqcn;
48      }
49  
50      @Override
51      public void close() throws IOException {
52          this.out.close();
53          this.logger.close(this.fqcn);
54      }
55  
56      @Override
57      public void flush() throws IOException {
58          this.out.flush();
59      }
60  
61      @Override
62      public String toString() {
63          return LoggerFilterOutputStream.class.getSimpleName() + "{stream=" + this.out + '}';
64      }
65  
66      @Override
67      public void write(final byte[] b) throws IOException {
68          this.out.write(b);
69          this.logger.put(this.fqcn, b, 0, b.length);
70      }
71  
72      @Override
73      public void write(final byte[] b, final int off, final int len) throws IOException {
74          this.out.write(b, off, len);
75          this.logger.put(this.fqcn, b, off, len);
76      }
77  
78      @Override
79      public void write(final int b) throws IOException {
80          this.out.write(b);
81          this.logger.put(this.fqcn, (byte) (b & 0xFF));
82      }
83  }