1
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
28 class DirectJDKLog implements Log {
29
30 public final Logger logger;
31
32
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
40
41 try {
42 Formatter fmt= (Formatter) Class.forName(System.getProperty(
43 FORMATTER, SIMPLE_FMT)).getConstructor().newInstance();
44
45
46 Logger root=Logger.getLogger("");
47 for (Handler handler : root.getHandlers()) {
48
49 if (handler instanceof ConsoleHandler) {
50 handler.setFormatter(fmt);
51 }
52 }
53 } catch (Throwable t) {
54
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
155
156
157
158
159 private void log(Level level, String msg, Throwable ex) {
160 if (logger.isLoggable(level)) {
161
162 Throwable dummyException=new Throwable();
163 StackTraceElement locations[]=dummyException.getStackTrace();
164
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