KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v2 > log > log4j > Log4jMLog


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.log4j;
25
26 import java.text.*;
27 import java.util.*;
28
29 import com.mchange.v2.log.*;
30 import com.mchange.v2.util.DoubleWeakHashMap;
31
32 import org.apache.log4j.*;
33
34 public final class Log4jMLog extends MLog
35 {
36     final static String JavaDoc CHECK_CLASS = "org.apache.log4j.Logger";
37
38     MLogger global = null;
39
40     public Log4jMLog() throws ClassNotFoundException JavaDoc
41     { Class.forName( CHECK_CLASS ); }
42
43     public MLogger getMLogger(String JavaDoc name)
44     {
45         Logger lg = Logger.getLogger(name);
46         return new Log4jMLogger( lg );
47     }
48
49     public MLogger getMLogger(Class JavaDoc cl)
50     {
51         Logger lg = Logger.getLogger(cl);
52         return new Log4jMLogger( lg );
53     }
54
55
56     public MLogger getMLogger()
57     {
58         Logger lg = Logger.getRootLogger();
59         return new Log4jMLogger( lg );
60     }
61
62     private final static class Log4jMLogger implements MLogger
63     {
64         final static String JavaDoc FQCN = Log4jMLogger.class.getName();
65
66         MLevel myLevel = null;
67         Logger logger;
68
69         Log4jMLogger( Logger logger )
70         { this.logger = logger; }
71
72         private static MLevel guessMLevel(Level lvl)
73         {
74             if (lvl == null)
75                 return null;
76             else if (lvl == Level.ALL)
77                 return MLevel.ALL;
78             else if (lvl == Level.DEBUG)
79                 return MLevel.FINEST;
80             else if (lvl == Level.ERROR)
81                 return MLevel.SEVERE;
82             else if (lvl == Level.FATAL)
83                 return MLevel.SEVERE;
84             else if (lvl == Level.INFO)
85                 return MLevel.INFO;
86             else if (lvl == Level.OFF)
87                 return MLevel.OFF;
88             else if (lvl == Level.WARN)
89                 return MLevel.WARNING;
90             else
91                 throw new IllegalArgumentException JavaDoc("Unknown level: " + lvl);
92         }
93
94         private static Level level(MLevel lvl)
95         {
96             if (lvl == null)
97                 return null;
98             else if (lvl == MLevel.ALL)
99                 return Level.ALL;
100             else if (lvl == MLevel.CONFIG)
101                 return Level.DEBUG;
102             else if (lvl == MLevel.FINE)
103                 return Level.DEBUG;
104             else if (lvl == MLevel.FINER)
105                 return Level.DEBUG;
106             else if (lvl == MLevel.FINEST)
107                 return Level.DEBUG;
108             else if (lvl == MLevel.INFO)
109                 return Level.INFO;
110             else if (lvl == MLevel.INFO)
111                 return Level.OFF;
112             else if (lvl == MLevel.SEVERE)
113                 return Level.ERROR;
114             else if (lvl == MLevel.WARNING)
115                 return Level.WARN;
116             else
117                 throw new IllegalArgumentException JavaDoc("Unknown MLevel: " + lvl);
118         }
119
120         private static String JavaDoc createMessage(String JavaDoc srcClass, String JavaDoc srcMeth, String JavaDoc msg)
121         {
122             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(511);
123             sb.append("[class: ");
124             sb.append( srcClass );
125             sb.append("; method: ");
126             sb.append( srcMeth );
127             if (! srcMeth.endsWith(")"))
128                 sb.append("()");
129             sb.append("] ");
130             sb.append( msg );
131             return sb.toString();
132         }
133
134         private static String JavaDoc createMessage(String JavaDoc srcMeth, String JavaDoc msg)
135         {
136             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(511);
137             sb.append("[method: ");
138             sb.append( srcMeth );
139             if (! srcMeth.endsWith(")"))
140                 sb.append("()");
141             sb.append("] ");
142             sb.append( msg );
143             return sb.toString();
144         }
145
146         public ResourceBundle getResourceBundle()
147         { return null; }
148
149         public String JavaDoc getResourceBundleName()
150         { return null; }
151
152         public void setFilter(Object JavaDoc java14Filter) throws SecurityException JavaDoc
153         { warning("setFilter() not supported by MLogger " + this.getClass().getName()); }
154
155         public Object JavaDoc getFilter()
156         { return null; }
157
158         private void log(Level lvl, Object JavaDoc msg, Throwable JavaDoc t)
159         { logger.log( FQCN, lvl, msg, t ); }
160
161         public void log(MLevel l, String JavaDoc msg)
162         { log( level(l), msg, null); }
163
164         public void log(MLevel l, String JavaDoc msg, Object JavaDoc param)
165         { log( level(l), (msg!=null ? MessageFormat.format(msg, new Object JavaDoc[] { param }) : null), null); }
166
167         public void log(MLevel l,String JavaDoc msg, Object JavaDoc[] params)
168         { log( level(l), (msg!=null ? MessageFormat.format(msg, params) : null), null); }
169
170         public void log(MLevel l, String JavaDoc msg, Throwable JavaDoc t)
171         { log( level(l), msg, t); }
172
173         public void logp(MLevel l, String JavaDoc srcClass, String JavaDoc srcMeth, String JavaDoc msg)
174         { log( level(l), createMessage( srcClass, srcMeth, msg), null); }
175
176         public void logp(MLevel l, String JavaDoc srcClass, String JavaDoc srcMeth, String JavaDoc msg, Object JavaDoc param)
177         { log( level(l), createMessage( srcClass, srcMeth, (msg!=null ? MessageFormat.format(msg, new Object JavaDoc[] {param}) : null) ), null); }
178
179         public void logp(MLevel l, String JavaDoc srcClass, String JavaDoc srcMeth, String JavaDoc msg, Object JavaDoc[] params)
180         { log( level(l), createMessage( srcClass, srcMeth, (msg!=null ? MessageFormat.format(msg, params) : null) ), null); }
181
182         public void logp(MLevel l, String JavaDoc srcClass, String JavaDoc srcMeth, String JavaDoc msg, Throwable JavaDoc t)
183         { log( level(l), createMessage( srcClass, srcMeth, msg ), t); }
184
185         public void logrb(MLevel l, String JavaDoc srcClass, String JavaDoc srcMeth, String JavaDoc rb, String JavaDoc msg)
186         { log( level(l), createMessage( srcClass, srcMeth, formatMessage(rb, msg, null) ), null); }
187
188         public void logrb(MLevel l, String JavaDoc srcClass, String JavaDoc srcMeth, String JavaDoc rb, String JavaDoc msg, Object JavaDoc param)
189         { log( level(l), createMessage( srcClass, srcMeth, formatMessage(rb, msg, new Object JavaDoc[] { param } ) ), null); }
190
191         public void logrb(MLevel l, String JavaDoc srcClass, String JavaDoc srcMeth, String JavaDoc rb, String JavaDoc msg, Object JavaDoc[] params)
192         { log( level(l), createMessage( srcClass, srcMeth, formatMessage(rb, msg, params) ), null); }
193
194         public void logrb(MLevel l, String JavaDoc srcClass, String JavaDoc srcMeth, String JavaDoc rb, String JavaDoc msg, Throwable JavaDoc t)
195         { log( level(l), createMessage( srcClass, srcMeth, formatMessage(rb, msg, null) ), t); }
196
197         public void entering(String JavaDoc srcClass, String JavaDoc srcMeth)
198         { log( Level.DEBUG, createMessage( srcClass, srcMeth, "entering method." ), null); }
199
200         public void entering(String JavaDoc srcClass, String JavaDoc srcMeth, Object JavaDoc param)
201         { log( Level.DEBUG, createMessage( srcClass, srcMeth, "entering method... param: " + param.toString() ), null); }
202
203         public void entering(String JavaDoc srcClass, String JavaDoc srcMeth, Object JavaDoc params[])
204         { log( Level.DEBUG, createMessage( srcClass, srcMeth, "entering method... " + LogUtils.createParamsList( params ) ), null); }
205
206         public void exiting(String JavaDoc srcClass, String JavaDoc srcMeth)
207         { log( Level.DEBUG, createMessage( srcClass, srcMeth, "exiting method." ), null); }
208
209         public void exiting(String JavaDoc srcClass, String JavaDoc srcMeth, Object JavaDoc result)
210         { log( Level.DEBUG, createMessage( srcClass, srcMeth, "exiting method... result: " + result.toString() ), null); }
211
212         public void throwing(String JavaDoc srcClass, String JavaDoc srcMeth, Throwable JavaDoc t)
213         { log( Level.DEBUG, createMessage( srcClass, srcMeth, "throwing exception... " ), t); }
214
215         public void severe(String JavaDoc msg)
216         { log( Level.ERROR, msg, null); }
217
218         public void warning(String JavaDoc msg)
219         { log( Level.WARN, msg, null); }
220
221         public void info(String JavaDoc msg)
222         { log( Level.INFO, msg, null); }
223
224         public void config(String JavaDoc msg)
225         { log( Level.DEBUG, msg, null); }
226
227         public void fine(String JavaDoc msg)
228         { log( Level.DEBUG, msg, null); }
229
230         public void finer(String JavaDoc msg)
231         { log( Level.DEBUG, msg, null); }
232
233         public void finest(String JavaDoc msg)
234         { log( Level.DEBUG, msg, null); }
235
236         public synchronized void setLevel(MLevel l) throws SecurityException JavaDoc
237         {
238             logger.setLevel( level( l ) );
239             myLevel = l;
240         }
241
242         public synchronized MLevel getLevel()
243         {
244             //System.err.println( logger.getLevel() );
245
if (myLevel == null)
246                 myLevel = guessMLevel( logger.getLevel() );
247             return myLevel;
248         }
249
250         public boolean isLoggable(MLevel l)
251         {
252             //System.err.println( "MLevel: " + l + "; isEnabledFor(): " + logger.isEnabledFor( level(l) ) + "; getLevel(): " + getLevel() +
253
//"; MLog.getLogger().getLevel(): " + MLog.getLogger().getLevel());
254
//new Exception("WHADDAFUC").printStackTrace();
255
return logger.isEnabledFor( level(l) );
256         }
257
258         public String JavaDoc getName()
259         { return logger.getName(); }
260
261         public void addHandler(Object JavaDoc h) throws SecurityException JavaDoc
262         {
263             if (! (h instanceof Appender))
264                 throw new IllegalArgumentException JavaDoc("The 'handler' " + h + " is not compatible with MLogger " + this);
265             logger.addAppender( (Appender) h );
266         }
267
268         public void removeHandler(Object JavaDoc h) throws SecurityException JavaDoc
269         {
270             if (! (h instanceof Appender))
271                 throw new IllegalArgumentException JavaDoc("The 'handler' " + h + " is not compatible with MLogger " + this);
272             logger.removeAppender( (Appender) h );
273         }
274
275         public Object JavaDoc[] getHandlers()
276         {
277             List tmp = new LinkedList();
278             for (Enumeration e = logger.getAllAppenders(); e.hasMoreElements(); )
279                 tmp.add( e.nextElement() );
280             return tmp.toArray();
281         }
282
283         public void setUseParentHandlers(boolean uph)
284         { logger.setAdditivity( uph ); }
285
286         public boolean getUseParentHandlers()
287         { return logger.getAdditivity(); }
288     }
289
290     private static String JavaDoc formatMessage( String JavaDoc rbname, String JavaDoc msg, Object JavaDoc[] params )
291     {
292         if ( msg == null )
293         {
294             if (params == null)
295                 return "";
296             else
297                 return LogUtils.createParamsList( params );
298         }
299         else
300         {
301             ResourceBundle rb = ResourceBundle.getBundle( rbname );
302             if (rb != null)
303             {
304                 String JavaDoc check = rb.getString( msg );
305                 if (check != null)
306                     msg = check;
307             }
308             return (params == null ? msg : MessageFormat.format( msg, params ));
309         }
310     }
311 }
Popular Tags