forked from logfellow/logstash-logback-encoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StructuredArguments.java
246 lines (220 loc) · 8.34 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
/**
* 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 net.logstash.logback.marker.MapEntriesAppendingMarker;
import net.logstash.logback.marker.ObjectAppendingMarker;
import net.logstash.logback.marker.ObjectFieldsAppendingMarker;
import net.logstash.logback.marker.RawJsonAppendingMarker;
import org.slf4j.ILoggerFactory;
import org.slf4j.LoggerFactory;
import ch.qos.logback.core.Context;
import ch.qos.logback.core.status.StatusManager;
import ch.qos.logback.core.status.WarnStatus;
/**
* 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.
*
* @see ObjectAppendingMarker
* @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)}.
*
* @see ObjectAppendingMarker
*/
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.
*
* @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)}.
*
* @see ObjectAppendingMarker
*/
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).
*
* @see ObjectAppendingMarker
* @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)}.
*
* @see ObjectAppendingMarker
*/
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
* map.toString() to the formatted message.
*
* @see MapEntriesAppendingMarker
*/
public static StructuredArgument entries(Map<?, ?> map) {
return new MapEntriesAppendingMarker(map);
}
/**
* Abbreviated convenience method for calling {@link #entries(Map)}.
*
* @see MapEntriesAppendingMarker
*/
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
* object.toString() to the formatted message.
*
* @see ObjectFieldsAppendingMarker
*/
public static StructuredArgument fields(Object object) {
return new ObjectFieldsAppendingMarker(object);
}
/**
* Abbreviated convenience method for calling {@link #fields(Object)}.
*
* @see ObjectFieldsAppendingMarker
*/
public static StructuredArgument f(Object object) {
return fields(object);
}
/**
* Adds a field to the JSON event whose key is fieldName and whose value is a JSON array of objects AND
* a string version of the array to the formatted message.
*
* @see ObjectAppendingMarker
*/
public static StructuredArgument array(String fieldName, Object... objects) {
return new ObjectAppendingMarker(fieldName, objects);
}
/**
* Abbreviated convenience method for calling {@link #array(String, Object...)}.
*
* @see ObjectAppendingMarker
*/
public static StructuredArgument a(String fieldName, Object... objects) {
return array(fieldName, objects);
}
/**
* Adds the rawJsonValue to the JSON event AND
* the rawJsonValue to the formatted message.
*
* @see RawJsonAppendingMarker
*/
public static StructuredArgument raw(String fieldName, String rawJsonValue) {
return new RawJsonAppendingMarker(fieldName, rawJsonValue);
}
/**
* Abbreviated convenience method for calling {@link #raw(String, String)}.
*
* @see RawJsonAppendingMarker
*/
public static StructuredArgument r(String fieldName, String rawJsonValue) {
return raw(fieldName, rawJsonValue);
}
/**
* Format the argument into a string.
* <p>
* This method mimics the slf4j behaviour:
* 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)}.
*/
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()]";
}
}
}