View Javadoc

1   /*
2    * Jour - java profiler and monitoring library
3    *
4    * Copyright (C) 2004 Jour team
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Library General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Library General Public License for more details.
15   *
16   * You should have received a copy of the GNU Library General Public
17   * License along with this library; if not, write to the
18   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19   * Boston, MA  02111-1307, USA.
20   */
21  package net.sf.jour.rt.agent;
22  
23  import net.sf.jour.rt.RtProperties;
24  import net.sf.jour.util.PropertiesBase;
25  import net.sf.jour.util.PropertiesReloadListener;
26  
27  /***
28   * TODO Add docs
29   * 
30   * Created on 02.12.2004
31   *
32   * Contributing Author(s):
33   *
34   *   Misha Lifschitz <mishalifschitz at users.sourceforge.net> (Inital implementation)
35   *   Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Inital implementation)
36   * 
37   * @author michaellif
38   * @version $Revision: 1.12 $ ($Author: vlads $) $Date: 2004/12/12 02:00:01 $
39   */
40  public class Elog implements PropertiesReloadListener {
41      /***   DOCUMENT ME!   */
42      private static EventLogger eventLogger;
43      
44  	public final static String DEFAULT_LOGGER = "EventQueueLogger";
45  	
46  	//	 Defaults to false, safe for production
47  	public static boolean ON;
48  	
49  	private static Elog instance = new Elog();
50  	
51      private Elog() {
52          try {
53              PropertiesBase properties = RtProperties.getInstance();
54              doOnPropertiesReload(properties);
55              properties.addListener(this);
56          } catch (NoClassDefFoundError e) {
57              SafeLog.error(Elog.class, "Intialization error", e);
58              eventLogger = null;
59              ON = false;
60          } catch (RuntimeException e) {
61              SafeLog.error(Elog.class, "Intialization error", e);
62              eventLogger = null;
63              ON = false;
64          }
65      }
66      
67      public void doOnPropertiesReload(PropertiesBase properties) {
68          try {
69  
70              String logClass = properties.getProperty("elog.class", DEFAULT_LOGGER);
71              EventLogger oldEventLogger = eventLogger;
72              if (logClass.equalsIgnoreCase("AsyncEventLogger")) {
73                  eventLogger = new AsyncEventLogger();
74              } else if (logClass.equalsIgnoreCase("Log4jEventLogger")) {
75                  eventLogger = new Log4jEventLogger();
76              } else {
77                  eventLogger = EventQueueLogger.getInstance();
78              }
79              if ((oldEventLogger != null) && (oldEventLogger != eventLogger)) {
80                  oldEventLogger.close();
81              }
82              SafeLog.info(Elog.class, "elog.class " + logClass); 
83  
84              // Defaults to false, safe for production
85              ON = properties.getBoolProperty("elog.ON",  false);
86              SafeLog.info(Elog.class, "Elog.ON=" + Elog.ON);
87              
88              Monitor.getInstance();
89  
90          } catch (NoClassDefFoundError e) {
91              SafeLog.error(Elog.class, "Intialization error", e);
92              eventLogger = null;
93              ON = false;
94          } catch (RuntimeException e) {
95              SafeLog.error(Elog.class, "Intialization error", e);
96              eventLogger = null;
97              ON = false;
98          }
99      }
100     
101     /***
102      * DOCUMENT ME!
103      * 
104      * @param event
105      *            DOCUMENT ME!
106      */
107     public static void logEvent(Event event) {
108         if ((ON) && (eventLogger != null)) {
109         	try {
110                 eventLogger.logEvent(event);
111             } catch (Throwable e) {
112                 SafeLog.error(Elog.class, "EventLogger error", e);
113                 eventLogger = null;
114             }
115         }
116     }
117 }