KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.logging.ConsoleHandler JavaDoc;
21 import java.util.logging.Handler JavaDoc;
22 import java.util.logging.Level JavaDoc;
23 import java.util.logging.Logger JavaDoc;
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: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
31  */

32 public class BootstrapJdk14Log implements Log {
33     static {
34         if (GeronimoLogging.isBootstrapLoggingInitializationEnabled()) {
35             Logger JavaDoc root = Logger.getLogger("");
36
37             GeronimoLogging geronimoLevel = GeronimoLogging.getDefaultLevel();
38             Level JavaDoc 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 JavaDoc[] handlers = root.getHandlers();
56             for (int index = 0; index < handlers.length; index++) {
57                 if (handlers[index] instanceof ConsoleHandler JavaDoc) {
58                     handlers[index].setLevel(javaLevel);
59                 }
60             }
61         }
62     }
63
64     private Logger JavaDoc logger = null;
65
66     public BootstrapJdk14Log(String JavaDoc name) {
67         logger = Logger.getLogger(name);
68     }
69
70     private void log(Level JavaDoc level, String JavaDoc messge, Throwable JavaDoc throwable) {
71         if (logger.isLoggable(level)) {
72             // need to determine if caller class name and method
73
StackTraceElement JavaDoc locations[] = new Throwable JavaDoc().getStackTrace();
74
75             // Caller will be the forth element
76
String JavaDoc cname = "unknown";
77             String JavaDoc method = "unknown";
78             if (locations != null && locations.length > 3) {
79                 StackTraceElement JavaDoc 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 JavaDoc message) {
92         log(Level.FINE, String.valueOf(message), null);
93     }
94
95     public void debug(Object JavaDoc message, Throwable JavaDoc exception) {
96         log(Level.FINE, String.valueOf(message), exception);
97     }
98
99     public void error(Object JavaDoc message) {
100         log(Level.SEVERE, String.valueOf(message), null);
101     }
102
103     public void error(Object JavaDoc message, Throwable JavaDoc exception) {
104         log(Level.SEVERE, String.valueOf(message), exception);
105     }
106
107     public void fatal(Object JavaDoc message) {
108         log(Level.SEVERE, String.valueOf(message), null);
109     }
110
111     public void fatal(Object JavaDoc message, Throwable JavaDoc exception) {
112         log(Level.SEVERE, String.valueOf(message), exception);
113     }
114
115     public Logger JavaDoc getLogger() {
116         return this.logger;
117     }
118
119     public void info(Object JavaDoc message) {
120         log(Level.INFO, String.valueOf(message), null);
121     }
122
123     public void info(Object JavaDoc message, Throwable JavaDoc 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 JavaDoc message) {
152         log(Level.FINEST, String.valueOf(message), null);
153     }
154
155     public void trace(Object JavaDoc message, Throwable JavaDoc exception) {
156         log(Level.FINEST, String.valueOf(message), exception);
157     }
158
159     public void warn(Object JavaDoc message) {
160         log(Level.WARNING, String.valueOf(message), null);
161     }
162
163     public void warn(Object JavaDoc message, Throwable JavaDoc exception) {
164         log(Level.WARNING, String.valueOf(message), exception);
165     }
166 }
167
Popular Tags