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 package org.apache.juli.logging;
18
19 import java.util.logging.ConsoleHandler;
20 import java.util.logging.Formatter;
21 import java.util.logging.Handler;
22 import java.util.logging.Level;
23 import java.util.logging.Logger;
24
25 /**
26  * Hard-coded java.util.logging commons-logging implementation.
27  */

28 class DirectJDKLog implements Log {
29     // no reason to hide this - but good reasons to not hide
30     public final Logger logger;
31
32     // Alternate config reader and console format
33     private static final String SIMPLE_FMT="java.util.logging.SimpleFormatter";
34     private static final String FORMATTER="org.apache.juli.formatter";
35
36     static {
37         if (System.getProperty("java.util.logging.config.class") == null  &&
38                 System.getProperty("java.util.logging.config.file") == null) {
39             // default configuration - it sucks. Let's override at least the
40             // formatter for the console
41             try {
42                 Formatter fmt= (Formatter) Class.forName(System.getProperty(
43                         FORMATTER, SIMPLE_FMT)).getConstructor().newInstance();
44                 // it is also possible that the user modified jre/lib/logging.properties -
45                 // but that's really stupid in most cases
46                 Logger root=Logger.getLogger("");
47                 for (Handler handler : root.getHandlers()) {
48                     // I only care about console - that's what's used in default config anyway
49                     if (handler instanceof  ConsoleHandler) {
50                         handler.setFormatter(fmt);
51                     }
52                 }
53             } catch (Throwable t) {
54                 // maybe it wasn't included - the ugly default will be used.
55             }
56
57         }
58     }
59
60     public DirectJDKLog(String name ) {
61         logger=Logger.getLogger(name);
62     }
63
64     @Override
65     public final boolean isErrorEnabled() {
66         return logger.isLoggable(Level.SEVERE);
67     }
68
69     @Override
70     public final boolean isWarnEnabled() {
71         return logger.isLoggable(Level.WARNING);
72     }
73
74     @Override
75     public final boolean isInfoEnabled() {
76         return logger.isLoggable(Level.INFO);
77     }
78
79     @Override
80     public final boolean isDebugEnabled() {
81         return logger.isLoggable(Level.FINE);
82     }
83
84     @Override
85     public final boolean isFatalEnabled() {
86         return logger.isLoggable(Level.SEVERE);
87     }
88
89     @Override
90     public final boolean isTraceEnabled() {
91         return logger.isLoggable(Level.FINER);
92     }
93
94     @Override
95     public final void debug(Object message) {
96         log(Level.FINE, String.valueOf(message), null);
97     }
98
99     @Override
100     public final void debug(Object message, Throwable t) {
101         log(Level.FINE, String.valueOf(message), t);
102     }
103
104     @Override
105     public final void trace(Object message) {
106         log(Level.FINER, String.valueOf(message), null);
107     }
108
109     @Override
110     public final void trace(Object message, Throwable t) {
111         log(Level.FINER, String.valueOf(message), t);
112     }
113
114     @Override
115     public final void info(Object message) {
116         log(Level.INFO, String.valueOf(message), null);
117     }
118
119     @Override
120     public final void info(Object message, Throwable t) {
121         log(Level.INFO, String.valueOf(message), t);
122     }
123
124     @Override
125     public final void warn(Object message) {
126         log(Level.WARNING, String.valueOf(message), null);
127     }
128
129     @Override
130     public final void warn(Object message, Throwable t) {
131         log(Level.WARNING, String.valueOf(message), t);
132     }
133
134     @Override
135     public final void error(Object message) {
136         log(Level.SEVERE, String.valueOf(message), null);
137     }
138
139     @Override
140     public final void error(Object message, Throwable t) {
141         log(Level.SEVERE, String.valueOf(message), t);
142     }
143
144     @Override
145     public final void fatal(Object message) {
146         log(Level.SEVERE, String.valueOf(message), null);
147     }
148
149     @Override
150     public final void fatal(Object message, Throwable t) {
151         log(Level.SEVERE, String.valueOf(message), t);
152     }
153
154     // from commons logging. This would be my number one reason why java.util.logging
155     // is bad - design by committee can be really bad ! The impact on performance of
156     // using java.util.logging - and the ugliness if you need to wrap it - is far
157     // worse than the unfriendly and uncommon default format for logs.
158
159     private void log(Level level, String msg, Throwable ex) {
160         if (logger.isLoggable(level)) {
161             // Hack (?) to get the stack trace.
162             Throwable dummyException=new Throwable();
163             StackTraceElement locations[]=dummyException.getStackTrace();
164             // Caller will be the third element
165             String cname = "unknown";
166             String method = "unknown";
167             if (locations != null && locations.length >2) {
168                 StackTraceElement caller = locations[2];
169                 cname = caller.getClassName();
170                 method = caller.getMethodName();
171             }
172             if (ex==null) {
173                 logger.logp(level, cname, method, msg);
174             } else {
175                 logger.logp(level, cname, method, msg, ex);
176             }
177         }
178     }
179
180     static Log getInstance(String name) {
181         return new DirectJDKLog( name );
182     }
183 }
184
185
186