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.FilterReader;
21 import java.io.IOException;
22 import java.io.Reader;
23 import java.nio.CharBuffer;
24
25 import org.apache.logging.log4j.Level;
26 import org.apache.logging.log4j.Marker;
27 import org.apache.logging.log4j.spi.ExtendedLogger;
28
29
30
31
32
33
34 public class LoggerReader extends FilterReader {
35 private static final String FQCN = LoggerReader.class.getName();
36
37 private final CharStreamLogger logger;
38 private final String fqcn;
39
40 protected LoggerReader(final Reader reader, final ExtendedLogger logger, final String fqcn, final Level level,
41 final Marker marker) {
42 super(reader);
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 super.close();
50 this.logger.close(this.fqcn);
51 }
52
53 @Override
54 public int read() throws IOException {
55 final int c = super.read();
56 this.logger.put(this.fqcn, c);
57 return c;
58 }
59
60 @Override
61 public int read(final char[] cbuf) throws IOException {
62 return read(cbuf, 0, cbuf.length);
63 }
64
65 @Override
66 public int read(final char[] cbuf, final int off, final int len) throws IOException {
67 final int charsRead = super.read(cbuf, off, len);
68 this.logger.put(this.fqcn, cbuf, off, charsRead);
69 return charsRead;
70 }
71
72 @Override
73 public int read(final CharBuffer target) throws IOException {
74 final int len = target.remaining();
75 final char[] cbuf = new char[len];
76 final int charsRead = read(cbuf, 0, len);
77 if (charsRead > 0) {
78 target.put(cbuf, 0, charsRead);
79 }
80 return charsRead;
81 }
82
83 @Override
84 public String toString() {
85 return LoggerReader.class.getSimpleName() + "{stream=" + this.in + '}';
86 }
87 }