001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.logging.log4j.io;
019
020import java.io.PrintWriter;
021import java.io.Writer;
022import java.util.Locale;
023
024import org.apache.logging.log4j.Level;
025import org.apache.logging.log4j.Marker;
026import org.apache.logging.log4j.spi.ExtendedLogger;
027
028/**
029 * Logs each line written to a pre-defined level. Can also be configured with a Marker. This class provides an interface
030 * that follows the {@link java.io.PrintWriter} methods in spirit, but doesn't require output to any external writer.
031 * <p>
032 * Integration with JDBC logging can be as simple as:
033 * </p>
034 * <pre>
035 *     PrintWriter pw = IoBuilder.forLogger().setLevel(Level.DEBUG).buildPrintWriter();
036 *     DriverManager.setLogWriter(pw);
037 *     DataSource ds = ...
038 *     ds.setLogWriter(pw);
039 * </pre>
040 *
041 * @since 2.1
042 */
043// TODO
044// All method implementations that call only super are apparently required for the unit tests to pass.
045// Not sure if this a bug in the tests or a feature.
046public class LoggerPrintWriter extends PrintWriter {
047    private static final String FQCN = LoggerPrintWriter.class.getName();
048
049    protected LoggerPrintWriter(final ExtendedLogger logger, final boolean autoFlush, final String fqcn,
050                                final Level level, final Marker marker) {
051        super(new LoggerWriter(logger, fqcn == null ? FQCN : fqcn, level, marker), autoFlush);
052    }
053
054    protected LoggerPrintWriter(final Writer writer, final boolean autoFlush, final ExtendedLogger logger,
055                                final String fqcn, final Level level, final Marker marker) {
056        super(new LoggerFilterWriter(writer, logger, fqcn == null ? FQCN : fqcn, level, marker), autoFlush);
057    }
058
059    @Override
060    public LoggerPrintWriter append(final char c) {
061        super.append(c);
062        return this;
063    }
064
065    @Override
066    public LoggerPrintWriter append(final CharSequence csq) {
067        super.append(csq);
068        return this;
069    }
070
071    @Override
072    public LoggerPrintWriter append(final CharSequence csq, final int start, final int end) {
073        super.append(csq, start, end);
074        return this;
075    }
076
077    @Override
078    public boolean checkError() {
079        return super.checkError();
080    }
081
082    @Override
083    public void close() {
084        super.close();
085    }
086
087    @Override
088    public void flush() {
089        super.flush();
090    }
091
092    @Override
093    public LoggerPrintWriter format(final Locale l, final String format, final Object... args) {
094        super.format(l, format, args);
095        return this;
096    }
097
098    @Override
099    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}