001    /**
002     *
003     * Copyright 2003-2004 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package org.apache.geronimo.kernel.log;
019    
020    import java.lang.reflect.Method;
021    import java.util.Map;
022    import java.util.HashMap;
023    
024    import org.apache.commons.logging.LogFactory;
025    
026    /**
027     * @version $Rev: 438786 $ $Date: 2006-08-30 21:35:25 -0700 (Wed, 30 Aug 2006) $
028     */
029    public class GeronimoLogging {
030    
031        //this needs to go before the instance constants or you get an NPE in the constructor.
032        private static final Map levels = new HashMap();
033    
034        public static final GeronimoLogging TRACE = new GeronimoLogging("TRACE");
035        public static final GeronimoLogging DEBUG = new GeronimoLogging("DEBUG");
036        public static final GeronimoLogging INFO = new GeronimoLogging("INFO");
037        public static final GeronimoLogging WARN = new GeronimoLogging("WARN");
038        public static final GeronimoLogging ERROR = new GeronimoLogging("ERROR");
039        public static final GeronimoLogging FATAL = new GeronimoLogging("FATAL");
040    
041        private static boolean initialized = false;
042        private static GeronimoLogging consoleLogLevel = ERROR;
043        private static GeronimoLogging defaultLevel;
044    
045        /**
046         * Initializes the logging system used by Geronimo.  This MUST be called in
047         * in the main class used to start the geronimo server.  This method forces
048         * commons logging to use GeronimoLogFactory, starts the initial commons-logging
049         * logging system, and forces mx4j to use commons logging.
050         */
051        public static void initialize(GeronimoLogging level) {
052            if (!initialized) {
053                defaultLevel = level;
054                consoleLogLevel = level;
055    
056                // force the log factory to initialize
057                LogFactory.getLog(GeronimoLogging.class);
058    
059                // force mx4j to use commons logging
060                // Use reflection so mx4j is not required (this is important in JDK 1.5)
061                // mx4j.log.Log.redirectTo(new mx4j.log.CommonsLogger());
062                try {
063                    Class clazz = Class.forName("mx4j.log.Log");
064                    Class paramClazz = Class.forName("mx4j.log.Logger");
065                    Method method = clazz.getDeclaredMethod("redirectTo", new Class[] {paramClazz});
066                    paramClazz = Class.forName("mx4j.log.CommonsLogger");
067                    method.invoke(null, new Object[] {paramClazz.newInstance()});
068                } catch (ClassNotFoundException e) {
069                    // MX4J is not present.
070                } catch (Exception e) {
071                    throw (AssertionError) new AssertionError("Cannot force MX4J to use commons logging.").initCause(e);
072                }
073    
074                initialized = true;
075            }
076        }
077    
078        public static void setDefaultLogLevel(GeronimoLogging level) {
079            defaultLevel = level;
080        }
081    
082        public static GeronimoLogging getDefaultLevel() {
083            return defaultLevel;
084        }
085    
086        public static GeronimoLogging getConsoleLogLevel() {
087            return consoleLogLevel;
088        }
089    
090        public static void setConsoleLogLevel(GeronimoLogging consoleLogLevel) {
091            GeronimoLogging.consoleLogLevel = consoleLogLevel;
092        }
093    
094        public static GeronimoLogging getGeronimoLogging(String level) {
095            return (GeronimoLogging) levels.get(level);
096        }
097    
098        private final String level;
099    
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    }