|
6 | 6 |
|
7 | 7 | public class WriterAppender extends AppenderSkeleton {
|
8 | 8 |
|
9 |
| - protected boolean immediateFlush = true; |
10 |
| - protected String encoding; |
11 |
| - protected QuietWriter qw; |
12 |
| - |
13 |
| - public WriterAppender() { |
14 |
| - } |
15 |
| - |
16 |
| - public WriterAppender(OutputStream os) { |
17 |
| - this(new OutputStreamWriter(os)); |
18 |
| - } |
19 |
| - |
20 |
| - public WriterAppender(Writer writer) { |
21 |
| - this.setWriter(writer); |
22 |
| - } |
23 |
| - |
24 |
| - public void setImmediateFlush(boolean value) { |
25 |
| - immediateFlush = value; |
26 |
| - } |
27 |
| - |
28 |
| - public boolean getImmediateFlush() { |
29 |
| - return immediateFlush; |
30 |
| - } |
31 |
| - |
32 |
| - /** |
33 |
| - * This method is called by the {@link AppenderSkeleton#doAppend} |
34 |
| - * method. |
35 |
| - */ |
36 |
| - public void append(LoggingEvent event) { |
37 |
| - if (! checkEntryConditions()) { |
38 |
| - return; |
39 |
| - } |
40 |
| - subAppend(event); |
41 |
| - } |
42 |
| - |
43 |
| - public final static String LINE_SEP = System.getProperty("line.separator"); |
44 |
| - |
45 |
| - protected void subAppend(LoggingEvent event) { |
46 |
| - this.qw.write(new Formater().format(event)); |
47 |
| - String s = event.getThrowableStrRep(); |
48 |
| - this.qw.write(s); |
49 |
| - if (shouldFlush(event)) { |
50 |
| - this.qw.flush(); |
51 |
| - } |
52 |
| - } |
53 |
| - |
54 |
| - protected boolean shouldFlush(final LoggingEvent event) { |
55 |
| - return immediateFlush; |
56 |
| - } |
57 |
| - |
58 |
| - |
59 |
| - protected boolean checkEntryConditions() { |
60 |
| - if (this.closed) { |
61 |
| - System.err.println("Not allowed to write to a closed appender."); |
62 |
| - return false; |
63 |
| - } |
64 |
| - |
65 |
| - if (this.qw == null) { |
66 |
| - System.err.println("No output stream or file set for the appender named [" + |
67 |
| - name + "]."); |
68 |
| - return false; |
69 |
| - } |
70 |
| - return true; |
71 |
| - } |
72 |
| - |
73 |
| - protected void reset() { |
74 |
| - closeWriter(); |
75 |
| - this.qw = null; |
76 |
| - //this.tp = null; |
77 |
| - } |
78 |
| - |
79 |
| - public |
80 |
| - synchronized void close() { |
81 |
| - if (this.closed) |
82 |
| - return; |
83 |
| - this.closed = true; |
84 |
| - writeFooter(); |
85 |
| - reset(); |
86 |
| - } |
87 |
| - |
88 |
| - private void writeFooter() { |
89 |
| - String f = "----------- WatchMe End -----------\n"; |
90 |
| - if (f != null && this.qw != null) { |
91 |
| - this.qw.write(f); |
92 |
| - this.qw.flush(); |
93 |
| - } |
94 |
| - } |
95 |
| - |
96 |
| - protected void closeWriter() { |
97 |
| - if (qw != null) { |
98 |
| - try { |
99 |
| - qw.close(); |
100 |
| - } catch (IOException e) { |
101 |
| - if (e instanceof InterruptedIOException) { |
102 |
| - Thread.currentThread().interrupt(); |
103 |
| - } |
104 |
| - System.err.println("Could not close " + qw); |
105 |
| - System.err.println(e.getMessage()); |
106 |
| - } |
107 |
| - } |
108 |
| - } |
109 |
| - |
110 |
| - public synchronized void setWriter(Writer writer) { |
111 |
| - reset(); |
112 |
| - this.qw = new QuietWriter(writer); |
113 |
| - writeHeader(); |
114 |
| - } |
115 |
| - |
116 |
| - protected void writeHeader() { |
117 |
| - String h = "----------- WatchMe Start -----------\n"; |
118 |
| - if (h != null && this.qw != null) |
119 |
| - this.qw.write(h); |
120 |
| - } |
121 |
| - |
122 |
| - |
123 |
| - protected OutputStreamWriter createWriter(OutputStream os) { |
124 |
| - OutputStreamWriter retval = null; |
125 |
| - String enc = getEncoding(); |
126 |
| - if (enc != null) { |
127 |
| - try { |
128 |
| - retval = new OutputStreamWriter(os, enc); |
129 |
| - } catch (IOException e) { |
130 |
| - if (e instanceof InterruptedIOException) { |
131 |
| - Thread.currentThread().interrupt(); |
132 |
| - } |
133 |
| - System.err.println("Error initializing output writer."); |
134 |
| - System.err.println("Unsupported encoding?"); |
135 |
| - } |
136 |
| - } |
137 |
| - if (retval == null) { |
138 |
| - retval = new OutputStreamWriter(os); |
139 |
| - } |
140 |
| - return retval; |
141 |
| - } |
142 |
| - |
143 |
| - public String getEncoding() { |
144 |
| - return encoding; |
145 |
| - } |
146 |
| - |
147 |
| - public void setEncoding(String value) { |
148 |
| - encoding = value; |
149 |
| - } |
150 |
| - |
151 |
| - @Override |
152 |
| - public void activateOptions() { |
153 |
| - } |
| 9 | + protected boolean immediateFlush = true; |
| 10 | + protected String encoding; |
| 11 | + protected QuietWriter qw; |
| 12 | + |
| 13 | + public final static String LINE_SEP = System.getProperty("line.separator"); |
| 14 | + StringBuffer sbuf = new StringBuffer(128); |
| 15 | + |
| 16 | + public WriterAppender() { |
| 17 | + } |
| 18 | + |
| 19 | + public WriterAppender(OutputStream os) { |
| 20 | + this(new OutputStreamWriter(os)); |
| 21 | + } |
| 22 | + |
| 23 | + public WriterAppender(Writer writer) { |
| 24 | + this.setWriter(writer); |
| 25 | + } |
| 26 | + |
| 27 | + public void setImmediateFlush(boolean value) { |
| 28 | + immediateFlush = value; |
| 29 | + } |
| 30 | + |
| 31 | + public boolean getImmediateFlush() { |
| 32 | + return immediateFlush; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * This method is called by the {@link AppenderSkeleton#doAppend} |
| 37 | + * method. |
| 38 | + */ |
| 39 | + public void append(LoggingEvent event) { |
| 40 | + if (!checkEntryConditions()) { |
| 41 | + return; |
| 42 | + } |
| 43 | + subAppend(event); |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + public String format(LoggingEvent event) { |
| 48 | + |
| 49 | + sbuf.setLength(0); |
| 50 | +// sbuf.append(event.getLevel().toString()); |
| 51 | + sbuf.append("- "); |
| 52 | + sbuf.append(event.getRenderedMessage()); |
| 53 | + sbuf.append(LINE_SEP); |
| 54 | + return sbuf.toString(); |
| 55 | + } |
| 56 | + |
| 57 | + protected void subAppend(LoggingEvent event) { |
| 58 | + this.qw.write(this.format(event)); |
| 59 | + String[] s = event.getThrowableStrRep(); |
| 60 | + if (s != null) { |
| 61 | + int len = s.length; |
| 62 | + for (int i = 0; i < len; i++) { |
| 63 | + this.qw.write(s[i]); |
| 64 | + this.qw.write(LINE_SEP); |
| 65 | + } |
| 66 | + } |
| 67 | + if (shouldFlush(event)) { |
| 68 | + this.qw.flush(); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + protected boolean shouldFlush(final LoggingEvent event) { |
| 73 | + return immediateFlush; |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + protected boolean checkEntryConditions() { |
| 78 | + if (this.closed) { |
| 79 | + System.err.println("Not allowed to write to a closed appender."); |
| 80 | + return false; |
| 81 | + } |
| 82 | + |
| 83 | + if (this.qw == null) { |
| 84 | + System.err.println("No output stream or file set for the appender named [" + |
| 85 | + name + "]."); |
| 86 | + return false; |
| 87 | + } |
| 88 | + return true; |
| 89 | + } |
| 90 | + |
| 91 | + protected void reset() { |
| 92 | + closeWriter(); |
| 93 | + this.qw = null; |
| 94 | + //this.tp = null; |
| 95 | + } |
| 96 | + |
| 97 | + public |
| 98 | + synchronized void close() { |
| 99 | + if (this.closed) |
| 100 | + return; |
| 101 | + this.closed = true; |
| 102 | + writeFooter(); |
| 103 | + reset(); |
| 104 | + } |
| 105 | + |
| 106 | + private void writeFooter() { |
| 107 | + String f = "----------- WatchMe End -----------\n"; |
| 108 | + if (f != null && this.qw != null) { |
| 109 | + this.qw.write(f); |
| 110 | + this.qw.flush(); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + protected void closeWriter() { |
| 115 | + if (qw != null) { |
| 116 | + try { |
| 117 | + qw.close(); |
| 118 | + } catch (IOException e) { |
| 119 | + if (e instanceof InterruptedIOException) { |
| 120 | + Thread.currentThread().interrupt(); |
| 121 | + } |
| 122 | + System.err.println("Could not close " + qw); |
| 123 | + System.err.println(e.getMessage()); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + public synchronized void setWriter(Writer writer) { |
| 129 | + reset(); |
| 130 | + this.qw = new QuietWriter(writer); |
| 131 | + writeHeader(); |
| 132 | + } |
| 133 | + |
| 134 | + protected void writeHeader() { |
| 135 | + String h = "----------- WatchMe Start -----------\n"; |
| 136 | + if (h != null && this.qw != null) |
| 137 | + this.qw.write(h); |
| 138 | + } |
| 139 | + |
| 140 | + |
| 141 | + protected OutputStreamWriter createWriter(OutputStream os) { |
| 142 | + OutputStreamWriter retval = null; |
| 143 | + String enc = getEncoding(); |
| 144 | + if (enc != null) { |
| 145 | + try { |
| 146 | + retval = new OutputStreamWriter(os, enc); |
| 147 | + } catch (IOException e) { |
| 148 | + if (e instanceof InterruptedIOException) { |
| 149 | + Thread.currentThread().interrupt(); |
| 150 | + } |
| 151 | + System.err.println("Error initializing output writer."); |
| 152 | + System.err.println("Unsupported encoding?"); |
| 153 | + } |
| 154 | + } |
| 155 | + if (retval == null) { |
| 156 | + retval = new OutputStreamWriter(os); |
| 157 | + } |
| 158 | + return retval; |
| 159 | + } |
| 160 | + |
| 161 | + public String getEncoding() { |
| 162 | + return encoding; |
| 163 | + } |
| 164 | + |
| 165 | + public void setEncoding(String value) { |
| 166 | + encoding = value; |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public void activateOptions() { |
| 171 | + } |
154 | 172 | }
|
0 commit comments