KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > util > logging > LoggerWrapperPrintWriter


1 package org.apache.ojb.broker.util.logging;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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 import java.io.PrintWriter JavaDoc;
19
20 import org.apache.commons.lang.BooleanUtils;
21
22 /**
23  * Extremely simple piggyback for OJB Logger interface to provide PrintWriter dito.
24  *
25  * @author <a HREF="mailto:mkalen@apache.org">Martin Kal&eacute;n</a>
26  * @version CVS $Id: LoggerWrapperPrintWriter.java,v 1.1.2.3 2005/12/21 22:28:16 tomdz Exp $
27  * @since OJB 1.0.4, 2005-apr-30
28  */

29 public class LoggerWrapperPrintWriter extends PrintWriter JavaDoc
30 {
31
32     private static final String JavaDoc 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      * Construct a new PrintWriter piggyback for the specified OJB logger.
41      * @param logger the logger to which all PrintWriter events should be sent to
42      * @param level the log level for PrintWriter events, can only be specified
43      * as a single level since ther is no priority concept in PrintWriter API
44      */

45     public LoggerWrapperPrintWriter(Logger logger, int level)
46     {
47         super(System.out); // dummy, must initialize stream
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 JavaDoc 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 JavaDoc("Internal OJB fault. Logger API does not permit level "
84                     + level);
85         }
86     }
87
88     private void logLn(String JavaDoc 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 JavaDoc(new char[]{c}));
111         }
112     }
113
114     public void println(char c)
115     {
116         if (!filterEverything)
117         {
118             logLn(new String JavaDoc(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 JavaDoc(chars));
207         }
208     }
209
210     public void println(char[] chars)
211     {
212         if (!filterEverything)
213         {
214             logLn(new String JavaDoc(chars));
215         }
216     }
217
218     public void print(Object JavaDoc o)
219     {
220         if (!filterEverything && o != null)
221         {
222             log(o.toString());
223         }
224     }
225
226     public void println(Object JavaDoc o)
227     {
228         if (!filterEverything && o != null)
229         {
230             logLn(o.toString());
231         }
232     }
233
234     public void print(String JavaDoc s)
235     {
236         if (!filterEverything)
237         {
238             log(s);
239         }
240     }
241
242     public void println(String JavaDoc 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 JavaDoc 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 JavaDoc(chars, i, i1));
279         }
280     }
281
282     public void write(String JavaDoc s, int i, int i1)
283     {
284         if (!filterEverything)
285         {
286             log(s.substring(i, i1));
287         }
288     }
289
290 }
291
Popular Tags