KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v2 > log > jdk14logging > Jdk14MLog


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1, as
10  * published by the Free Software Foundation.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this software; see the file LICENSE. If not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

22
23
24 package com.mchange.v2.log.jdk14logging;
25
26 import java.util.*;
27 import java.util.logging.*;
28 import com.mchange.v2.log.*;
29 import com.mchange.v2.util.DoubleWeakHashMap;
30
31 public final class Jdk14MLog extends MLog
32 {
33     private static String JavaDoc[] UNKNOWN_ARRAY = new String JavaDoc[] {"UNKNOWN_CLASS", "UNKNOWN_METHOD"};
34
35     private final static String JavaDoc CHECK_CLASS = "java.util.logging.Logger";
36     
37     private final Map namedLoggerMap = new DoubleWeakHashMap();
38
39     MLogger global = null;
40
41     public Jdk14MLog() throws ClassNotFoundException JavaDoc
42     { Class.forName( CHECK_CLASS ); }
43
44     public synchronized MLogger getMLogger(String JavaDoc name)
45     {
46         MLogger out = (MLogger) namedLoggerMap.get( name );
47         if (out == null)
48         {
49             Logger lg = Logger.getLogger(name);
50             out = new Jdk14MLogger( lg );
51             namedLoggerMap.put( name, out );
52         }
53         return out;
54     }
55
56     public synchronized MLogger getMLogger(Class JavaDoc cl)
57     { return getLogger( cl.getName() ); }
58
59
60     public synchronized MLogger getMLogger()
61     {
62     if (global == null)
63         global = new Jdk14MLogger( LogManager.getLogManager().getLogger("global") );
64     return global;
65     }
66
67     /*
68      * We have to do this ourselves when class and method aren't provided,
69      * because the automatic extraction of this information will find the
70      * (not very informative) calls in this class.
71      */

72     private static String JavaDoc[] findCallingClassAndMethod()
73     {
74     StackTraceElement JavaDoc[] ste = new Throwable JavaDoc().getStackTrace();
75     for (int i = 0, len = ste.length; i < len; ++i)
76         {
77         StackTraceElement JavaDoc check = ste[i];
78         String JavaDoc cn = check.getClassName();
79         if (cn != null && ! cn.startsWith("com.mchange.v2.log.jdk14logging"))
80             return new String JavaDoc[] { check.getClassName(), check.getMethodName() };
81         }
82     return UNKNOWN_ARRAY;
83     }
84
85
86
87     private final static class Jdk14MLogger implements MLogger
88     {
89     Logger logger;
90
91     Jdk14MLogger( Logger logger )
92     {
93         this.logger = logger;
94         //System.err.println("LOGGER: " + this.logger);
95
}
96
97     private static Level level(MLevel lvl)
98     { return (Level) lvl.asJdk14Level(); }
99
100     public ResourceBundle getResourceBundle()
101     { return logger.getResourceBundle(); }
102
103     public String JavaDoc getResourceBundleName()
104     { return logger.getResourceBundleName(); }
105
106     public void setFilter(Object JavaDoc java14Filter) throws SecurityException JavaDoc
107     {
108         if (! (java14Filter instanceof Filter))
109         throw new IllegalArgumentException JavaDoc("MLogger.setFilter( ... ) requires a java.util.logging.Filter. " +
110                            "This is not enforced by the compiler only to permit building under jdk 1.3");
111         logger.setFilter( (Filter) java14Filter );
112     }
113
114     public Object JavaDoc getFilter()
115     { return logger.getFilter(); }
116
117     public void log(MLevel l, String JavaDoc msg)
118     {
119         if (! logger.isLoggable( level(l) )) return;
120
121         String JavaDoc[] sa = findCallingClassAndMethod();
122         logger.logp( level(l), sa[0], sa[1], msg );
123     }
124
125     public void log(MLevel l, String JavaDoc msg, Object JavaDoc param)
126     {
127         if (! logger.isLoggable( level(l) )) return;
128
129         String JavaDoc[] sa = findCallingClassAndMethod();
130         logger.logp( level(l), sa[0], sa[1], msg, param );
131     }
132
133     public void log(MLevel l,String JavaDoc msg, Object JavaDoc[] params)
134     {
135         if (! logger.isLoggable( level(l) )) return;
136
137         String JavaDoc[] sa = findCallingClassAndMethod();
138         logger.logp( level(l), sa[0], sa[1], msg, params );
139     }
140
141     public void log(MLevel l, String JavaDoc msg, Throwable JavaDoc t)
142     {
143         if (! logger.isLoggable( level(l) )) return;
144
145         String JavaDoc[] sa = findCallingClassAndMethod();
146         logger.logp( level(l), sa[0], sa[1], msg, t );
147     }
148
149     public void logp(MLevel l, String JavaDoc srcClass, String JavaDoc srcMeth, String JavaDoc msg)
150     {
151         if (! logger.isLoggable( level(l) )) return;
152
153         if (srcClass == null && srcMeth == null)
154         {
155             String JavaDoc[] sa = findCallingClassAndMethod();
156             srcClass = sa[0];
157             srcMeth = sa[1];
158         }
159         logger.logp( level(l), srcClass, srcMeth, msg );
160     }
161
162     public void logp(MLevel l, String JavaDoc srcClass, String JavaDoc srcMeth, String JavaDoc msg, Object JavaDoc param)
163     {
164         if (! logger.isLoggable( level(l) )) return;
165
166         if (srcClass == null && srcMeth == null)
167         {
168             String JavaDoc[] sa = findCallingClassAndMethod();
169             srcClass = sa[0];
170             srcMeth = sa[1];
171         }
172         logger.logp( level(l), srcClass, srcMeth, msg, param );
173     }
174
175     public void logp(MLevel l, String JavaDoc srcClass, String JavaDoc srcMeth, String JavaDoc msg, Object JavaDoc[] params)
176     {
177         if (! logger.isLoggable( level(l) )) return;
178
179         if (srcClass == null && srcMeth == null)
180         {
181             String JavaDoc[] sa = findCallingClassAndMethod();
182             srcClass = sa[0];
183             srcMeth = sa[1];
184         }
185         logger.logp( level(l), srcClass, srcMeth, msg, params );
186     }
187
188     public void logp(MLevel l, String JavaDoc srcClass, String JavaDoc srcMeth, String JavaDoc msg, Throwable JavaDoc t)
189     {
190         if (! logger.isLoggable( level(l) )) return;
191
192         if (srcClass == null && srcMeth == null)
193         {
194             String JavaDoc[] sa = findCallingClassAndMethod();
195             srcClass = sa[0];
196             srcMeth = sa[1];
197         }
198         logger.logp( level(l), srcClass, srcMeth, msg, t );
199     }
200
201     public void logrb(MLevel l, String JavaDoc srcClass, String JavaDoc srcMeth, String JavaDoc rb, String JavaDoc msg)
202     {
203         if (! logger.isLoggable( level(l) )) return;
204
205         if (srcClass == null && srcMeth == null)
206         {
207             String JavaDoc[] sa = findCallingClassAndMethod();
208             srcClass = sa[0];
209             srcMeth = sa[1];
210         }
211         logger.logrb( level(l), srcClass, srcMeth, rb, msg );
212     }
213
214     public void logrb(MLevel l, String JavaDoc srcClass, String JavaDoc srcMeth, String JavaDoc rb, String JavaDoc msg, Object JavaDoc param)
215     {
216         if (! logger.isLoggable( level(l) )) return;
217
218         if (srcClass == null && srcMeth == null)
219         {
220             String JavaDoc[] sa = findCallingClassAndMethod();
221             srcClass = sa[0];
222             srcMeth = sa[1];
223         }
224         logger.logrb( level(l), srcClass, srcMeth, rb, msg, param );
225     }
226
227     public void logrb(MLevel l, String JavaDoc srcClass, String JavaDoc srcMeth, String JavaDoc rb, String JavaDoc msg, Object JavaDoc[] params)
228     {
229         if (! logger.isLoggable( level(l) )) return;
230
231         if (srcClass == null && srcMeth == null)
232         {
233             String JavaDoc[] sa = findCallingClassAndMethod();
234             srcClass = sa[0];
235             srcMeth = sa[1];
236         }
237         logger.logrb( level(l), srcClass, srcMeth, rb, msg, params );
238     }
239
240     public void logrb(MLevel l, String JavaDoc srcClass, String JavaDoc srcMeth, String JavaDoc rb, String JavaDoc msg, Throwable JavaDoc t)
241     {
242         if (! logger.isLoggable( level(l) )) return;
243
244         if (srcClass == null && srcMeth == null)
245         {
246             String JavaDoc[] sa = findCallingClassAndMethod();
247             srcClass = sa[0];
248             srcMeth = sa[1];
249         }
250         logger.logrb( level(l), srcClass, srcMeth, rb, msg, t );
251     }
252
253     public void entering(String JavaDoc srcClass, String JavaDoc srcMeth)
254     {
255         if (! logger.isLoggable( Level.FINER )) return;
256
257         logger.entering( srcClass, srcMeth );
258     }
259
260     public void entering(String JavaDoc srcClass, String JavaDoc srcMeth, Object JavaDoc param)
261     {
262         if (! logger.isLoggable( Level.FINER )) return;
263
264         logger.entering( srcClass, srcMeth, param );
265     }
266
267     public void entering(String JavaDoc srcClass, String JavaDoc srcMeth, Object JavaDoc params[])
268     {
269         if (! logger.isLoggable( Level.FINER )) return;
270
271         logger.entering( srcClass, srcMeth, params );
272     }
273
274     public void exiting(String JavaDoc srcClass, String JavaDoc srcMeth)
275     {
276         if (! logger.isLoggable( Level.FINER )) return;
277
278         logger.exiting( srcClass, srcMeth );
279     }
280
281     public void exiting(String JavaDoc srcClass, String JavaDoc srcMeth, Object JavaDoc result)
282     {
283         if (! logger.isLoggable( Level.FINER )) return;
284
285         logger.exiting( srcClass, srcMeth, result );
286     }
287
288     public void throwing(String JavaDoc srcClass, String JavaDoc srcMeth, Throwable JavaDoc t)
289     {
290         if (! logger.isLoggable( Level.FINER )) return;
291
292         logger.throwing( srcClass, srcMeth, t );
293     }
294
295     public void severe(String JavaDoc msg)
296     {
297         if (! logger.isLoggable( Level.SEVERE )) return;
298
299         String JavaDoc[] sa = findCallingClassAndMethod();
300         logger.logp( Level.SEVERE, sa[0], sa[1], msg );
301     }
302
303     public void warning(String JavaDoc msg)
304     {
305         if (! logger.isLoggable( Level.WARNING )) return;
306
307         String JavaDoc[] sa = findCallingClassAndMethod();
308         logger.logp( Level.WARNING, sa[0], sa[1], msg );
309     }
310
311     public void info(String JavaDoc msg)
312     {
313         if (! logger.isLoggable( Level.INFO )) return;
314
315         String JavaDoc[] sa = findCallingClassAndMethod();
316         logger.logp( Level.INFO, sa[0], sa[1], msg );
317     }
318
319     public void config(String JavaDoc msg)
320     {
321         if (! logger.isLoggable( Level.CONFIG )) return;
322
323         String JavaDoc[] sa = findCallingClassAndMethod();
324         logger.logp( Level.CONFIG, sa[0], sa[1], msg );
325     }
326
327     public void fine(String JavaDoc msg)
328     {
329         if (! logger.isLoggable( Level.FINE )) return;
330
331         String JavaDoc[] sa = findCallingClassAndMethod();
332         logger.logp( Level.FINE, sa[0], sa[1], msg );
333     }
334
335     public void finer(String JavaDoc msg)
336     {
337         if (! logger.isLoggable( Level.FINER )) return;
338
339         String JavaDoc[] sa = findCallingClassAndMethod();
340         logger.logp( Level.FINER, sa[0], sa[1], msg );
341     }
342
343     public void finest(String JavaDoc msg)
344     {
345         if (! logger.isLoggable( Level.FINEST )) return;
346
347         String JavaDoc[] sa = findCallingClassAndMethod();
348         logger.logp( Level.FINEST, sa[0], sa[1], msg );
349     }
350
351     public void setLevel(MLevel l) throws SecurityException JavaDoc
352     { logger.setLevel( level(l) ); }
353                           
354     public MLevel getLevel()
355     { return MLevel.fromIntValue( logger.getLevel().intValue() ); }
356
357     public boolean isLoggable(MLevel l)
358     { return logger.isLoggable( level(l) ); }
359
360     public String JavaDoc getName()
361     { return logger.getName(); }
362
363     public void addHandler(Object JavaDoc h) throws SecurityException JavaDoc
364     {
365         if (! (h instanceof Handler))
366         throw new IllegalArgumentException JavaDoc("MLogger.addHandler( ... ) requires a java.util.logging.Handler. " +
367                            "This is not enforced by the compiler only to permit building under jdk 1.3");
368         logger.addHandler( (Handler) h );
369     }
370
371     public void removeHandler(Object JavaDoc h) throws SecurityException JavaDoc
372     {
373         if (! (h instanceof Handler))
374         throw new IllegalArgumentException JavaDoc("MLogger.removeHandler( ... ) requires a java.util.logging.Handler. " +
375                            "This is not enforced by the compiler only to permit building under jdk 1.3");
376         logger.removeHandler( (Handler) h );
377     }
378
379     public Object JavaDoc[] getHandlers()
380     { return logger.getHandlers(); }
381
382     public void setUseParentHandlers(boolean uph)
383     { logger.setUseParentHandlers( uph ); }
384
385     public boolean getUseParentHandlers()
386     { return logger.getUseParentHandlers(); }
387     }
388 }
Popular Tags