KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.List JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import com.mchange.v1.util.StringTokenizerUtils;
29 import com.mchange.v2.cfg.MultiPropertiesConfig;
30
31 public abstract class MLog
32 {
33     final static NameTransformer transformer;
34     final static MLog mlog;
35
36     final static MultiPropertiesConfig CONFIG;
37
38     final static MLogger logger;
39
40     static
41     {
42     String JavaDoc[] defaults = new String JavaDoc[]
43     {
44         "/com/mchange/v2/log/default-mchange-log.properties",
45         "/mchange-log.properties",
46         "/"
47     };
48     CONFIG = MultiPropertiesConfig.readVmConfig( defaults, null );
49
50     String JavaDoc classnamesStr = CONFIG.getProperty("com.mchange.v2.log.MLog");
51     String JavaDoc[] classnames = null;
52     if (classnamesStr == null)
53         classnamesStr = CONFIG.getProperty("com.mchange.v2.log.mlog");
54     if (classnamesStr != null)
55         classnames = StringTokenizerUtils.tokenizeToArray( classnamesStr, ", \t\r\n" );
56
57     boolean warn = false;
58     MLog tmpml = null;
59     if (classnames != null)
60         tmpml = findByClassnames( classnames );
61     if (tmpml == null)
62         tmpml = findByClassnames( MLogClasses.CLASSNAMES );
63     if (tmpml == null)
64         {
65         warn = true;
66         tmpml = new FallbackMLog();
67         }
68     mlog = tmpml;
69     if (warn)
70         info("Using " + mlog.getClass().getName() + " -- Named logger's not supported, everything goes to System.err.");
71
72     logger = mlog.getLogger( MLog.class );
73     String JavaDoc loggerDesc = mlog.getClass().getName();
74     if ("com.mchange.v2.log.jdk14logging.Jdk14MLog".equals( loggerDesc ))
75         loggerDesc = "java 1.4+ standard";
76     else if ("com.mchange.v2.log.log4j.Log4jMLog".equals( loggerDesc ))
77         loggerDesc = "log4j";
78     
79     if (logger.isLoggable( MLevel.INFO ))
80         logger.log( MLevel.INFO, "MLog clients using " + loggerDesc + " logging.");
81
82     NameTransformer tmpt = null;
83     String JavaDoc tClassName = CONFIG.getProperty("com.mchange.v2.log.NameTransformer");
84     if (tClassName == null)
85         tClassName = CONFIG.getProperty("com.mchange.v2.log.nametransformer");
86     try
87         {
88         if (tClassName != null)
89             tmpt = (NameTransformer) Class.forName( tClassName ).newInstance();
90         }
91     catch ( Exception JavaDoc e )
92         {
93         System.err.println("Failed to instantiate com.mchange.v2.log.NameTransformer '" + tClassName + "'!");
94         e.printStackTrace();
95         }
96     transformer = tmpt;
97
98     //System.err.println(mlog);
99
}
100
101     public static MLog findByClassnames( String JavaDoc[] classnames )
102     {
103     List JavaDoc attempts = null;
104     for (int i = 0, len = classnames.length; i < len; ++i)
105         {
106         try { return (MLog) Class.forName( classnames[i] ).newInstance(); }
107         catch (Exception JavaDoc e)
108             {
109             if (attempts == null)
110                 attempts = new ArrayList JavaDoc();
111             attempts.add( classnames[i] );
112 // System.err.println("com.mchange.v2.log.MLog '" + classnames[i] + "' could not be loaded!");
113
// e.printStackTrace();
114
}
115         }
116     System.err.println("Tried without success to load the following MLog classes:");
117     for (int i = 0, len = attempts.size(); i < len; ++i)
118         System.err.println("\t" + attempts.get(i));
119     return null;
120     }
121
122     public static MLog instance()
123     { return mlog; }
124
125     public static MLogger getLogger(String JavaDoc name)
126     {
127     MLogger out;
128     if ( transformer == null )
129         out = instance().getMLogger( name );
130     else
131         {
132         String JavaDoc xname = transformer.transformName( name );
133         if (xname != null)
134             out = instance().getMLogger( xname );
135         else
136             out = instance().getMLogger( name );
137         }
138     return out;
139     }
140
141     public static MLogger getLogger(Class JavaDoc cl)
142     {
143     MLogger out;
144     if ( transformer == null )
145         out = instance().getMLogger( cl );
146     else
147         {
148         String JavaDoc xname = transformer.transformName( cl );
149         if (xname != null)
150             out = instance().getMLogger( xname );
151         else
152             out = instance().getMLogger( cl );
153         }
154     return out;
155     }
156
157     public static MLogger getLogger()
158     {
159     MLogger out;
160     if ( transformer == null )
161         out = instance().getMLogger();
162     else
163         {
164         String JavaDoc xname = transformer.transformName();
165         if (xname != null)
166             out = instance().getMLogger( xname );
167         else
168             out = instance().getMLogger();
169         }
170     return out;
171     }
172
173     public static void log(MLevel l, String JavaDoc msg)
174     { instance().getLogger().log( l, msg ); }
175
176     public static void log(MLevel l, String JavaDoc msg, Object JavaDoc param)
177     { instance().getLogger().log( l, msg, param ); }
178
179     public static void log(MLevel l,String JavaDoc msg, Object JavaDoc[] params)
180     { instance().getLogger().log( l, msg, params ); }
181
182     public static void log(MLevel l, String JavaDoc msg,Throwable JavaDoc t)
183     { instance().getLogger().log( l, msg, t ); }
184
185     public static void logp(MLevel l, String JavaDoc srcClass, String JavaDoc srcMeth, String JavaDoc msg)
186     { instance().getLogger().logp( l, srcClass, srcMeth, msg ); }
187
188     public static void logp(MLevel l, String JavaDoc srcClass, String JavaDoc srcMeth, String JavaDoc msg, Object JavaDoc param)
189     { instance().getLogger().logp( l, srcClass, srcMeth, msg, param ); }
190
191     public static void logp(MLevel l, String JavaDoc srcClass, String JavaDoc srcMeth, String JavaDoc msg, Object JavaDoc[] params)
192     { instance().getLogger().logp( l, srcClass, srcMeth, msg, params ); }
193
194     public static void logp(MLevel l, String JavaDoc srcClass, String JavaDoc srcMeth, String JavaDoc msg, Throwable JavaDoc t)
195     { instance().getLogger().logp( l, srcClass, srcMeth, msg, t ); }
196
197     public static void logrb(MLevel l, String JavaDoc srcClass, String JavaDoc srcMeth, String JavaDoc rb, String JavaDoc msg)
198     { instance().getLogger().logp( l, srcClass, srcMeth, rb, msg ); }
199
200     public static void logrb(MLevel l, String JavaDoc srcClass, String JavaDoc srcMeth, String JavaDoc rb, String JavaDoc msg, Object JavaDoc param)
201     { instance().getLogger().logrb( l, srcClass, srcMeth, rb, msg, param ); }
202
203     public static void logrb(MLevel l, String JavaDoc srcClass, String JavaDoc srcMeth, String JavaDoc rb, String JavaDoc msg, Object JavaDoc[] params)
204     { instance().getLogger().logrb( l, srcClass, srcMeth, rb, msg, params ); }
205
206     public static void logrb(MLevel l, String JavaDoc srcClass, String JavaDoc srcMeth, String JavaDoc rb, String JavaDoc msg, Throwable JavaDoc t)
207     { instance().getLogger().logrb( l, srcClass, srcMeth, rb, msg, t ); }
208
209     public static void entering(String JavaDoc srcClass, String JavaDoc srcMeth)
210     { instance().getLogger().entering( srcClass, srcMeth ); }
211
212     public static void entering(String JavaDoc srcClass, String JavaDoc srcMeth, Object JavaDoc param)
213     { instance().getLogger().entering( srcClass, srcMeth, param ); }
214
215     public static void entering(String JavaDoc srcClass, String JavaDoc srcMeth, Object JavaDoc params[])
216     { instance().getLogger().entering( srcClass, srcMeth, params ); }
217
218     public static void exiting(String JavaDoc srcClass, String JavaDoc srcMeth)
219     { instance().getLogger().exiting( srcClass, srcMeth ); }
220
221     public static void exiting(String JavaDoc srcClass, String JavaDoc srcMeth, Object JavaDoc result)
222     { instance().getLogger().exiting( srcClass, srcMeth, result ); }
223
224     public static void throwing(String JavaDoc srcClass, String JavaDoc srcMeth, Throwable JavaDoc t)
225     { instance().getLogger().throwing( srcClass, srcMeth, t); }
226
227     public static void severe(String JavaDoc msg)
228     { instance().getLogger().severe( msg ); }
229
230     public static void warning(String JavaDoc msg)
231     { instance().getLogger().warning( msg ); }
232
233     public static void info(String JavaDoc msg)
234     { instance().getLogger().info( msg ); }
235
236     public static void config(String JavaDoc msg)
237     { instance().getLogger().config( msg ); }
238
239     public static void fine(String JavaDoc msg)
240     { instance().getLogger().fine( msg ); }
241
242     public static void finer(String JavaDoc msg)
243     { instance().getLogger().finer( msg ); }
244
245     public static void finest(String JavaDoc msg)
246     { instance().getLogger().finest( msg ); }
247
248     public abstract MLogger getMLogger(String JavaDoc name);
249     public abstract MLogger getMLogger(Class JavaDoc cl);
250     public abstract MLogger getMLogger();
251 }
Popular Tags