KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > log > Log


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package mx4j.log;
10
11 import java.security.AccessController JavaDoc;
12 import java.security.PrivilegedAction JavaDoc;
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15 import javax.management.RuntimeOperationsException JavaDoc;
16
17 import mx4j.MX4JSystemKeys;
18
19 /**
20  * Main class for the log service. <p>
21  * The system property 'mx4j.log.priority' controls the priority of the standard logging, and defaults to 'warn'.
22  * Possible values are, from least to greatest priority, the following (case insensitive):
23  * <ul>
24  * <li>trace
25  * <li>debug
26  * <li>info
27  * <li>warn
28  * <li>error
29  * <li>fatal
30  * </ul>
31  *
32  * @version $Revision: 1.7 $
33  */

34 public class Log
35 {
36    private static Logger m_prototype;
37    private static Map JavaDoc m_prototypeMap = new HashMap JavaDoc();
38    private static Map JavaDoc m_loggerCache = new HashMap JavaDoc();
39    private static int m_defaultPriority;
40
41    static
42    {
43       // Do not require callers up in the stack to have this permission
44
String JavaDoc priority = (String JavaDoc)AccessController.doPrivileged(new PrivilegedAction JavaDoc()
45       {
46          public Object JavaDoc run()
47          {
48             return System.getProperty(MX4JSystemKeys.MX4J_LOG_PRIORITY);
49          }
50       });
51       if ("trace".equalsIgnoreCase(priority))
52       {
53          m_defaultPriority = Logger.TRACE;
54       }
55       else if ("debug".equalsIgnoreCase(priority))
56       {
57          m_defaultPriority = Logger.DEBUG;
58       }
59       else if ("info".equalsIgnoreCase(priority))
60       {
61          m_defaultPriority = Logger.INFO;
62       }
63       else if ("warn".equalsIgnoreCase(priority))
64       {
65          m_defaultPriority = Logger.WARN;
66       }
67       else if ("error".equalsIgnoreCase(priority))
68       {
69          m_defaultPriority = Logger.ERROR;
70       }
71       else if ("fatal".equalsIgnoreCase(priority))
72       {
73          m_defaultPriority = Logger.FATAL;
74       }
75       else
76       {
77          m_defaultPriority = Logger.INFO;
78       }
79
80       String JavaDoc prototype = (String JavaDoc)AccessController.doPrivileged(new PrivilegedAction JavaDoc()
81       {
82          public Object JavaDoc run()
83          {
84             return System.getProperty(MX4JSystemKeys.MX4J_LOG_PROTOTYPE);
85          }
86       });
87       if (prototype != null && prototype.trim().length() > 0)
88       {
89          try
90          {
91             ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
92             Class JavaDoc cls = cl.loadClass(prototype);
93             redirectTo((Logger)cls.newInstance());
94          }
95          catch (Exception JavaDoc x)
96          {
97             x.printStackTrace();
98             // Do nothing else: the user will see the exception trace
99
// and understand that the property was wrong
100
}
101       }
102    }
103
104    private Log()
105    {
106    }
107
108    /**
109     * Sets the default priority for all loggers.
110     *
111     * @see #setDefaultPriority
112     */

113    public static void setDefaultPriority(int priority)
114    {
115       switch (priority)
116       {
117          case Logger.TRACE:
118             m_defaultPriority = Logger.TRACE;
119             break;
120          case Logger.DEBUG:
121             m_defaultPriority = Logger.DEBUG;
122             break;
123          case Logger.INFO:
124             m_defaultPriority = Logger.INFO;
125             break;
126          case Logger.WARN:
127             m_defaultPriority = Logger.WARN;
128             break;
129          case Logger.ERROR:
130             m_defaultPriority = Logger.ERROR;
131             break;
132          case Logger.FATAL:
133             m_defaultPriority = Logger.FATAL;
134             break;
135          default:
136             m_defaultPriority = Logger.WARN;
137             break;
138       }
139    }
140
141    /**
142     * Returns the default priority.
143     *
144     * @see #setDefaultPriority
145     */

146    public static int getDefaultPriority()
147    {
148       return m_defaultPriority;
149    }
150
151    /**
152     * Returns a new instance of a Logger associated with the given <code>category</code>;
153     * if {@link #redirectTo} has been called then a new instance of the prototype Logger, associated with the given
154     * <code>category<code>, is returned. This requires the prototype Logger class to have a public parameterless
155     * constructor.
156     */

157    public static Logger getLogger(String JavaDoc category)
158    {
159       if (category == null)
160       {
161          throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc("Category cannot be null"));
162       }
163
164       synchronized (m_loggerCache)
165       {
166          Logger logger = (Logger)m_loggerCache.get(category);
167          if (logger == null)
168          {
169             // Try to see if a delegate for this category overrides other settings
170
Logger prototype = null;
171             synchronized (m_prototypeMap)
172             {
173                prototype = (Logger)m_prototypeMap.get(category);
174             }
175             if (prototype == null)
176             {
177                // Try to see if a prototype for all categories has been set
178
if (m_prototype != null)
179                {
180                   logger = createLogger(m_prototype, category);
181                }
182                else
183                {
184                   logger = createLogger(null, category);
185                }
186             }
187             else
188             {
189                logger = createLogger(prototype, category);
190             }
191
192             // cache it
193
m_loggerCache.put(category, logger);
194          }
195          return logger;
196       }
197    }
198
199    private static Logger createLogger(Logger prototype, String JavaDoc category)
200    {
201       Logger logger = null;
202       try
203       {
204          logger = prototype == null ? new Logger() : (Logger)prototype.getClass().newInstance();
205       }
206       catch (Exception JavaDoc x)
207       {
208          x.printStackTrace();
209          logger = new Logger();
210       }
211       logger.setCategory(category);
212       logger.setPriority(m_defaultPriority);
213       return logger;
214    }
215
216    /**
217     * Tells to the log service to use the given <code>delegate</code> Logger to perform logging. <br>
218     * Use a null delegate to remove redirection.
219     *
220     * @see #getLogger
221     */

222    public static void redirectTo(Logger prototype)
223    {
224       m_prototype = prototype;
225
226       // Clear the cache, as we want requests for new loggers to be generated again.
227
synchronized (m_loggerCache)
228       {
229          m_loggerCache.clear();
230       }
231    }
232
233    /**
234     * Tells to the log service to use the given <code>delegate</code> Logger to perform logging for the given
235     * category (that cannot be null). <br>
236     * Settings made using this method overrides the ones made with {@link #redirectTo(Logger) redirectTo}, meaning
237     * that it is possible to redirect all the log to a certain delegate but certain categories.
238     * Use a null delegate to remove redirection for the specified category.
239     *
240     * @see #getLogger
241     */

242    public static void redirectTo(Logger prototype, String JavaDoc category)
243    {
244       if (category == null)
245       {
246          throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc("Category cannot be null"));
247       }
248
249       if (prototype == null)
250       {
251          // Remove the redirection
252
synchronized (m_prototypeMap)
253          {
254             m_prototypeMap.remove(category);
255          }
256
257          // Clear the cache for this category
258
synchronized (m_loggerCache)
259          {
260             m_loggerCache.remove(category);
261          }
262       }
263       else
264       {
265          // Put or replace
266
synchronized (m_prototypeMap)
267          {
268             m_prototypeMap.put(category, prototype);
269          }
270
271          // Clear the cache for this category
272
synchronized (m_loggerCache)
273          {
274             m_loggerCache.remove(category);
275          }
276       }
277    }
278 }
279
Popular Tags