View Javadoc

1   /**
2    *
3    * Copyright 2003-2004 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  
18  package org.apache.geronimo.kernel.log;
19  
20  import java.lang.reflect.Method;
21  import java.util.Map;
22  import java.util.HashMap;
23  
24  import org.apache.commons.logging.LogFactory;
25  
26  /**
27   * @version $Rev: 438786 $ $Date: 2006-08-30 21:35:25 -0700 (Wed, 30 Aug 2006) $
28   */
29  public class GeronimoLogging {
30  
31      //this needs to go before the instance constants or you get an NPE in the constructor.
32      private static final Map levels = new HashMap();
33  
34      public static final GeronimoLogging TRACE = new GeronimoLogging("TRACE");
35      public static final GeronimoLogging DEBUG = new GeronimoLogging("DEBUG");
36      public static final GeronimoLogging INFO = new GeronimoLogging("INFO");
37      public static final GeronimoLogging WARN = new GeronimoLogging("WARN");
38      public static final GeronimoLogging ERROR = new GeronimoLogging("ERROR");
39      public static final GeronimoLogging FATAL = new GeronimoLogging("FATAL");
40  
41      private static boolean initialized = false;
42      private static GeronimoLogging consoleLogLevel = ERROR;
43      private static GeronimoLogging defaultLevel;
44  
45      /**
46       * Initializes the logging system used by Geronimo.  This MUST be called in
47       * in the main class used to start the geronimo server.  This method forces
48       * commons logging to use GeronimoLogFactory, starts the initial commons-logging
49       * logging system, and forces mx4j to use commons logging.
50       */
51      public static void initialize(GeronimoLogging level) {
52          if (!initialized) {
53              defaultLevel = level;
54              consoleLogLevel = level;
55  
56              // force the log factory to initialize
57              LogFactory.getLog(GeronimoLogging.class);
58  
59              // force mx4j to use commons logging
60              // Use reflection so mx4j is not required (this is important in JDK 1.5)
61              // mx4j.log.Log.redirectTo(new mx4j.log.CommonsLogger());
62              try {
63                  Class clazz = Class.forName("mx4j.log.Log");
64                  Class paramClazz = Class.forName("mx4j.log.Logger");
65                  Method method = clazz.getDeclaredMethod("redirectTo", new Class[] {paramClazz});
66                  paramClazz = Class.forName("mx4j.log.CommonsLogger");
67                  method.invoke(null, new Object[] {paramClazz.newInstance()});
68              } catch (ClassNotFoundException e) {
69                  // MX4J is not present.
70              } catch (Exception e) {
71                  throw (AssertionError) new AssertionError("Cannot force MX4J to use commons logging.").initCause(e);
72              }
73  
74              initialized = true;
75          }
76      }
77  
78      public static void setDefaultLogLevel(GeronimoLogging level) {
79          defaultLevel = level;
80      }
81  
82      public static GeronimoLogging getDefaultLevel() {
83          return defaultLevel;
84      }
85  
86      public static GeronimoLogging getConsoleLogLevel() {
87          return consoleLogLevel;
88      }
89  
90      public static void setConsoleLogLevel(GeronimoLogging consoleLogLevel) {
91          GeronimoLogging.consoleLogLevel = consoleLogLevel;
92      }
93  
94      public static GeronimoLogging getGeronimoLogging(String level) {
95          return (GeronimoLogging) levels.get(level);
96      }
97  
98      private final String level;
99  
100     private GeronimoLogging(String level) {
101         this.level = level;
102         levels.put(level, this);
103     }
104 
105     public String toString() {
106         return level;
107     }
108 
109     /**
110      * Check if the Geronimo bootstrap logging initialization is enabled.
111      *
112      * <p>Checks the system property <tt>geronimo.bootstrap.logging.enabled</tt>
113      * if not set, or set to "true" then bootstrap logging initialization is enabled.
114      *
115      * @return  True of bootstrap logging initialization is enabled.
116      */
117     public static boolean isBootstrapLoggingInitializationEnabled() {
118         String value = System.getProperty("geronimo.bootstrap.logging.enabled");
119         if (value == null) {
120             return true;
121         }
122         else {
123             return Boolean.valueOf(value).booleanValue();
124         }
125     }
126 }