    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.nio.charset.Charset;
    023
    024    import org.apache.logging.log4j.Level;
    025    import org.apache.logging.log4j.Marker;
    026    import 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.OutputStream} methods in spirit, but doesn't require output to any external stream.
    031     * This class should <em>not</em> be used as a stream for an underlying logger unless it's being used as a bridge.
    032     * Otherwise, infinite loops may occur!
    033     *
    034     * @since 2.1
    035     */
    036    public class LoggerOutputStream extends OutputStream {
    037        private static final String FQCN = LoggerOutputStream.class.getName();
    038
    039        private final ByteStreamLogger logger;
    040        private final String fqcn;
    041
    042        protected LoggerOutputStream(final ExtendedLogger logger, final Level level, final Marker marker,
    043                                     final Charset charset, final String fqcn) {
    044            this.logger = new ByteStreamLogger(logger, level, marker, charset);
    045            this.fqcn = fqcn == null ? FQCN : fqcn;
    046        }
    047
    048        @Override
    049        public void close() throws IOException {
    050            this.logger.close(this.fqcn);
    051        }
    052
    053        @Override
    054        public void flush() throws IOException {
    055            // do nothing
    056        }
    057
    058        @Override
    059        public void write(final byte[] b) throws IOException {
    060            this.logger.put(this.fqcn, b, 0, b.length);
    061        }
    062
    063        @Override
    064        public void write(final byte[] b, final int off, final int len) throws IOException {
    065            this.logger.put(this.fqcn, b, off, len);
    066        }
    067
    068        @Override
    069        public void write(final int b) throws IOException {
    070            this.logger.put(this.fqcn, (byte) (b & 0xFF));
    071        }
    072    }