1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
package net.sourceforge.acelogger.execution; |
16 | |
|
17 | |
import java.io.PrintWriter; |
18 | |
import java.io.StringWriter; |
19 | |
import java.text.MessageFormat; |
20 | |
import java.util.MissingResourceException; |
21 | |
import java.util.ResourceBundle; |
22 | |
|
23 | |
import net.sourceforge.acelogger.LogEvent; |
24 | |
import net.sourceforge.acelogger.Logger; |
25 | |
import net.sourceforge.acelogger.constants.FormatterConstants; |
26 | |
import net.sourceforge.acelogger.formatter.Formatter; |
27 | |
import net.sourceforge.acelogger.level.LogLevel; |
28 | |
import net.sourceforge.acelogger.location.LogEventLocation; |
29 | |
import net.sourceforge.acelogger.location.gatherer.LocationGatherer; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public class InternalLogger implements Logger { |
42 | |
|
43 | |
private static final String IDENTIFIER = "net.sourceforge.acelogger"; |
44 | |
|
45 | |
private Formatter formatter; |
46 | |
|
47 | |
private LocationGatherer gatherer; |
48 | |
|
49 | |
private static class InternalFormatter implements Formatter { |
50 | |
|
51 | |
private static final String RESOURCE_BUNDLE_NAME = |
52 | |
"net.sourceforge.acelogger.messages.InternalLogging"; |
53 | |
|
54 | 1 | private static final String LOG_ENTRY_PATTERN = |
55 | |
"{0,date,yyyy-MM-dd HH:mm:ss,SSS} [{1}] ({2}) - {3}" + FormatterConstants.NEW_LINE; |
56 | |
|
57 | |
private ResourceBundle resolver; |
58 | |
|
59 | 2 | public InternalFormatter() { |
60 | 2 | resolver = ResourceBundle.getBundle(RESOURCE_BUNDLE_NAME); |
61 | 2 | } |
62 | |
|
63 | |
|
64 | |
public String formatLogCall(LogEvent call) { |
65 | 10 | String resolvedPattern = ""; |
66 | |
try { |
67 | |
|
68 | 10 | resolvedPattern = resolver.getString(call.getMessage()); |
69 | 0 | } catch (NullPointerException e) { |
70 | |
|
71 | 10 | } catch (MissingResourceException e) { |
72 | |
|
73 | 0 | } catch (ClassCastException e) { |
74 | |
|
75 | 0 | } catch (Throwable e) { |
76 | 0 | e.printStackTrace(); |
77 | 10 | } |
78 | 10 | String formattedMessage = MessageFormat.format( |
79 | |
resolvedPattern, call.getMessageParameters() |
80 | |
); |
81 | 10 | StringBuilder formattedLogCall = new StringBuilder(); |
82 | 10 | formattedLogCall.append(MessageFormat.format(LOG_ENTRY_PATTERN, call.getCallTime(), |
83 | |
call.getLevel(), call.getLocation().getStackTraceLikeName(), formattedMessage |
84 | |
)); |
85 | 10 | if (call.getCause() != null) { |
86 | 10 | StringWriter stackTraceDestination = new StringWriter(); |
87 | 10 | PrintWriter stackTraceWriter = new PrintWriter(stackTraceDestination); |
88 | 10 | call.getCause().printStackTrace(stackTraceWriter); |
89 | 10 | stackTraceWriter.flush(); |
90 | 10 | stackTraceWriter.close(); |
91 | 10 | formattedLogCall.append(stackTraceDestination.toString()); |
92 | |
} |
93 | 10 | return formattedLogCall.toString(); |
94 | |
} |
95 | |
|
96 | |
|
97 | |
public String getIdentifier() { |
98 | 0 | return IDENTIFIER; |
99 | |
} |
100 | |
|
101 | |
} |
102 | |
|
103 | 4 | private static class InternalLocationGatherer implements LocationGatherer { |
104 | |
|
105 | |
private static final int FRAME_SHIFT = 3; |
106 | |
|
107 | |
|
108 | |
public LogEventLocation getCallLocation() { |
109 | 10 | return getCallLocation(Thread.currentThread()); |
110 | |
} |
111 | |
|
112 | |
|
113 | |
public LogEventLocation getCallLocation(Thread currentThread) { |
114 | 10 | StackTraceElement[] stackTrace = currentThread.getStackTrace(); |
115 | 10 | int userCodeCallIndex = 0; |
116 | 50 | for (int i = 0; i < stackTrace.length; i++) { |
117 | 50 | String frameClassName = stackTrace[i].getClassName(); |
118 | 50 | if (!frameClassName.startsWith("java.util.concurrent") |
119 | |
&& !frameClassName.startsWith("java.lang.Thread") |
120 | |
&& !frameClassName.startsWith(InternalLogger.class.getName()) |
121 | |
&& !frameClassName.startsWith(UncaughtExceptionLogger.class.getName())) { |
122 | 10 | userCodeCallIndex = i; |
123 | 10 | if (i + FRAME_SHIFT < stackTrace.length) { |
124 | 10 | userCodeCallIndex += FRAME_SHIFT; |
125 | |
} |
126 | |
break; |
127 | |
} |
128 | |
} |
129 | 10 | StackTraceElement userCodeFrame = stackTrace[userCodeCallIndex]; |
130 | 10 | return new LogEventLocation( |
131 | |
userCodeFrame.getClass().getPackage().getName(), |
132 | |
userCodeFrame.getClassName(), |
133 | |
userCodeFrame.getMethodName(), |
134 | |
userCodeFrame.getFileName(), |
135 | |
userCodeFrame.getLineNumber(), |
136 | |
currentThread.getName() |
137 | |
); |
138 | |
} |
139 | |
|
140 | |
|
141 | |
public String getIdentifier() { |
142 | 0 | return IDENTIFIER; |
143 | |
} |
144 | |
|
145 | |
} |
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | 2 | public InternalLogger() { |
153 | 2 | formatter = new InternalFormatter(); |
154 | 2 | gatherer = new InternalLocationGatherer(); |
155 | 2 | } |
156 | |
|
157 | |
|
158 | |
public void debug(String message, Object... params) { |
159 | |
|
160 | 0 | } |
161 | |
|
162 | |
|
163 | |
public void debug(String message, Throwable cause, Object... params) { |
164 | |
|
165 | 0 | } |
166 | |
|
167 | |
|
168 | |
public void error(String message, Object... params) { |
169 | 0 | LogEvent call = new LogEvent( |
170 | |
message, LogLevel.ERROR, gatherer.getCallLocation(), null, params); |
171 | 0 | System.out.print(formatter.formatLogCall(call)); |
172 | 0 | } |
173 | |
|
174 | |
|
175 | |
public void error(String message, Throwable cause, Object... params) { |
176 | 10 | LogEvent call = new LogEvent( |
177 | |
message, LogLevel.ERROR, gatherer.getCallLocation(), cause, params); |
178 | 10 | System.out.print(formatter.formatLogCall(call)); |
179 | 10 | } |
180 | |
|
181 | |
|
182 | |
public void fatal(String message, Object... params) { |
183 | 0 | LogEvent call = new LogEvent( |
184 | |
message, LogLevel.FATAL, gatherer.getCallLocation(), null, params); |
185 | 0 | System.out.print(formatter.formatLogCall(call)); |
186 | 0 | } |
187 | |
|
188 | |
|
189 | |
public void fatal(String message, Throwable cause, Object... params) { |
190 | 0 | LogEvent call = new LogEvent( |
191 | |
message, LogLevel.FATAL, gatherer.getCallLocation(), cause, params); |
192 | 0 | System.out.print(formatter.formatLogCall(call)); |
193 | 0 | } |
194 | |
|
195 | |
|
196 | |
public void info(String message, Object... params) { |
197 | 0 | LogEvent call = new LogEvent( |
198 | |
message, LogLevel.INFO, gatherer.getCallLocation(), null, params); |
199 | 0 | System.out.print(formatter.formatLogCall(call)); |
200 | 0 | } |
201 | |
|
202 | |
|
203 | |
public void info(String message, Throwable cause, Object... params) { |
204 | 0 | LogEvent call = new LogEvent( |
205 | |
message, LogLevel.INFO, gatherer.getCallLocation(), cause, params); |
206 | 0 | System.out.print(formatter.formatLogCall(call)); |
207 | 0 | } |
208 | |
|
209 | |
|
210 | |
public boolean isDebugEnabled() { |
211 | 0 | return false; |
212 | |
} |
213 | |
|
214 | |
|
215 | |
public boolean isErrorEnabled() { |
216 | 0 | return true; |
217 | |
} |
218 | |
|
219 | |
|
220 | |
public boolean isFatalEnabled() { |
221 | 0 | return true; |
222 | |
} |
223 | |
|
224 | |
|
225 | |
public boolean isInfoEnabled() { |
226 | 0 | return true; |
227 | |
} |
228 | |
|
229 | |
|
230 | |
public boolean isTraceEnabled() { |
231 | 0 | return false; |
232 | |
} |
233 | |
|
234 | |
|
235 | |
public boolean isWarnEnabled() { |
236 | 0 | return true; |
237 | |
} |
238 | |
|
239 | |
|
240 | |
public void trace(String message, Object... params) { |
241 | |
|
242 | 0 | } |
243 | |
|
244 | |
|
245 | |
public void trace(String message, Throwable cause, Object... params) { |
246 | |
|
247 | 0 | } |
248 | |
|
249 | |
|
250 | |
public void warn(String message, Object... params) { |
251 | |
|
252 | |
|
253 | 2 | } |
254 | |
|
255 | |
|
256 | |
public void warn(String message, Throwable cause, Object... params) { |
257 | |
|
258 | |
|
259 | 0 | } |
260 | |
|
261 | |
|
262 | |
public String getIdentifier() { |
263 | 0 | return IDENTIFIER; |
264 | |
} |
265 | |
|
266 | |
} |