    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
    018    package org.apache.logging.log4j.io;
    019
    020    import java.io.IOException;
    021    import java.io.OutputStream;
    022    import java.io.PrintStream;
    023    import java.io.UnsupportedEncodingException;
    024    import java.nio.charset.Charset;
    025    import java.util.Locale;
    026
    027    import org.apache.logging.log4j.Level;
    028    import org.apache.logging.log4j.Marker;
    029    import org.apache.logging.log4j.spi.ExtendedLogger;
    030
    031    /**
    032     * Logs each line written to a pre-defined level. Can also be configured with a Marker. This class provides an interface
    033     * that follows the {@link java.io.PrintStream} methods in spirit, but doesn't require output to any external stream.
    034     * This class should <em>not</em> be used as a stream for an underlying logger unless it's being used as a bridge.
    035     * Otherwise, infinite loops may occur!
    036     *
    037     * @since 2.1
    038     */
    039    public class LoggerPrintStream extends PrintStream {
    040        private static final String FQCN = LoggerPrintStream.class.getName();
    041
    042        protected LoggerPrintStream(final ExtendedLogger logger, final boolean autoFlush, final Charset charset,
    043                                    final String fqcn, final Level level, final Marker marker)
    044            throws UnsupportedEncodingException {
    045            super(new LoggerOutputStream(logger, level, marker, ensureNonNull(charset), fqcn == null ? FQCN : fqcn),
    046                autoFlush, ensureNonNull(charset).name());
    047        }
    048
    049        protected LoggerPrintStream(final OutputStream out, final boolean autoFlush, final Charset charset,
    050                                    final ExtendedLogger logger, final String fqcn, final Level level, final Marker marker)
    051            throws UnsupportedEncodingException {
    052            super(new LoggerFilterOutputStream(out, ensureNonNull(charset), logger, fqcn == null ? FQCN : fqcn, level,
    053                marker), autoFlush, ensureNonNull(charset).name());
    054        }
    055
    056        private static Charset ensureNonNull(final Charset charset) {
    057            return charset == null ? Charset.defaultCharset() : charset;
    058        }
    059
    060        @Override
    061        public LoggerPrintStream append(final char c) {
    062            super.append(c);
    063            return this;
    064        }
    065
    066        @Override
    067        public LoggerPrintStream append(final CharSequence csq) {
    068            super.append(csq);
    069            return this;
    070        }
    071
    072        @Override
    073        public LoggerPrintStream append(final CharSequence csq, final int start, final int end) {
    074            super.append(csq, start, end);
    075            return this;
    076        }
    077
    078        @Override
    079        public boolean checkError() {
    080            return super.checkError();
    081        }
    082
    083        @Override
    084        public void close() {
    085            super.close();
    086        }
    087
    088        @Override
    089        public void flush() {
    090            super.flush();
    091        }
    092
    093        @Override
    094        public LoggerPrintStream format(final Locale l, final String format, final Object... args) {
    095            super.format(l, format, args);
    096            return this;
    097        }
    098
    099        @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    }