1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.logging.log4j.io;
19
20 import java.io.FilterWriter;
21 import java.io.IOException;
22 import java.io.Writer;
23
24 import org.apache.logging.log4j.Level;
25 import org.apache.logging.log4j.Marker;
26 import org.apache.logging.log4j.spi.ExtendedLogger;
27
28
29
30
31
32
33
34 public class LoggerFilterWriter extends FilterWriter {
35 private static final String FQCN = LoggerFilterWriter.class.getName();
36
37 private final CharStreamLogger logger;
38 private final String fqcn;
39
40 protected LoggerFilterWriter(final Writer out, final ExtendedLogger logger, final String fqcn, final Level level,
41 final Marker marker) {
42 super(out);
43 this.logger = new CharStreamLogger(logger, level, marker);
44 this.fqcn = fqcn == null ? FQCN : fqcn;
45 }
46
47 @Override
48 public void close() throws IOException {
49 this.out.close();
50 this.logger.close(this.fqcn);
51 }
52
53 @Override
54 public void flush() throws IOException {
55 this.out.flush();
56 }
57
58 @Override
59 public String toString() {
60 return LoggerFilterWriter.class.getSimpleName() + "{writer=" + this.out + '}';
61 }
62
63 @Override
64 public void write(final char[] cbuf) throws IOException {
65 this.out.write(cbuf);
66 this.logger.put(this.fqcn, cbuf, 0, cbuf.length);
67 }
68
69 @Override
70 public void write(final char[] cbuf, final int off, final int len) throws IOException {
71 this.out.write(cbuf, off, len);
72 this.logger.put(this.fqcn, cbuf, off, len);
73 }
74
75 @Override
76 public void write(final int c) throws IOException {
77 this.out.write(c);
78 this.logger.put(this.fqcn, (char) c);
79 }
80
81 @Override
82 public void write(final String str) throws IOException {
83 this.out.write(str);
84 this.logger.put(this.fqcn, str, 0, str.length());
85 }
86
87 @Override
88 public void write(final String str, final int off, final int len) throws IOException {
89 this.out.write(str, off, len);
90 this.logger.put(this.fqcn, str, off, len);
91 }
92 }