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