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