KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > kernel > log > GeronimoLogging


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.HashMap JavaDoc;
23
24 import org.apache.commons.logging.LogFactory;
25
26 /**
27  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 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 JavaDoc levels = new HashMap JavaDoc();
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 JavaDoc clazz = Class.forName("mx4j.log.Log");
64                 Class JavaDoc paramClazz = Class.forName("mx4j.log.Logger");
65                 Method JavaDoc method = clazz.getDeclaredMethod("redirectTo", new Class JavaDoc[] {paramClazz});
66                 paramClazz = Class.forName("mx4j.log.CommonsLogger");
67                 method.invoke(null, new Object JavaDoc[] {paramClazz.newInstance()});
68             } catch (ClassNotFoundException JavaDoc e) {
69                 // MX4J is not present.
70
} catch (Exception JavaDoc e) {
71                 throw (AssertionError JavaDoc) new AssertionError JavaDoc("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 JavaDoc level) {
95         return (GeronimoLogging) levels.get(level);
96     }
97
98     private final String JavaDoc level;
99
100     private GeronimoLogging(String JavaDoc level) {
101         this.level = level;
102         levels.put(level, this);
103     }
104
105     public String JavaDoc 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 JavaDoc 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 }
127
Popular Tags