1 | |
package org.apache.ojb.broker.util.logging; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
import java.io.PrintWriter; |
19 | |
|
20 | |
import org.apache.commons.lang.BooleanUtils; |
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
public class LoggerWrapperPrintWriter extends PrintWriter |
30 | |
{ |
31 | |
|
32 | |
private static final String LINESEP = System.getProperty("line.separator"); |
33 | |
private static final int DEFAULT_LEVEL = Logger.INFO; |
34 | |
|
35 | |
private final Logger logger; |
36 | |
private final int level; |
37 | |
private final boolean filterEverything; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
public LoggerWrapperPrintWriter(Logger logger, int level) |
46 | |
{ |
47 | |
super(System.out); |
48 | |
this.logger = logger; |
49 | |
this.level = level; |
50 | |
filterEverything = !logger.isEnabledFor(level); |
51 | |
} |
52 | |
|
53 | |
public LoggerWrapperPrintWriter(Logger logger) |
54 | |
{ |
55 | |
this(logger, DEFAULT_LEVEL); |
56 | |
} |
57 | |
|
58 | |
private void log(String s) |
59 | |
{ |
60 | |
switch (level) |
61 | |
{ |
62 | |
case Logger.FATAL: |
63 | |
logger.fatal(s); |
64 | |
break; |
65 | |
|
66 | |
case Logger.ERROR: |
67 | |
logger.error(s); |
68 | |
break; |
69 | |
|
70 | |
case Logger.WARN: |
71 | |
logger.warn(s); |
72 | |
break; |
73 | |
|
74 | |
case Logger.INFO: |
75 | |
logger.info(s); |
76 | |
break; |
77 | |
|
78 | |
case Logger.DEBUG: |
79 | |
logger.debug(s); |
80 | |
break; |
81 | |
|
82 | |
default: |
83 | |
throw new RuntimeException("Internal OJB fault. Logger API does not permit level " |
84 | |
+ level); |
85 | |
} |
86 | |
} |
87 | |
|
88 | |
private void logLn(String s) |
89 | |
{ |
90 | |
if (s != null) |
91 | |
{ |
92 | |
log(s); |
93 | |
} |
94 | |
log(LINESEP); |
95 | |
} |
96 | |
|
97 | |
|
98 | |
public void println() |
99 | |
{ |
100 | |
if (!filterEverything) |
101 | |
{ |
102 | |
logLn(null); |
103 | |
} |
104 | |
} |
105 | |
|
106 | |
public void print(char c) |
107 | |
{ |
108 | |
if (!filterEverything) |
109 | |
{ |
110 | |
log(new String(new char[]{c})); |
111 | |
} |
112 | |
} |
113 | |
|
114 | |
public void println(char c) |
115 | |
{ |
116 | |
if (!filterEverything) |
117 | |
{ |
118 | |
logLn(new String(new char[]{c})); |
119 | |
} |
120 | |
} |
121 | |
|
122 | |
public void print(double v) |
123 | |
{ |
124 | |
if (!filterEverything) |
125 | |
{ |
126 | |
log(Double.toString(v)); |
127 | |
} |
128 | |
} |
129 | |
|
130 | |
public void println(double v) |
131 | |
{ |
132 | |
if (!filterEverything) |
133 | |
{ |
134 | |
logLn(Double.toString(v)); |
135 | |
} |
136 | |
} |
137 | |
|
138 | |
public void print(float v) |
139 | |
{ |
140 | |
if (!filterEverything) |
141 | |
{ |
142 | |
log(Float.toString(v)); |
143 | |
} |
144 | |
} |
145 | |
|
146 | |
public void println(float v) |
147 | |
{ |
148 | |
if (!filterEverything) |
149 | |
{ |
150 | |
logLn(Float.toString(v)); |
151 | |
} |
152 | |
} |
153 | |
|
154 | |
public void print(int i) |
155 | |
{ |
156 | |
if (!filterEverything) |
157 | |
{ |
158 | |
log(Integer.toString(i)); |
159 | |
} |
160 | |
} |
161 | |
|
162 | |
public void println(int i) |
163 | |
{ |
164 | |
if (!filterEverything) |
165 | |
{ |
166 | |
logLn(Integer.toString(i)); |
167 | |
} |
168 | |
} |
169 | |
|
170 | |
public void print(long l) |
171 | |
{ |
172 | |
if (!filterEverything) |
173 | |
{ |
174 | |
log(Long.toString(l)); |
175 | |
} |
176 | |
} |
177 | |
|
178 | |
public void println(long l) |
179 | |
{ |
180 | |
if (!filterEverything) |
181 | |
{ |
182 | |
logLn(Long.toString(l)); |
183 | |
} |
184 | |
} |
185 | |
|
186 | |
public void print(boolean b) |
187 | |
{ |
188 | |
if (!filterEverything) |
189 | |
{ |
190 | |
log(BooleanUtils.toStringTrueFalse(b)); |
191 | |
} |
192 | |
} |
193 | |
|
194 | |
public void println(boolean b) |
195 | |
{ |
196 | |
if (!filterEverything) |
197 | |
{ |
198 | |
logLn(BooleanUtils.toStringTrueFalse(b)); |
199 | |
} |
200 | |
} |
201 | |
|
202 | |
public void print(char[] chars) |
203 | |
{ |
204 | |
if (!filterEverything) |
205 | |
{ |
206 | |
log(new String(chars)); |
207 | |
} |
208 | |
} |
209 | |
|
210 | |
public void println(char[] chars) |
211 | |
{ |
212 | |
if (!filterEverything) |
213 | |
{ |
214 | |
logLn(new String(chars)); |
215 | |
} |
216 | |
} |
217 | |
|
218 | |
public void print(Object o) |
219 | |
{ |
220 | |
if (!filterEverything && o != null) |
221 | |
{ |
222 | |
log(o.toString()); |
223 | |
} |
224 | |
} |
225 | |
|
226 | |
public void println(Object o) |
227 | |
{ |
228 | |
if (!filterEverything && o != null) |
229 | |
{ |
230 | |
logLn(o.toString()); |
231 | |
} |
232 | |
} |
233 | |
|
234 | |
public void print(String s) |
235 | |
{ |
236 | |
if (!filterEverything) |
237 | |
{ |
238 | |
log(s); |
239 | |
} |
240 | |
} |
241 | |
|
242 | |
public void println(String s) |
243 | |
{ |
244 | |
if (!filterEverything) |
245 | |
{ |
246 | |
logLn(s); |
247 | |
} |
248 | |
} |
249 | |
|
250 | |
public void write(int i) |
251 | |
{ |
252 | |
if (!filterEverything) |
253 | |
{ |
254 | |
print(i); |
255 | |
} |
256 | |
} |
257 | |
|
258 | |
public void write(String s) |
259 | |
{ |
260 | |
if (!filterEverything) |
261 | |
{ |
262 | |
print(s); |
263 | |
} |
264 | |
} |
265 | |
|
266 | |
public void write(char[] chars) |
267 | |
{ |
268 | |
if (!filterEverything) |
269 | |
{ |
270 | |
print(chars); |
271 | |
} |
272 | |
} |
273 | |
|
274 | |
public void write(char[] chars, int i, int i1) |
275 | |
{ |
276 | |
if (!filterEverything) |
277 | |
{ |
278 | |
log(new String(chars, i, i1)); |
279 | |
} |
280 | |
} |
281 | |
|
282 | |
public void write(String s, int i, int i1) |
283 | |
{ |
284 | |
if (!filterEverything) |
285 | |
{ |
286 | |
log(s.substring(i, i1)); |
287 | |
} |
288 | |
} |
289 | |
|
290 | |
} |