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.FilterWriter;
021import java.io.IOException;
022import java.io.Writer;
023
024import org.apache.logging.log4j.Level;
025import org.apache.logging.log4j.Marker;
026import org.apache.logging.log4j.io.internal.InternalFilterWriter;
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.Writer} methods in spirit, but doesn't require output to any external out.
032 *
033 * @since 2.1
034 */
035public class LoggerFilterWriter extends FilterWriter {
036    private static final String FQCN = LoggerFilterWriter.class.getName();
037
038    private final InternalFilterWriter logger;
039
040    protected LoggerFilterWriter(final Writer out, final ExtendedLogger logger, final String fqcn, final Level level,
041                                 final Marker marker) {
042        super(out);
043        this.logger = new InternalFilterWriter(out, logger, fqcn == null ? FQCN : fqcn, level, marker);
044    }
045
046    @Override
047    public void close() throws IOException {
048        this.logger.close();
049    }
050
051    @Override
052    public void flush() throws IOException {
053        this.logger.flush();
054    }
055
056    @Override
057    public String toString() {
058        return LoggerFilterWriter.class.getSimpleName() + logger.toString();
059    }
060
061    @Override
062    public void write(final char[] cbuf) throws IOException {
063        this.logger.write(cbuf);
064    }
065
066    @Override
067    public void write(final char[] cbuf, final int off, final int len) throws IOException {
068        this.logger.write(cbuf, off, len);
069    }
070
071    @Override
072    public void write(final int c) throws IOException {
073        this.logger.write(c);
074    }
075
076    @Override
077    public void write(final String str) throws IOException {
078        this.logger.write(str);
079    }
080
081    @Override
082    public void write(final String str, final int off, final int len) throws IOException {
083        this.logger.write(str, off, len);
084    }
085}