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.util.logging.ConsoleHandler;
21  import java.util.logging.Handler;
22  import java.util.logging.Level;
23  import java.util.logging.Logger;
24  
25  import org.apache.commons.logging.Log;
26  
27  /**
28   * Bootstrap log implementation for use with JDK 1.4 logging.
29   *
30   * @version $Rev: 438786 $ $Date: 2006-08-30 21:35:25 -0700 (Wed, 30 Aug 2006) $
31   */
32  public class BootstrapJdk14Log implements Log {
33      static {
34          if (GeronimoLogging.isBootstrapLoggingInitializationEnabled()) {
35              Logger root = Logger.getLogger("");
36  
37              GeronimoLogging geronimoLevel = GeronimoLogging.getDefaultLevel();
38              Level javaLevel;
39              if (geronimoLevel == GeronimoLogging.TRACE) {
40                  javaLevel = Level.FINEST;
41              } else if (geronimoLevel == GeronimoLogging.DEBUG) {
42                  javaLevel = Level.FINE;
43              } else if (geronimoLevel == GeronimoLogging.INFO) {
44                  javaLevel = Level.INFO;
45              } else if (geronimoLevel == GeronimoLogging.WARN) {
46                  javaLevel = Level.WARNING;
47              } else {
48                  javaLevel = Level.SEVERE;
49              }
50  
51              // set the root level
52              root.setLevel(javaLevel);
53  
54              // set the console handler level (if present)
55              Handler[] handlers = root.getHandlers();
56              for (int index = 0; index < handlers.length; index++) {
57                  if (handlers[index] instanceof ConsoleHandler) {
58                      handlers[index].setLevel(javaLevel);
59                  }
60              }
61          }
62      }
63  
64      private Logger logger = null;
65  
66      public BootstrapJdk14Log(String name) {
67          logger = Logger.getLogger(name);
68      }
69  
70      private void log(Level level, String messge, Throwable throwable) {
71          if (logger.isLoggable(level)) {
72              // need to determine if caller class name and method
73              StackTraceElement locations[] = new Throwable().getStackTrace();
74  
75              // Caller will be the forth element
76              String cname = "unknown";
77              String method = "unknown";
78              if (locations != null && locations.length > 3) {
79                  StackTraceElement caller = locations[3];
80                  cname = caller.getClassName();
81                  method = caller.getMethodName();
82              }
83              if (throwable == null) {
84                  logger.logp(level, cname, method, messge);
85              } else {
86                  logger.logp(level, cname, method, messge, throwable);
87              }
88          }
89      }
90  
91      public void debug(Object message) {
92          log(Level.FINE, String.valueOf(message), null);
93      }
94  
95      public void debug(Object message, Throwable exception) {
96          log(Level.FINE, String.valueOf(message), exception);
97      }
98  
99      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 }