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.FilterOutputStream;
021import java.io.IOException;
022import java.io.OutputStream;
023import java.nio.charset.Charset;
024
025import org.apache.logging.log4j.Level;
026import org.apache.logging.log4j.Marker;
027import org.apache.logging.log4j.spi.ExtendedLogger;
028
029/**
030 * Logs each line written to a pre-defined level. Can also be configured with a Marker. This class provides an interface
031 * that follows the {@link java.io.OutputStream} methods in spirit, but doesn't require output to any external stream.
032 * This class should <em>not</em> be used as a stream for an underlying logger unless it's being used as a bridge.
033 * Otherwise, infinite loops may occur!
034 * 
035 * @since 2.1
036 */
037public class LoggerFilterOutputStream extends FilterOutputStream {
038    private static final String FQCN = LoggerFilterOutputStream.class.getName();
039
040    private final ByteStreamLogger logger;
041    private final String fqcn;
042
043    protected LoggerFilterOutputStream(final OutputStream out, final Charset charset, final ExtendedLogger logger,
044                                       final String fqcn, final Level level, final Marker marker) {
045        super(out);
046        this.logger = new ByteStreamLogger(logger, level, marker, charset);
047        this.fqcn = fqcn == null ? FQCN : fqcn;
048    }
049
050    @Override
051    public void close() throws IOException {
052        this.out.close();
053        this.logger.close(this.fqcn);
054    }
055
056    @Override
057    public void flush() throws IOException {
058        this.out.flush();
059    }
060
061    @Override
062    public String toString() {
063        return LoggerFilterOutputStream.class.getSimpleName() + "{stream=" + this.out + '}';
064    }
065
066    @Override
067    public void write(final byte[] b) throws IOException {
068        this.out.write(b);
069        this.logger.put(this.fqcn, b, 0, b.length);
070    }
071
072    @Override
073    public void write(final byte[] b, final int off, final int len) throws IOException {
074        this.out.write(b, off, len);
075        this.logger.put(this.fqcn, b, off, len);
076    }
077
078    @Override
079    public void write(final int b) throws IOException {
080        this.out.write(b);
081        this.logger.put(this.fqcn, (byte) (b & 0xFF));
082    }
083}