-
Notifications
You must be signed in to change notification settings - Fork 401
/
StructuredArguments.java
320 lines (291 loc) · 11.2 KB
/
StructuredArguments.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
/*
* Copyright 2013-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.logstash.logback.argument;
import java.util.Arrays;
import java.util.Map;
import java.util.function.Supplier;
import net.logstash.logback.marker.MapEntriesAppendingMarker;
import net.logstash.logback.marker.ObjectAppendingMarker;
import net.logstash.logback.marker.ObjectFieldsAppendingMarker;
import net.logstash.logback.marker.RawJsonAppendingMarker;
import ch.qos.logback.core.Context;
import ch.qos.logback.core.status.StatusManager;
import ch.qos.logback.core.status.WarnStatus;
import org.slf4j.ILoggerFactory;
import org.slf4j.LoggerFactory;
/**
* Factory for creating {@link StructuredArgument}s.
*/
public class StructuredArguments {
/**
* The default message format used when writing key value pairs to the log message.
*/
public static final String DEFAULT_KEY_VALUE_MESSAGE_FORMAT_PATTERN = "{0}={1}";
/**
* A message format pattern that will only write
* the argument value to a log message (i.e. it won't write the key).
*/
public static final String VALUE_ONLY_MESSAGE_FORMAT_PATTERN = "{1}";
private StructuredArguments() {
}
/**
* Convenience method for calling {@link #keyValue(String, Object, String)}
* using the {@link #DEFAULT_KEY_VALUE_MESSAGE_FORMAT_PATTERN}.
* <p>
* Basically, adds "key":"value" to the JSON event AND
* name=value to the formatted message.
*
* @param key the key (field name)
* @param value the value
* @return a pre-populated {@link StructuredArgument} instance
*
* @see ObjectAppendingMarker
* @see #keyValue(String, Object, String)
* @see #DEFAULT_KEY_VALUE_MESSAGE_FORMAT_PATTERN
*/
public static StructuredArgument keyValue(String key, Object value) {
return keyValue(key, value, DEFAULT_KEY_VALUE_MESSAGE_FORMAT_PATTERN);
}
/**
* Abbreviated convenience method for calling {@link #keyValue(String, Object)}.
*
* @param key the key (field name)
* @param value the value
* @return a pre-populated {@link StructuredArgument} instance
*
* @see #keyValue(String, Object)
*/
public static StructuredArgument kv(String key, Object value) {
return keyValue(key, value);
}
/**
* Adds "key":"value" to the JSON event AND
* name/value to the formatted message using the given messageFormatPattern.
*
* @param key the key (field name)
* @param value the value
* @param messageFormatPattern the pattern used to concatenate the key and the value
* @return a pre-populated {@link StructuredArgument} instance
*
* @see ObjectAppendingMarker
*/
public static StructuredArgument keyValue(String key, Object value, String messageFormatPattern) {
return new ObjectAppendingMarker(key, value, messageFormatPattern);
}
/**
* Abbreviated convenience method for calling {@link #keyValue(String, Object, String)}.
*
* @param key the key (field name)
* @param value the value
* @param messageFormatPattern the pattern used to concatenate the key and the value
* @return a pre-populated {@link StructuredArgument} instance
*
* @see #keyValue(String, Object, String)
*/
public static StructuredArgument kv(String key, Object value, String messageFormatPattern) {
return keyValue(key, value, messageFormatPattern);
}
/**
* Adds "key":"value" to the JSON event AND
* value to the formatted message (without the key).
*
* @param key the key (field name)
* @param value the value
* @return a pre-populated {@link StructuredArgument} instance
*
* @see #keyValue(String, Object, String)
* @see #VALUE_ONLY_MESSAGE_FORMAT_PATTERN
*/
public static StructuredArgument value(String key, Object value) {
return keyValue(key, value, VALUE_ONLY_MESSAGE_FORMAT_PATTERN);
}
/**
* Abbreviated convenience method for calling {@link #value(String, Object)}.
*
* @param key the key (field name)
* @param value the value
* @return a pre-populated {@link StructuredArgument} instance
*
* @see #value(String, Object)
*/
public static StructuredArgument v(String key, Object value) {
return value(key, value);
}
/**
* Adds a "key":"value" entry for each Map entry to the JSON event AND
* {@code map.toString()} to the formatted message.
*
* @param map {@link Map} holding the key/value pairs
* @return a pre-populated {@link StructuredArgument} instance
*
* @see MapEntriesAppendingMarker
*/
public static StructuredArgument entries(Map<?, ?> map) {
return new MapEntriesAppendingMarker(map);
}
/**
* Abbreviated convenience method for calling {@link #entries(Map)}.
*
* @param map {@link Map} holding the key/value pairs
* @return a pre-populated {@link StructuredArgument} instance
*
* @see #entries(Map)
*/
public static StructuredArgument e(Map<?, ?> map) {
return entries(map);
}
/**
* Adds a "key":"value" entry for each field in the given object to the JSON event AND
* {@code object.toString()} to the formatted message.
*
* @param object the object to write fields from
* @return a pre-populated {@link StructuredArgument} instance
*
* @see ObjectFieldsAppendingMarker
*/
public static StructuredArgument fields(Object object) {
return new ObjectFieldsAppendingMarker(object);
}
/**
* Abbreviated convenience method for calling {@link #fields(Object)}.
*
* @param object the object to write fields from
* @return a pre-populated {@link StructuredArgument} instance
*
* @see #fields(Object)
*/
public static StructuredArgument f(Object object) {
return fields(object);
}
/**
* Adds a field to the JSON event whose key is {@code fieldName} and whose value
* is a JSON array of objects AND a string version of the array to the formatted message.
*
* @param fieldName field name
* @param objects elements of the array to write under the {@code fieldName} key
* @return a pre-populated {@link StructuredArgument} instance
*
* @see ObjectAppendingMarker
*/
public static StructuredArgument array(String fieldName, Object... objects) {
return new ObjectAppendingMarker(fieldName, objects);
}
/**
* Abbreviated convenience method for calling {@link #array(String, Object...)}.
*
* @param fieldName field name
* @param objects elements of the array to write under the {@code fieldName} key
* @return a pre-populated {@link StructuredArgument} instance
*
* @see #array(String, Object...)
*/
public static StructuredArgument a(String fieldName, Object... objects) {
return array(fieldName, objects);
}
/**
* Adds the {@code rawJsonValue} to the JSON event AND
* the {@code rawJsonValue} to the formatted message.
*
* @param fieldName field name
* @param rawJsonValue the raw JSON value
* @return a pre-populated {@link StructuredArgument} instance
*
* @see RawJsonAppendingMarker
*/
public static StructuredArgument raw(String fieldName, String rawJsonValue) {
return new RawJsonAppendingMarker(fieldName, rawJsonValue);
}
/**
* Abbreviated convenience method for calling {@link #raw(String, String)}.
*
* @param fieldName field name
* @param rawJsonValue the raw JSON value
* @return a pre-populated {@link StructuredArgument} instance
*
* @see #raw(String, String)
*/
public static StructuredArgument r(String fieldName, String rawJsonValue) {
return raw(fieldName, rawJsonValue);
}
/**
* Defer the evaluation of the argument until actually required.
*
* @param structuredArgumentSupplier a supplier for the argument value
* @return a pre-populated {@link StructuredArgument} instance
*
* @see DeferredStructuredArgument
*/
public static StructuredArgument defer(Supplier<? extends StructuredArgument> structuredArgumentSupplier) {
return new DeferredStructuredArgument(structuredArgumentSupplier);
}
/**
* Format the argument into a string.
*
* This method mimics the slf4j behavior:
* array objects are formatted as array using {@link Arrays#toString},
* non array object using {@link String#valueOf}.
*
* <p>See org.slf4j.helpers.MessageFormatter#deeplyAppendParameter(StringBuilder, Object, Map)}
*
* @param arg the argument to format
* @return formatted string version of the argument
*/
public static String toString(Object arg) {
if (arg == null) {
return "null";
}
Class<?> argClass = arg.getClass();
try {
if (!argClass.isArray()) {
return String.valueOf(arg);
} else {
if (argClass == byte[].class) {
return Arrays.toString((byte[]) arg);
} else if (argClass == short[].class) {
return Arrays.toString((short[]) arg);
} else if (argClass == int[].class) {
return Arrays.toString((int[]) arg);
} else if (argClass == long[].class) {
return Arrays.toString((long[]) arg);
} else if (argClass == char[].class) {
return Arrays.toString((char[]) arg);
} else if (argClass == float[].class) {
return Arrays.toString((float[]) arg);
} else if (argClass == double[].class) {
return Arrays.toString((double[]) arg);
} else if (argClass == boolean[].class) {
return Arrays.toString((boolean[]) arg);
} else {
return Arrays.deepToString((Object[]) arg);
}
}
} catch (Exception e) {
ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();
if (loggerFactory instanceof Context) {
Context context = (Context) loggerFactory;
StatusManager statusManager = context.getStatusManager();
statusManager.add(new WarnStatus(
"Failed toString() invocation on an object of type [" + argClass.getName() + "]",
StructuredArguments.class,
e));
} else {
System.err.println("Failed toString() invocation on an object of type [" + argClass.getName() + "]");
e.printStackTrace();
}
return "[FAILED toString()]";
}
}
}