-
Notifications
You must be signed in to change notification settings - Fork 136
/
GlassFishLogRecord.java
327 lines (249 loc) · 8.39 KB
/
GlassFishLogRecord.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
* Copyright (c) 2022, 2024 Eclipse Foundation and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.main.jul.record;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.util.ResourceBundle;
import java.util.logging.ErrorManager;
import java.util.logging.Level;
import java.util.logging.LogRecord;
/**
* This class provides additional attributes not supported by JUL LogRecord
*
* @author David Matejcek
*/
public class GlassFishLogRecord extends LogRecord {
private static final long serialVersionUID = -4820672332353631291L;
private static final ZoneId TIME_ZONE = ZoneId.systemDefault();
private final LogRecord record;
private final String threadName;
private String messageKey;
/**
* Creates new record. Source class and method will be autodetected now or set after this
* constructor ends.
*
* @param level
* @param message
* @param autodetectSource
*/
public GlassFishLogRecord(final Level level, final String message, final boolean autodetectSource) {
this(new LogRecord(level, message), autodetectSource);
}
/**
* Wraps the log record.
*
* @param record
* @param autodetectSource
*/
public GlassFishLogRecord(final LogRecord record, final boolean autodetectSource) {
super(record.getLevel(), null);
this.threadName = Thread.currentThread().getName();
this.record = record;
if (autodetectSource) {
detectClassAndMethod(record);
}
}
/**
* @return the message identifier (generally not unique, may be null)
*/
public String getMessageKey() {
return messageKey;
}
/**
* This is called just to remember the original message value after it was translated using
* the resource bundle.
*
* @param messageKey the message identifier (generally not unique, may be null)
*/
void setMessageKey(final String messageKey) {
this.messageKey = messageKey;
}
/**
* @return name of the thread which created this log record.
*/
public String getThreadName() {
return threadName;
}
@Override
public Level getLevel() {
return this.record.getLevel();
}
@Override
public void setLevel(final Level level) {
this.record.setLevel(level);
}
@Override
public long getSequenceNumber() {
return this.record.getSequenceNumber();
}
@Override
public void setSequenceNumber(final long seq) {
this.record.setSequenceNumber(seq);
}
@Override
public String getLoggerName() {
return this.record.getLoggerName();
}
@Override
public void setLoggerName(final String name) {
this.record.setLoggerName(name);
}
@Override
public String getSourceClassName() {
return this.record.getSourceClassName();
}
@Override
public void setSourceClassName(final String className) {
this.record.setSourceClassName(className == null || className.isEmpty() ? null : className);
}
@Override
public String getSourceMethodName() {
return this.record.getSourceMethodName();
}
@Override
public void setSourceMethodName(final String methodName) {
this.record.setSourceMethodName(methodName == null || methodName.isEmpty() ? null : methodName);
}
@Override
public String getMessage() {
return this.record.getMessage();
}
@Override
public void setMessage(final String message) {
this.record.setMessage(message == null || message.isEmpty() ? null : message);
}
@Override
public Object[] getParameters() {
return this.record.getParameters();
}
@Override
public void setParameters(final Object[] parameters) {
this.record.setParameters(parameters);
}
@Override
public int getThreadID() {
return this.record.getThreadID();
}
@Override
public void setThreadID(final int threadID) {
this.record.setThreadID(threadID);
}
@Override
public long getMillis() {
return this.record.getMillis();
}
@SuppressWarnings("deprecation")
@Deprecated
@Override
public void setMillis(final long millis) {
this.record.setMillis(millis);
}
@Override
public void setInstant(final Instant instant) {
this.record.setInstant(instant);
}
@Override
public Instant getInstant() {
return this.record.getInstant();
}
@Override
public Throwable getThrown() {
return this.record.getThrown();
}
@Override
public void setThrown(final Throwable thrown) {
this.record.setThrown(thrown);
}
@Override
public ResourceBundle getResourceBundle() {
return this.record.getResourceBundle();
}
@Override
public void setResourceBundle(final ResourceBundle bundle) {
this.record.setResourceBundle(bundle);
}
@Override
public String getResourceBundleName() {
return this.record.getResourceBundleName();
}
@Override
public void setResourceBundleName(final String name) {
this.record.setResourceBundleName(name);
}
/**
* @return {@link #getMillis()} converted to {@link OffsetDateTime} in local time zone.
*/
public OffsetDateTime getTime() {
return OffsetDateTime.ofInstant(getInstant(), TIME_ZONE);
}
/**
* @return printed stacktrace of {@link #getThrown()} or null
*/
public String getThrownStackTrace() {
if (getThrown() == null) {
return null;
}
try (StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw)) {
getThrown().printStackTrace(pw);
return sw.toString();
} catch (final IOException e) {
new ErrorManager().error("Cannot print stacktrace!", e, ErrorManager.FORMAT_FAILURE);
return null;
}
}
@Override
public String toString() {
return getMessage();
}
private void detectClassAndMethod(final LogRecord wrappedRecord) {
final StackTraceElement[] stack = Thread.currentThread().getStackTrace();
boolean ignoredClass = false;
for (final StackTraceElement element : stack) {
final String className = element.getClassName();
if (!ignoredClass) {
ignoredClass = isIgnoredStackTraceElement(className);
continue;
}
if (!isIgnoredStackTraceElement(className)) {
wrappedRecord.setSourceClassName(className);
wrappedRecord.setSourceMethodName(element.getMethodName());
return;
}
}
}
/**
* @param sourceClassName usually class which created this record
* @return if true the class will not be used as a source.
*/
protected boolean isIgnoredStackTraceElement(final String sourceClassName) {
return "org.glassfish.main.jul.GlassFishLogger".equals(sourceClassName)
|| "org.glassfish.main.jul.GlassFishLoggerWrapper".equals(sourceClassName)
// see LogDomains in GlassFish sources
|| "com.sun.logging.LogDomainsLogger".equals(sourceClassName)
// remaining classes are in the JDK
|| "java.util.logging.Logger".equals(sourceClassName)
|| "java.util.logging.LoggingProxyImpl".equals(sourceClassName)
// see LoggingPrintStream
|| "java.lang.Throwable".equals(sourceClassName)
|| sourceClassName.startsWith("java.lang.reflect.")
|| sourceClassName.startsWith("sun.util.logging.")
|| sourceClassName.startsWith("sun.reflect.");
}
}