View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.logging.log4j.io;
19  
20  import java.io.IOException;
21  import java.io.Writer;
22  
23  import org.apache.logging.log4j.Level;
24  import org.apache.logging.log4j.Marker;
25  import org.apache.logging.log4j.io.internal.InternalWriter;
26  import org.apache.logging.log4j.spi.ExtendedLogger;
27  
28  /**
29   * Logs each line written to a pre-defined level. Can also be configured with a Marker. This class provides an interface
30   * that follows the {@link java.io.Writer} methods in spirit, but doesn't require output to any external writer.
31   *
32   * @since 2.1
33   */
34  public class LoggerWriter extends Writer {
35      private static final String FQCN = LoggerWriter.class.getName();
36  
37      private final InternalWriter writer;
38  
39      protected LoggerWriter(final ExtendedLogger logger, final String fqcn, final Level level, final Marker marker) {
40          this.writer = new InternalWriter(logger, fqcn == null ? FQCN : fqcn, level, marker);
41      }
42  
43      @Override
44      public void close() throws IOException {
45          writer.close();
46      }
47  
48      @Override
49      public void flush() throws IOException {
50          // do nothing
51      }
52  
53      @Override
54      public String toString() {
55          return this.getClass().getSimpleName() + "[fqcn=" + writer.toString();
56      }
57  
58      @Override
59      public void write(final char[] cbuf) throws IOException {
60          writer.write(cbuf);
61      }
62  
63      @Override
64      public void write(final char[] cbuf, final int off, final int len) throws IOException {
65          writer.write(cbuf, off, len);
66      }
67  
68      @Override
69      public void write(final int c) throws IOException {
70          writer.write(c);
71      }
72  
73      @Override
74      public void write(final String str) throws IOException {
75          writer.write(str);
76      }
77  
78      @Override
79      public void write(final String str, final int off, final int len) throws IOException {
80          writer.write(str, off, len);
81      }
82  }