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.IOException;
    21  import java.io.OutputStream;
    22  import java.io.PrintStream;
    23  import java.io.UnsupportedEncodingException;
    24  import java.nio.charset.Charset;
    25  import java.util.Locale;
    26
    27  import org.apache.logging.log4j.Level;
    28  import org.apache.logging.log4j.Marker;
    29  import org.apache.logging.log4j.spi.ExtendedLogger;
    30
    31  /**
    32   * Logs each line written to a pre-defined level. Can also be configured with a Marker. This class provides an interface
    33   * that follows the {@link java.io.PrintStream} methods in spirit, but doesn't require output to any external stream.
    34   * This class should <em>not</em> be used as a stream for an underlying logger unless it's being used as a bridge.
    35   * Otherwise, infinite loops may occur!
    36   *
    37   * @since 2.1
    38   */
    39  public class LoggerPrintStream extends PrintStream {
    40      private static final String FQCN = LoggerPrintStream.class.getName();
    41
    42      protected LoggerPrintStream(final ExtendedLogger logger, final boolean autoFlush, final Charset charset,
    43                                  final String fqcn, final Level level, final Marker marker)
    44          throws UnsupportedEncodingException {
    45          super(new LoggerOutputStream(logger, level, marker, ensureNonNull(charset), fqcn == null ? FQCN : fqcn),
    46              autoFlush, ensureNonNull(charset).name());
    47      }
    48
    49      protected LoggerPrintStream(final OutputStream out, final boolean autoFlush, final Charset charset,
    50                                  final ExtendedLogger logger, final String fqcn, final Level level, final Marker marker)
    51          throws UnsupportedEncodingException {
    52          super(new LoggerFilterOutputStream(out, ensureNonNull(charset), logger, fqcn == null ? FQCN : fqcn, level,
    53              marker), autoFlush, ensureNonNull(charset).name());
    54      }
    55
    56      private static Charset ensureNonNull(final Charset charset) {
    57          return charset == null ? Charset.defaultCharset() : charset;
    58      }
    59
    60      @Override
    61      public LoggerPrintStream append(final char c) {
    62          super.append(c);
    63          return this;
    64      }
    65
    66      @Override
    67      public LoggerPrintStream append(final CharSequence csq) {
    68          super.append(csq);
    69          return this;
    70      }
    71
    72      @Override
    73      public LoggerPrintStream append(final CharSequence csq, final int start, final int end) {
    74          super.append(csq, start, end);
    75          return this;
    76      }
    77
    78      @Override
    79      public boolean checkError() {
    80          return super.checkError();
    81      }
    82
    83      @Override
    84      public void close() {
    85          super.close();
    86      }
    87
    88      @Override
    89      public void flush() {
    90          super.flush();
    91      }
    92
    93      @Override
    94      public LoggerPrintStream format(final Locale l, final String format, final Object... args) {
    95          super.format(l, format, args);
    96          return this;
    97      }
    98
    99      @Override
    100     public LoggerPrintStream format(final String format, final Object... args) {
    101         super.format(format, args);
    102         return this;
    103     }
    104
    105     @Override
    106     public void print(final boolean b) {
    107         super.print(b);
    108     }
    109
    110     @Override
    111     public void print(final char c) {
    112         super.print(c);
    113     }
    114
    115     @Override
    116     public void print(final char[] s) {
    117         super.print(s);
    118     }
    119
    120     @Override
    121     public void print(final double d) {
    122         super.print(d);
    123     }
    124
    125     @Override
    126     public void print(final float f) {
    127         super.print(f);
    128     }
    129
    130     @Override
    131     public void print(final int i) {
    132         super.print(i);
    133     }
    134
    135     @Override
    136     public void print(final long l) {
    137         super.print(l);
    138     }
    139
    140     @Override
    141     public void print(final Object obj) {
    142         super.print(obj);
    143     }
    144
    145     @Override
    146     public void print(final String s) {
    147         super.print(s);
    148     }
    149
    150     @Override
    151     public LoggerPrintStream printf(final Locale l, final String format, final Object... args) {
    152         super.printf(l, format, args);
    153         return this;
    154     }
    155
    156     @Override
    157     public LoggerPrintStream printf(final String format, final Object... args) {
    158         super.printf(format, args);
    159         return this;
    160     }
    161
    162     @Override
    163     public void println() {
    164         super.println();
    165     }
    166
    167     @Override
    168     public void println(final boolean x) {
    169         super.println(x);
    170     }
    171
    172     @Override
    173     public void println(final char x) {
    174         super.println(x);
    175     }
    176
    177     @Override
    178     public void println(final char[] x) {
    179         super.println(x);
    180     }
    181
    182     @Override
    183     public void println(final double x) {
    184         super.println(x);
    185     }
    186
    187     @Override
    188     public void println(final float x) {
    189         super.println(x);
    190     }
    191
    192     @Override
    193     public void println(final int x) {
    194         super.println(x);
    195     }
    196
    197     @Override
    198     public void println(final long x) {
    199         super.println(x);
    200     }
    201
    202     @Override
    203     public void println(final Object x) {
    204         super.println(x);
    205     }
    206
    207     @Override
    208     public void println(final String x) {
    209         super.println(x);
    210     }
    211
    212     @Override
    213     public String toString() {
    214         return LoggerPrintStream.class.getSimpleName() + "{stream=" + this.out + '}';
    215     }
    216
    217     @Override
    218     public void write(final byte[] b) throws IOException {
    219         super.write(b);
    220     }
    221
    222     @Override
    223     public void write(final byte[] b, final int off, final int len) {
    224         super.write(b, off, len);
    225     }
    226
    227     @Override
    228     public void write(final int b) {
    229         super.write(b);
    230     }
    231 }