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.util.logging.ConsoleHandler;
021    import java.util.logging.Handler;
022    import java.util.logging.Level;
023    import java.util.logging.Logger;
024    
025    import org.apache.commons.logging.Log;
026    
027    /**
028     * Bootstrap log implementation for use with JDK 1.4 logging.
029     *
030     * @version $Rev: 438786 $ $Date: 2006-08-30 21:35:25 -0700 (Wed, 30 Aug 2006) $
031     */
032    public class BootstrapJdk14Log implements Log {
033        static {
034            if (GeronimoLogging.isBootstrapLoggingInitializationEnabled()) {
035                Logger root = Logger.getLogger("");
036    
037                GeronimoLogging geronimoLevel = GeronimoLogging.getDefaultLevel();
038                Level javaLevel;
039                if (geronimoLevel == GeronimoLogging.TRACE) {
040                    javaLevel = Level.FINEST;
041                } else if (geronimoLevel == GeronimoLogging.DEBUG) {
042                    javaLevel = Level.FINE;
043                } else if (geronimoLevel == GeronimoLogging.INFO) {
044                    javaLevel = Level.INFO;
045                } else if (geronimoLevel == GeronimoLogging.WARN) {
046                    javaLevel = Level.WARNING;
047                } else {
048                    javaLevel = Level.SEVERE;
049                }
050    
051                // set the root level
052                root.setLevel(javaLevel);
053    
054                // set the console handler level (if present)
055                Handler[] handlers = root.getHandlers();
056                for (int index = 0; index < handlers.length; index++) {
057                    if (handlers[index] instanceof ConsoleHandler) {
058                        handlers[index].setLevel(javaLevel);
059                    }
060                }
061            }
062        }
063    
064        private Logger logger = null;
065    
066        public BootstrapJdk14Log(String name) {
067            logger = Logger.getLogger(name);
068        }
069    
070        private void log(Level level, String messge, Throwable throwable) {
071            if (logger.isLoggable(level)) {
072                // need to determine if caller class name and method
073                StackTraceElement locations[] = new Throwable().getStackTrace();
074    
075                // Caller will be the forth element
076                String cname = "unknown";
077                String method = "unknown";
078                if (locations != null && locations.length > 3) {
079                    StackTraceElement caller = locations[3];
080                    cname = caller.getClassName();
081                    method = caller.getMethodName();
082                }
083                if (throwable == null) {
084                    logger.logp(level, cname, method, messge);
085                } else {
086                    logger.logp(level, cname, method, messge, throwable);
087                }
088            }
089        }
090    
091        public void debug(Object message) {
092            log(Level.FINE, String.valueOf(message), null);
093        }
094    
095        public void debug(Object message, Throwable exception) {
096            log(Level.FINE, String.valueOf(message), exception);
097        }
098    
099        public void error(Object message) {
100            log(Level.SEVERE, String.valueOf(message), null);
101        }
102    
103        public void error(Object message, Throwable exception) {
104            log(Level.SEVERE, String.valueOf(message), exception);
105        }
106    
107        public void fatal(Object message) {
108            log(Level.SEVERE, String.valueOf(message), null);
109        }
110    
111        public void fatal(Object message, Throwable exception) {
112            log(Level.SEVERE, String.valueOf(message), exception);
113        }
114    
115        public Logger getLogger() {
116            return this.logger;
117        }
118    
119        public void info(Object message) {
120            log(Level.INFO, String.valueOf(message), null);
121        }
122    
123        public void info(Object message, Throwable exception) {
124            log(Level.INFO, String.valueOf(message), exception);
125        }
126    
127        public boolean isDebugEnabled() {
128            return (logger.isLoggable(Level.FINE));
129        }
130    
131        public boolean isErrorEnabled() {
132            return (logger.isLoggable(Level.SEVERE));
133        }
134    
135        public boolean isFatalEnabled() {
136            return (logger.isLoggable(Level.SEVERE));
137        }
138    
139        public boolean isInfoEnabled() {
140            return (logger.isLoggable(Level.INFO));
141        }
142    
143        public boolean isTraceEnabled() {
144            return (logger.isLoggable(Level.FINEST));
145        }
146    
147        public boolean isWarnEnabled() {
148            return (logger.isLoggable(Level.WARNING));
149        }
150    
151        public void trace(Object message) {
152            log(Level.FINEST, String.valueOf(message), null);
153        }
154    
155        public void trace(Object message, Throwable exception) {
156            log(Level.FINEST, String.valueOf(message), exception);
157        }
158    
159        public void warn(Object message) {
160            log(Level.WARNING, String.valueOf(message), null);
161        }
162    
163        public void warn(Object message, Throwable exception) {
164            log(Level.WARNING, String.valueOf(message), exception);
165        }
166    }