KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > log > LogConfig


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.log;
31
32 import com.caucho.config.ConfigException;
33 import com.caucho.config.types.RawString;
34 import com.caucho.jmx.Jmx;
35 import com.caucho.loader.CloseListener;
36 import com.caucho.loader.Environment;
37 import com.caucho.util.L10N;
38 import com.caucho.vfs.WriteStream;
39
40 import javax.annotation.PostConstruct;
41 import java.util.ArrayList JavaDoc;
42 import java.util.logging.Formatter JavaDoc;
43 import java.util.logging.Handler JavaDoc;
44 import java.util.logging.Level JavaDoc;
45 import java.util.logging.Logger JavaDoc;
46
47 /**
48  * Environment-specific configuration.
49  */

50 public class LogConfig extends RotateLog {
51   private static final L10N L = new L10N(LogConfig.class);
52   
53   private ArrayList JavaDoc<Handler JavaDoc> _handlers;
54   private Formatter JavaDoc _formatter;
55   private String JavaDoc _timestamp;
56
57   private ArrayList JavaDoc<SubLogger> _subLoggers = new ArrayList JavaDoc<SubLogger>();
58   private SubLogger _subLogger;
59
60   private String JavaDoc _mbeanName;
61   private boolean _isInit;
62
63   public LogConfig()
64   {
65     setTimestamp("[%Y/%m/%d %H:%M:%S.%s] ");
66   }
67
68   /**
69    * Sets the name of the logger to configure.
70    *
71    * @deprecated Use setName()
72    */

73   public void setId(String JavaDoc name)
74   {
75     // backward compat
76
if (name.equals("/"))
77       name = "";
78     
79     setName(name);
80   }
81
82   /**
83    * Sets the name of the logger to configure.
84    */

85   public void setName(String JavaDoc name)
86   {
87     getSubLogger().setName(name);
88   }
89
90   /**
91    * Returns the name of the logger to configure.
92    */

93   public String JavaDoc getName()
94   {
95     return getSubLogger().getName();
96   }
97
98   /**
99    * Sets the mbean-name of the logger to configure.
100    */

101   public void setMBeanName(String JavaDoc name)
102   {
103     _mbeanName = name;
104   }
105
106   /**
107    * Sets the use-parent-handlers
108    */

109   public void setUseParentHandlers(boolean useParentHandlers)
110     throws ConfigException
111   {
112     getSubLogger().setUseParentHandlers(useParentHandlers);
113   }
114
115   /**
116    * Sets the output level.
117    */

118   public void setLevel(String JavaDoc level)
119     throws ConfigException
120   {
121     getSubLogger().setLevel(level);
122
123     if (_isInit && _handlers != null) {
124       for (Handler JavaDoc handler : _handlers) {
125     handler.setLevel(getSubLogger().getLevel());
126       }
127     }
128
129
130   }
131
132   /**
133    * Sets the output level.
134    */

135   public String JavaDoc getLevel()
136   {
137     Level JavaDoc level = getSubLogger().getLevel();
138
139     if (level != null)
140       return level.getName();
141     else
142       return Level.INFO.getName();
143   }
144
145   /**
146    * Sets the sublogger.
147    */

148   private SubLogger getSubLogger()
149   {
150     if (_subLogger == null) {
151       _subLogger = new SubLogger();
152       _subLoggers.add(_subLogger);
153     }
154
155     return _subLogger;
156   }
157
158   /**
159    * Sets the timestamp.
160    */

161   public void setTimestamp(String JavaDoc timestamp)
162   {
163     _timestamp = timestamp;
164   }
165
166   /**
167    * A format string uses EL expressions and the EL variable `log', which is an
168    * instance of LogRecord.
169    */

170   public void setFormat(RawString format)
171   {
172     if (_formatter == null) {
173       _formatter = new ELFormatter();
174     }
175
176     if (_formatter instanceof ELFormatter)
177       ((ELFormatter)_formatter).setFormat(format);
178   }
179
180   /**
181    * A format string uses EL expressions and the EL variable `log', which is an
182    * instance of LogRecord.
183    */

184   public String JavaDoc getFormat()
185   {
186     if (_formatter != null && _formatter instanceof ELFormatter)
187       return ((ELFormatter)_formatter).getFormat();
188     else
189       return null;
190   }
191
192   /**
193    * Sets the formatter.
194    */

195   public void setFormatter(Formatter JavaDoc formatter)
196   {
197     _formatter = formatter;
198   }
199
200   /**
201    * Adds a handler
202    */

203   public void addHandler(Handler JavaDoc handler)
204   {
205     if (handler == null)
206       throw new NullPointerException JavaDoc();
207     
208     if (_handlers == null)
209       _handlers = new ArrayList JavaDoc<Handler JavaDoc>();
210
211     _handlers.add(handler);
212   }
213
214   /**
215    * Returns the tag name.
216    */

217   public String JavaDoc getTagName()
218   {
219     return "log";
220   }
221
222   /**
223    * Adds a logger.
224    */

225   public SubLogger createLogger()
226   {
227     SubLogger subLogger = new SubLogger();
228
229     subLogger.setLevel("info");
230     _subLoggers.add(subLogger);
231     
232     return subLogger;
233   }
234
235   /**
236    * Initialize the log.
237    */

238   @PostConstruct
239   public void init()
240     throws ConfigException, java.io.IOException JavaDoc
241   {
242     _isInit = true;
243
244     if (_handlers == null)
245       super.init();
246     
247     if (_subLoggers.size() == 0)
248       getSubLogger();
249
250     if (_subLogger != null)
251       _subLogger.init();
252     
253     if (_formatter instanceof ELFormatter) {
254       ((ELFormatter)_formatter).init();
255     }
256     /*
257     else if (_formatter == null) {
258       _formatter = new MessageFormatter();
259     }
260     */

261
262     WriteStream os = null;
263     
264     if (_handlers == null) {
265       os = getRotateStream().getStream();
266
267       if (_timestamp != null) {
268     TimestampFilter filter = new TimestampFilter();
269     filter.setTimestamp(_timestamp);
270     filter.setStream(os);
271     os = new WriteStream(filter);
272       }
273
274       String JavaDoc encoding = System.getProperty("file.encoding");
275       
276       if (encoding != null)
277     os.setEncoding(encoding);
278       
279       os.setDisableClose(true);
280     }
281
282     boolean hasCloseListener = false;
283     for (int i = 0; i < _subLoggers.size(); i++) {
284       SubLogger subLogger = _subLoggers.get(i);
285       Logger JavaDoc logger = subLogger.getLogger();
286
287       Level JavaDoc subLevel = subLogger.getLevel();
288       Level JavaDoc level = logger.getLevel();
289
290       if (subLevel != null
291       && (level == null && subLevel.intValue() < Level.INFO.intValue()
292           || level != null && subLevel.intValue() < level.intValue())) {
293     logger.setLevel(subLogger.getLevel());
294       }
295
296       if (_handlers == null) {
297     StreamHandler handler = new StreamHandler(os);
298     handler.setFormatter(_formatter);
299
300     _handlers = new ArrayList JavaDoc<Handler JavaDoc>();
301     _handlers.add(handler);
302       }
303
304       for (int j = 0; j < _handlers.size(); j++) {
305     SubHandler subHandler = new SubHandler(_handlers.get(j));
306
307     if (subLogger.getLevel() != null)
308       subHandler.setLevel(subLogger.getLevel());
309
310     if (! (logger instanceof EnvironmentLogger)) {
311       CloseListener listener = new CloseListener(subHandler);
312       
313       Environment.addClassLoaderListener(listener);
314     }
315     
316     logger.addHandler(subHandler);
317       }
318     }
319
320     if (_mbeanName != null) {
321       try {
322     Jmx.register(this, Jmx.getObjectName(_mbeanName));
323       } catch (Throwable JavaDoc e) {
324       }
325     }
326   }
327
328   static Level JavaDoc toLevel(String JavaDoc level)
329     throws ConfigException
330   {
331     if (level.equals("off"))
332       return Level.OFF;
333     else if (level.equals("severe"))
334       return Level.SEVERE;
335     else if (level.equals("warning"))
336       return Level.WARNING;
337     else if (level.equals("info"))
338       return Level.INFO;
339     else if (level.equals("config"))
340       return Level.CONFIG;
341     else if (level.equals("fine"))
342       return Level.FINE;
343     else if (level.equals("finer"))
344       return Level.FINER;
345     else if (level.equals("finest"))
346       return Level.FINEST;
347     else if (level.equals("all"))
348       return Level.ALL;
349     else
350       throw new ConfigException(L.l("`{0}' is an unknown log level. Log levels are:\noff - disable logging\nsevere - severe errors only\nwarning - warnings\ninfo - information\nconfig - configuration\nfine - fine debugging\nfiner - finer debugging\nfinest - finest debugging\nall - all debugging", level));
351   }
352
353   public static class SubLogger {
354     private Logger JavaDoc _logger;
355     private String JavaDoc _name = "";
356     private Level JavaDoc _level;
357     private boolean _useParentHandlers = true;
358
359     // for mbean management
360
private Handler JavaDoc _handler;
361
362     /**
363      * Sets the name of the logger to configure.
364      */

365     public void setId(String JavaDoc name)
366     {
367       // backward compat
368
if (name.equals("/"))
369     name = "";
370     
371       setName(name);
372     }
373
374     /**
375      * Sets the name of the logger to configure.
376      */

377     public void setName(String JavaDoc name)
378     {
379       _name = name;
380     }
381
382     /**
383      * Gets the name of the logger to configure.
384      */

385     public String JavaDoc getName()
386     {
387       return _name;
388     }
389
390     /**
391      * Sets the use-parent-handlers
392      */

393     public void setUseParentHandlers(boolean useParentHandlers)
394       throws ConfigException
395     {
396       _logger = Logger.getLogger(_name);
397
398       _logger.setUseParentHandlers(useParentHandlers);
399
400       _useParentHandlers = useParentHandlers;
401     }
402
403     /**
404      * Gets the logger.
405      */

406     public Logger JavaDoc getLogger()
407     {
408       return Logger.getLogger(_name);
409     }
410
411     /**
412      * Gets the output level.
413      */

414     public Level JavaDoc getLevel()
415     {
416       return _level;
417     }
418
419     /**
420      * Sets the output level.
421      */

422     public void setLevel(String JavaDoc level)
423       throws ConfigException
424     {
425       if (level.equals("off"))
426     _level = Level.OFF;
427       else if (level.equals("severe"))
428     _level = Level.SEVERE;
429       else if (level.equals("warning"))
430     _level = Level.WARNING;
431       else if (level.equals("info"))
432     _level = Level.INFO;
433       else if (level.equals("config"))
434     _level = Level.CONFIG;
435       else if (level.equals("fine"))
436     _level = Level.FINE;
437       else if (level.equals("finer"))
438     _level = Level.FINER;
439       else if (level.equals("finest"))
440     _level = Level.FINEST;
441       else if (level.equals("all"))
442     _level = Level.ALL;
443       else
444     throw new ConfigException(L.l("`{0}' is an unknown log level. Log levels are:\noff - disable logging\nsevere - severe errors only\nwarning - warnings\ninfo - information\nconfig - configuration\nfine - fine debugging\nfiner - finer debugging\nfinest - finest debugging\nall - all debugging", level));
445     }
446
447     /**
448      * Sets the handler.
449      */

450     public void setHandler(Handler JavaDoc handler)
451     {
452       _handler = handler;
453     }
454
455     /**
456      * Gets the handler.
457      */

458     public Handler JavaDoc getHandler()
459     {
460       return _handler;
461     }
462
463     @PostConstruct
464     public void init()
465       throws ConfigException
466     {
467       if (_name == null)
468     throw new ConfigException(L.l("`name' is a required attribute.. Each logger must configure the log name, e.g. com.caucho.server.webapp."));
469     }
470   }
471 }
472
Popular Tags