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.Writer;
022    
023    import org.apache.logging.log4j.Level;
024    import org.apache.logging.log4j.Marker;
025    import org.apache.logging.log4j.spi.ExtendedLogger;
026    
027    /**
028     * Logs each line written to a pre-defined level. Can also be configured with a Marker. This class provides an interface
029     * that follows the {@link java.io.Writer} methods in spirit, but doesn't require output to any external writer.
030     * 
031     * @since 2.1
032     */
033    public class LoggerWriter extends Writer {
034        private static final String FQCN = LoggerWriter.class.getName();
035    
036        private final CharStreamLogger logger;
037        private final String fqcn;
038    
039        protected LoggerWriter(final ExtendedLogger logger, final String fqcn, final Level level, final Marker marker) {
040            this.logger = new CharStreamLogger(logger, level, marker);
041            this.fqcn = fqcn == null ? FQCN : fqcn;
042        }
043    
044        @Override
045        public void close() throws IOException {
046            this.logger.close(this.fqcn);
047        }
048    
049        @Override
050        public void flush() throws IOException {
051            // do nothing
052        }
053    
054        @Override
055        public String toString() {
056            return this.getClass().getSimpleName() + "[fqcn=" + this.fqcn + ", logger=" + this.logger + "]";
057        }
058    
059        @Override
060        public void write(final char[] cbuf) throws IOException {
061            this.logger.put(this.fqcn, cbuf, 0, cbuf.length);
062        }
063    
064        @Override
065        public void write(final char[] cbuf, final int off, final int len) throws IOException {
066            this.logger.put(this.fqcn, cbuf, off, len);
067        }
068    
069        @Override
070        public void write(final int c) throws IOException {
071            this.logger.put(this.fqcn, (char) c);
072        }
073    
074        @Override
075        public void write(final String str) throws IOException {
076            this.logger.put(this.fqcn, str, 0, str.length());
077        }
078    
079        @Override
080        public void write(final String str, final int off, final int len) throws IOException {
081            this.logger.put(this.fqcn, str, off, len);
082        }
083    }