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
|
// SPDX-FileCopyrightText: © 2021 Matthias Andreas Benkard <code@mail.matthias.benkard.de>
//
// SPDX-License-Identifier: LGPL-3.0-or-later
package eu.mulk.quarkus.googlecloud.jsonlogging;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.ServiceLoader.Provider;
import java.util.logging.Level;
import java.util.stream.Collectors;
import org.jboss.logmanager.ExtFormatter;
import org.jboss.logmanager.ExtLogRecord;
/**
* Formats log records as JSON for consumption by Google Cloud Logging.
*
* <p>Meant to be used in containers running on Google Kubernetes Engine (GKE).
*
* @see LogEntry
*/
public class Formatter extends ExtFormatter {
private static final String TRACE_LEVEL = "TRACE";
private static final String DEBUG_LEVEL = "DEBUG";
private static final String INFO_LEVEL = "INFO";
private static final String NOTICE_LEVEL = "NOTICE";
private static final String WARNING_LEVEL = "WARNING";
private static final String ERROR_LEVEL = "ERROR";
private static final String CRITICAL_LEVEL = "CRITICAL";
private static final String ALERT_LEVEL = "ALERT";
private static final String EMERGENCY_LEVEL = "EMERGENCY";
private static final String ERROR_EVENT_TYPE =
"type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent";
private final List<StructuredParameterProvider> parameterProviders;
private final List<LabelProvider> labelProviders;
private final ThreadLocal<StringBuilder> stringBuilder;
/**
* Constructs a {@link Formatter} with custom configuration.
*
* <p><strong>Note:</strong> This constructor does not automatically discover providers using the
* {@link ServiceLoader} mechanism. See {@link #load} for this case use.
*
* @param parameterProviders the {@link StructuredParameterProvider}s to apply to each log entry.
* @param labelProviders the {@link LabelProvider}s to apply to each log entry.
*/
public Formatter(
Collection<StructuredParameterProvider> parameterProviders,
Collection<LabelProvider> labelProviders) {
this.parameterProviders = List.copyOf(parameterProviders);
this.labelProviders = List.copyOf(labelProviders);
this.stringBuilder = ThreadLocal.withInitial(StringBuilder::new);
}
/**
* Constructs a {@link Formatter} with parameter and label providers supplied by {@link
* ServiceLoader}.
*
* <p>In addition to the providers supplied as parameters, this factory method loads all {@link
* StructuredParameterProvider}s and {@link LabelProvider}s found through the {@link
* ServiceLoader} mechanism.
*
* @param parameterProviders the {@link StructuredParameterProvider}s to apply to each log entry.
* @param labelProviders the {@link LabelProvider}s to apply to each log entry.
* @return a new formatter.
*/
public static Formatter load(
Collection<StructuredParameterProvider> parameterProviders,
Collection<LabelProvider> labelProviders) {
parameterProviders = new ArrayList<>(parameterProviders);
parameterProviders.addAll(loadStructuredParameterProviders());
labelProviders = new ArrayList<>(labelProviders);
labelProviders.addAll(loadLabelProviders());
return new Formatter(parameterProviders, labelProviders);
}
private static List<StructuredParameterProvider> loadStructuredParameterProviders() {
return ServiceLoader.load(StructuredParameterProvider.class, Formatter.class.getClassLoader())
.stream()
.map(Provider::get)
.collect(Collectors.toList());
}
private static List<LabelProvider> loadLabelProviders() {
return ServiceLoader.load(LabelProvider.class, Formatter.class.getClassLoader()).stream()
.map(Provider::get)
.collect(Collectors.toList());
}
@Override
public String format(ExtLogRecord logRecord) {
var message = formatMessageWithStackTrace(logRecord);
List<StructuredParameter> parameters = new ArrayList<>();
Map<String, String> labels = new HashMap<>();
var providerContext = new ProviderContext(logRecord);
for (var parameterProvider : parameterProviders) {
var parameter = parameterProvider.getParameter(providerContext);
if (parameter != null) {
parameters.add(parameter);
}
}
for (var labelProvider : labelProviders) {
var providedLabels = labelProvider.getLabels(providerContext);
if (providedLabels != null) {
for (var label : providedLabels) {
labels.put(label.key(), label.value());
}
}
}
String insertId = null;
if (logRecord.getParameters() != null) {
for (var parameter : logRecord.getParameters()) {
if (parameter instanceof StructuredParameter) {
parameters.add((StructuredParameter) parameter);
} else if (parameter instanceof Label) {
var label = (Label) parameter;
labels.put(label.key(), label.value());
} else if (parameter instanceof InsertId) {
insertId = ((InsertId) parameter).value();
}
}
}
var mdc = logRecord.getMdcCopy();
var ndc = logRecord.getNdc();
var sourceLocation = sourceLocationOf(logRecord);
var entry =
new LogEntry(
message,
severityOf(logRecord.getLevel()),
new LogEntry.Timestamp(logRecord.getInstant()),
null,
null,
sourceLocation,
labels,
parameters,
mdc,
ndc,
logRecord.getLevel().intValue() >= 1000 ? ERROR_EVENT_TYPE : null,
insertId);
var b = stringBuilder.get();
b.delete(0, b.length());
b.append("{");
entry.json(b);
b.append("}\n");
return b.toString();
}
private static LogEntry.SourceLocation sourceLocationOf(ExtLogRecord logRecord) {
var sourceFileName = logRecord.getSourceFileName();
var sourceLineNumber = logRecord.getSourceLineNumber();
var sourceClassName = logRecord.getSourceClassName();
var sourceMethodName = logRecord.getSourceMethodName();
return (sourceFileName == null
&& sourceLineNumber <= 0
&& sourceClassName == null
&& sourceMethodName == null)
? null
: new LogEntry.SourceLocation(
sourceFileName,
String.valueOf(sourceLineNumber),
String.format("%s.%s", sourceClassName, sourceMethodName));
}
/**
* Formats the log message corresponding to {@code logRecord} including a stack trace of the
* {@link ExtLogRecord#getThrown()} exception if any.
*/
private String formatMessageWithStackTrace(ExtLogRecord logRecord) {
var messageStringWriter = new StringWriter();
var messagePrintWriter = new PrintWriter(messageStringWriter);
messagePrintWriter.append(this.formatMessage(logRecord));
if (logRecord.getThrown() != null) {
messagePrintWriter.println();
logRecord.getThrown().printStackTrace(messagePrintWriter);
}
messagePrintWriter.close();
return messageStringWriter.toString();
}
/** Computes the Google Cloud Logging severity corresponding to a given {@link Level}. */
private static String severityOf(Level level) {
if (level.intValue() < 500) {
return TRACE_LEVEL;
} else if (level.intValue() < 700) {
return DEBUG_LEVEL;
} else if (level.intValue() < 850) {
return INFO_LEVEL;
} else if (level.intValue() < 900) {
return NOTICE_LEVEL;
} else if (level.intValue() < 1000) {
return WARNING_LEVEL;
} else if (level.intValue() < 1100) {
return ERROR_LEVEL;
} else if (level.intValue() < 1200) {
return CRITICAL_LEVEL;
} else if (level.intValue() < 1300) {
return ALERT_LEVEL;
} else {
return EMERGENCY_LEVEL;
}
}
/**
* An implementation of {@link LabelProvider.Context} and {@link
* StructuredParameterProvider.Context}.
*/
private static class ProviderContext
implements LabelProvider.Context, StructuredParameterProvider.Context {
private final String loggerName;
private final long sequenceNumber;
private final String threadName;
private ProviderContext(ExtLogRecord logRecord) {
loggerName = logRecord.getLoggerName();
sequenceNumber = logRecord.getSequenceNumber();
threadName = logRecord.getThreadName();
}
@Override
public String loggerName() {
return loggerName;
}
@Override
public long sequenceNumber() {
return sequenceNumber;
}
@Override
public String threadName() {
return threadName;
}
}
}
|