KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v2 > log > FallbackMLog


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;
25
26 import java.text.*;
27 import java.util.*;
28 import java.util.logging.*;
29 import com.mchange.lang.ThrowableUtils;
30
31 public final class FallbackMLog extends MLog
32 {
33     final static MLevel DEFAULT_CUTOFF_LEVEL;
34
35     static
36     {
37     MLevel dflt = null;
38     String JavaDoc dfltName = MLog.CONFIG.getProperty( "com.mchange.v2.log.FallbackMLog.DEFAULT_CUTOFF_LEVEL" );
39     if (dfltName != null)
40         dflt = MLevel.fromSeverity( dfltName );
41     if (dflt == null)
42         dflt = MLevel.INFO;
43     DEFAULT_CUTOFF_LEVEL = dflt;
44     }
45
46     MLogger logger = new FallbackMLogger();
47
48     public synchronized MLogger getMLogger(String JavaDoc name)
49     { return logger; }
50
51     public MLogger getMLogger(Class JavaDoc cl)
52     { return getLogger( cl.getName() ); }
53
54
55     public MLogger getMLogger()
56     { return logger; }
57
58     private final static class FallbackMLogger implements MLogger
59     {
60     MLevel cutoffLevel = DEFAULT_CUTOFF_LEVEL;
61
62     private void formatrb(MLevel l, String JavaDoc srcClass, String JavaDoc srcMeth, String JavaDoc rbname, String JavaDoc msg, Object JavaDoc[] params, Throwable JavaDoc t)
63     {
64         ResourceBundle rb = ResourceBundle.getBundle( rbname );
65         if (msg != null && rb != null)
66         {
67             String JavaDoc check = rb.getString( msg );
68             if (check != null)
69             msg = check;
70         }
71         format( l, srcClass, srcMeth, msg, params, t);
72     }
73
74     private void format(MLevel l, String JavaDoc srcClass, String JavaDoc srcMeth, String JavaDoc msg, Object JavaDoc[] params, Throwable JavaDoc t)
75     { System.err.println( formatString( l, srcClass, srcMeth, msg, params, t ) ); }
76
77     private String JavaDoc formatString(MLevel l, String JavaDoc srcClass, String JavaDoc srcMeth, String JavaDoc msg, Object JavaDoc[] params, Throwable JavaDoc t)
78     {
79         boolean add_parens = (srcMeth != null && ! srcMeth.endsWith(")"));
80         
81         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(256);
82         sb.append(l.getLineHeader());
83         sb.append(' ');
84         if (srcClass != null && srcMeth != null)
85         {
86             sb.append('[');
87             sb.append( srcClass );
88             sb.append( '.' );
89             sb.append( srcMeth );
90             if (add_parens)
91             sb.append("()");
92             sb.append( ']' );
93         }
94         else if (srcClass != null)
95         {
96             sb.append('[');
97             sb.append( srcClass );
98             sb.append( ']' );
99         }
100         else if (srcMeth != null)
101         {
102             sb.append('[');
103             sb.append( srcMeth );
104             if (add_parens)
105             sb.append("()");
106             sb.append( ']' );
107         }
108         if (msg == null)
109         {
110             if (params != null)
111             {
112                 sb.append("params: ");
113                 for (int i = 0, len = params.length; i < len; ++i)
114                 {
115                     if (i != 0) sb.append(", ");
116                     sb.append( params[i] );
117                 }
118             }
119         }
120         else
121         {
122             if (params == null)
123             sb.append( msg );
124             else
125             {
126                 MessageFormat mfmt = new MessageFormat( msg );
127                 sb.append( mfmt.format( params ) );
128             }
129         }
130         
131         if (t != null)
132         sb.append( ThrowableUtils.extractStackTrace( t ) );
133
134         return sb.toString();
135     }
136
137     public ResourceBundle getResourceBundle()
138     {
139         //warn("Using logger " + this.getClass().getName() + ", which does not support ResourceBundles.");
140
return null;
141     }
142
143     public String JavaDoc getResourceBundleName()
144     { return null; }
145
146     public void setFilter(Object JavaDoc java14Filter) throws SecurityException JavaDoc
147     {
148         warning("Using FallbackMLog -- Filters not supported!");
149     }
150
151     public Object JavaDoc getFilter()
152     {
153         return null;
154     }
155
156     public void log(MLevel l, String JavaDoc msg)
157     {
158         if ( isLoggable( l ) )
159         format( l, null, null, msg, null, null );
160     }
161
162     public void log(MLevel l, String JavaDoc msg, Object JavaDoc param)
163     {
164         if ( isLoggable( l ) )
165         format( l, null, null, msg, new Object JavaDoc[] { param }, null );
166     }
167
168     public void log(MLevel l,String JavaDoc msg, Object JavaDoc[] params)
169     {
170         if ( isLoggable( l ) )
171         format( l, null, null, msg, params, null );
172     }
173
174     public void log(MLevel l, String JavaDoc msg, Throwable JavaDoc t)
175     {
176         if ( isLoggable( l ) )
177         format( l, null, null, msg, null, t );
178     }
179
180     public void logp(MLevel l, String JavaDoc srcClass, String JavaDoc srcMeth, String JavaDoc msg)
181     {
182         if ( isLoggable( l ) )
183         format( l, srcClass, srcMeth, msg, null, null );
184     }
185
186     public void logp(MLevel l, String JavaDoc srcClass, String JavaDoc srcMeth, String JavaDoc msg, Object JavaDoc param)
187     {
188         if ( isLoggable( l ) )
189         format( l, srcClass, srcMeth, msg, new Object JavaDoc[] { param }, null );
190     }
191
192     public void logp(MLevel l, String JavaDoc srcClass, String JavaDoc srcMeth, String JavaDoc msg, Object JavaDoc[] params)
193     {
194         if ( isLoggable( l ) )
195         format( l, srcClass, srcMeth, msg, params, null );
196     }
197
198     public void logp(MLevel l, String JavaDoc srcClass, String JavaDoc srcMeth, String JavaDoc msg, Throwable JavaDoc t)
199     {
200         if ( isLoggable( l ) )
201         format( l, srcClass, srcMeth, msg, null, t );
202     }
203
204     public void logrb(MLevel l, String JavaDoc srcClass, String JavaDoc srcMeth, String JavaDoc rb, String JavaDoc msg)
205     {
206         if ( isLoggable( l ) )
207         formatrb( l, srcClass, srcMeth, rb, msg, null, null );
208     }
209
210     public void logrb(MLevel l, String JavaDoc srcClass, String JavaDoc srcMeth, String JavaDoc rb, String JavaDoc msg, Object JavaDoc param)
211     {
212         if ( isLoggable( l ) )
213         formatrb( l, srcClass, srcMeth, rb, msg, new Object JavaDoc[] { param }, null );
214     }
215
216     public void logrb(MLevel l, String JavaDoc srcClass, String JavaDoc srcMeth, String JavaDoc rb, String JavaDoc msg, Object JavaDoc[] params)
217     {
218         if ( isLoggable( l ) )
219         formatrb( l, srcClass, srcMeth, rb, msg, params, null );
220     }
221
222     public void logrb(MLevel l, String JavaDoc srcClass, String JavaDoc srcMeth, String JavaDoc rb, String JavaDoc msg, Throwable JavaDoc t)
223     {
224         if ( isLoggable( l ) )
225         formatrb( l, srcClass, srcMeth, rb, msg, null, t );
226     }
227
228     public void entering(String JavaDoc srcClass, String JavaDoc srcMeth)
229     {
230         if ( isLoggable( MLevel.FINER ) )
231         format(MLevel.FINER, srcClass, srcMeth, "Entering method.", null, null);
232     }
233
234     public void entering(String JavaDoc srcClass, String JavaDoc srcMeth, Object JavaDoc param)
235     {
236         if ( isLoggable( MLevel.FINER ) )
237         format(MLevel.FINER, srcClass, srcMeth, "Entering method with argument " + param, null, null);
238     }
239
240     public void entering(String JavaDoc srcClass, String JavaDoc srcMeth, Object JavaDoc[] params)
241     {
242         if ( isLoggable( MLevel.FINER ) )
243         {
244             if (params == null)
245             entering( srcClass, srcMeth );
246             else
247             {
248                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc(128);
249                 sb.append("( ");
250                 for (int i = 0, len = params.length; i < len; ++i)
251                 {
252                     if (i != 0) sb.append(", ");
253                     sb.append( params[i] );
254                 }
255                 sb.append(" )");
256                 format(MLevel.FINER, srcClass, srcMeth, "Entering method with arguments " + sb.toString(), null, null);
257             }
258         }
259     }
260
261     public void exiting(String JavaDoc srcClass, String JavaDoc srcMeth)
262     {
263         if ( isLoggable( MLevel.FINER ) )
264         format(MLevel.FINER, srcClass, srcMeth, "Exiting method.", null, null);
265     }
266
267     public void exiting(String JavaDoc srcClass, String JavaDoc srcMeth, Object JavaDoc result)
268     {
269         if ( isLoggable( MLevel.FINER ) )
270         format(MLevel.FINER, srcClass, srcMeth, "Exiting method with result " + result, null, null);
271     }
272
273     public void throwing(String JavaDoc srcClass, String JavaDoc srcMeth, Throwable JavaDoc t)
274     {
275         if ( isLoggable( MLevel.FINE ) )
276         format(MLevel.FINE, srcClass, srcMeth, "Throwing exception." , null, t);
277     }
278
279     public void severe(String JavaDoc msg)
280     {
281         if ( isLoggable( MLevel.SEVERE ) )
282         format(MLevel.SEVERE, null, null, msg, null, null);
283     }
284
285     public void warning(String JavaDoc msg)
286     {
287         if ( isLoggable( MLevel.WARNING ) )
288         format(MLevel.WARNING, null, null, msg, null, null);
289     }
290
291     public void info(String JavaDoc msg)
292     {
293         if ( isLoggable( MLevel.INFO ) )
294         format(MLevel.INFO, null, null, msg, null, null);
295     }
296
297     public void config(String JavaDoc msg)
298     {
299         if ( isLoggable( MLevel.CONFIG ) )
300         format(MLevel.CONFIG, null, null, msg, null, null);
301     }
302
303     public void fine(String JavaDoc msg)
304     {
305         if ( isLoggable( MLevel.FINE ) )
306         format(MLevel.FINE, null, null, msg, null, null);
307     }
308
309     public void finer(String JavaDoc msg)
310     {
311         if ( isLoggable( MLevel.FINER ) )
312         format(MLevel.FINER, null, null, msg, null, null);
313     }
314
315     public void finest(String JavaDoc msg)
316     {
317         if ( isLoggable( MLevel.FINEST ) )
318         format(MLevel.FINEST, null, null, msg, null, null);
319     }
320
321     public void setLevel(MLevel l) throws SecurityException JavaDoc
322     { this.cutoffLevel = l; }
323                           
324     public synchronized MLevel getLevel()
325     { return cutoffLevel; }
326
327     public synchronized boolean isLoggable(MLevel l)
328     { return (l.intValue() >= cutoffLevel.intValue()); }
329
330     public String JavaDoc getName()
331     { return "global"; }
332
333     public void addHandler(Object JavaDoc h) throws SecurityException JavaDoc
334     {
335         warning("Using FallbackMLog -- Handlers not supported.");
336     }
337
338     public void removeHandler(Object JavaDoc h) throws SecurityException JavaDoc
339     {
340         warning("Using FallbackMLog -- Handlers not supported.");
341     }
342
343     public Object JavaDoc[] getHandlers()
344     {
345         warning("Using FallbackMLog -- Handlers not supported.");
346         return new Object JavaDoc[0];
347     }
348
349     public void setUseParentHandlers(boolean uph)
350     {
351         warning("Using FallbackMLog -- Handlers not supported.");
352     }
353
354     public boolean getUseParentHandlers()
355     { return false; }
356     }
357 }
Popular Tags