7
7
import com .google .gson .reflect .TypeToken ;
8
8
import org .apache .logging .log4j .Level ;
9
9
import org .apache .logging .log4j .LogManager ;
10
+ import org .apache .logging .log4j .MarkerManager ;
10
11
import org .apache .logging .log4j .ThreadContext ;
11
12
import org .apache .logging .log4j .core .Appender ;
12
13
import org .apache .logging .log4j .core .LoggerContext ;
13
14
import org .apache .logging .log4j .core .appender .AbstractAppender ;
14
15
import org .apache .logging .log4j .core .config .Configuration ;
15
16
import org .apache .logging .log4j .layout .template .json .JsonTemplateLayout ;
16
- import org .apache .logging .log4j .message .ObjectMessage ;
17
+ import org .apache .logging .log4j .message .MapMessage ;
17
18
18
19
import java .lang .reflect .Type ;
19
20
import java .util .*;
@@ -206,82 +207,102 @@ public boolean isErrorEnabled() {
206
207
return log .isErrorEnabled ();
207
208
}
208
209
210
+ public boolean isFatalEnabled () {
211
+ return log .isFatalEnabled ();
212
+ }
209
213
214
+ public boolean isWarnEnabled () {
215
+ return log .isWarnEnabled ();
216
+ }
210
217
218
+ public boolean isInfoEnabled () {
219
+ return log .isInfoEnabled ();
220
+ }
211
221
222
+ public boolean isTraceEnabled () {
223
+ return log .isTraceEnabled ();
224
+ }
212
225
226
+ public boolean isEnabled (int logLevel ) {
227
+ return log .isEnabled (getLogLevel (logLevel ));
228
+ }
213
229
214
-
215
- /******** NEW methods to improve logging ********/
230
+ public boolean isEnabled (int logLevel , String marker ) {
231
+ return log .isEnabled (getLogLevel (logLevel ), MarkerManager .getMarker (marker ));
232
+ }
216
233
217
234
public void setContext (String key , Object value ) {
218
235
// Add entry to the MDC (only works for JSON log format)
219
236
ThreadContext .put (key , fromObjectToString (value ));
220
237
}
221
238
222
239
public void write (String message , int logLevel , Object data , boolean stackTrace ) {
223
- printLog ("data" , data , stackTrace , Level .DEBUG );
224
- }
240
+ if (isJsonLogFormat ())
241
+ writeJsonFormat (message , logLevel , data , stackTrace );
242
+ else
243
+ writeTextFormat (message , logLevel , data , stackTrace );
244
+ }
245
+
246
+ private void writeTextFormat (String message , int logLevel , Object data , boolean stackTrace ) {
247
+ String dataKey = "data" ;
248
+ Map <String , Object > mapMessage = new LinkedHashMap <>();
249
+
250
+ if (data == null || (data instanceof String && "null" .equals (data .toString ()))) {
251
+ mapMessage .put (dataKey , (Object ) null );
252
+ } else if (data instanceof GxUserType ) { // SDT
253
+ mapMessage .put (dataKey , jsonStringToMap (fromObjectToString (data )));
254
+ } else if (data instanceof String && isJson ((String ) data )) { // JSON Strings
255
+ mapMessage .put (dataKey , jsonStringToMap (fromObjectToString (data )));
256
+ } else {
257
+ mapMessage .put (dataKey , data );
258
+ }
225
259
226
- public void write ( String message , int logLevel , Object data ) {
227
- printLog ( "data " , data , false , Level . DEBUG );
228
- }
260
+ if ( stackTrace ) {
261
+ mapMessage . put ( "stackTrace " , getStackTraceAsList () );
262
+ }
229
263
230
- private ObjectMessage buildLogMessage (String messageKey , Object messageValue , boolean stackTrace ) {
231
- Map <String , Object > messageMap ;
232
- String stacktraceLabel = "stackTrace" ;
233
- String msgLabel = "message" ;
264
+ String json = new Gson ().newBuilder ().serializeNulls ().create ().toJson (mapMessage );
265
+ String format = "{} - {}" ;
234
266
235
- if (isNullOrBlank (messageValue )) {
236
- if (stackTrace ) {
237
- messageMap = new LinkedHashMap <>();
238
- messageMap .put (msgLabel , messageKey );
239
- messageMap .put (stacktraceLabel , getStackTraceAsList ());
240
- if (isJsonLogFormat ())
241
- return new ObjectMessage (messageMap );
242
- else
243
- return new ObjectMessage (new Gson ().toJson (messageMap ));
244
- }
245
- return new ObjectMessage (messageKey );
246
- } else {
247
- messageMap = objectToMap (messageKey , messageValue );
248
- if (stackTrace ) {
249
- messageMap .put (stacktraceLabel , getStackTraceAsList ());
250
- }
251
- if (isJsonLogFormat ())
252
- return new ObjectMessage (messageMap );
253
- else
254
- return new ObjectMessage (new Gson ().toJson (messageMap ));
255
- }
256
- }
267
+ log .log (getLogLevel (logLevel ), format , message , json );
257
268
269
+ }
258
270
271
+ private void writeJsonFormat (String message , int logLevel , Object data , boolean stackTrace ) {
272
+ String dataKey = "data" ;
273
+ MapMessage <?, ?> mapMessage = new MapMessage <>().with ("message" , message );
259
274
260
- private void printLog (final String messageKey , final Object messageValue , final boolean stackTrace ,
261
- final Level logLevel ) {
275
+ if (data == null || (data instanceof String && "null" .equals (data .toString ()))) {
276
+ mapMessage .with (dataKey , (Object ) null );
277
+ } else if (data instanceof GxUserType ) { // SDT
278
+ mapMessage .with (dataKey , jsonStringToMap (fromObjectToString (data )));
279
+ } else if (data instanceof String && isJson ((String ) data )) { // JSON Strings
280
+ mapMessage .with (dataKey , jsonStringToMap (fromObjectToString (data )));
281
+ } else {
282
+ mapMessage .with (dataKey , data );
283
+ }
262
284
263
- /* Generate the message JSON in this format:
264
- * { "message" :
265
- * {
266
- * "messageKey": "USER messageValue",
267
- * }
268
- * }
269
- * */
270
- ObjectMessage om = buildLogMessage (messageKey , messageValue , stackTrace );
285
+ if (stackTrace ) {
286
+ mapMessage .with ("stackTrace" , getStackTraceAsList ());
287
+ }
271
288
272
- // Log the message received or the crafted msg
273
- if (logLevel .equals (Level .FATAL )) log .fatal (om );
274
- else if (logLevel .equals (Level .ERROR )) log .error (om );
275
- else if (logLevel .equals (Level .WARN )) log .warn (om );
276
- else if (logLevel .equals (Level .INFO )) log .info (om );
277
- else if (logLevel .equals (Level .DEBUG )) log .debug (om );
278
- else if (logLevel .equals (Level .TRACE )) log .trace (om );
289
+ log .log (getLogLevel (logLevel ), mapMessage );
279
290
}
280
291
281
-
292
+ private Level getLogLevel (int logLevel ) {
293
+ switch (logLevel ) {
294
+ case LogLevel .OFF : return Level .OFF ;
295
+ case LogLevel .TRACE : return Level .TRACE ;
296
+ case LogLevel .INFO : return Level .INFO ;
297
+ case LogLevel .WARNING : return Level .WARN ;
298
+ case LogLevel .ERROR : return Level .ERROR ;
299
+ case LogLevel .FATAL : return Level .FATAL ;
300
+ default : return Level .DEBUG ;
301
+ }
302
+ }
282
303
283
304
private static String fromObjectToString (Object value ) {
284
- String res = "" ;
305
+ String res ;
285
306
if (value == null ) {
286
307
res = "null" ;
287
308
} else if (value instanceof String && isJson ((String ) value )) {
@@ -311,47 +332,6 @@ private static boolean isJson(String input) {
311
332
}
312
333
}
313
334
314
- private Map <String , Object > objectToMap (String key , Object value ) {
315
- Map <String , Object > result = new LinkedHashMap <>();
316
- if (value == null ) {
317
- result .put (key , null );
318
- } else if (value instanceof Number || value instanceof Boolean
319
- || value instanceof Map || value instanceof List ) {
320
- result .put (key , value );
321
- } else if (value instanceof GxUserType ) {
322
- result .put (key , jsonStringToMap (((GxUserType ) value ).toJSonString ()));
323
- } else if (value instanceof String ) {
324
- String str = (String ) value ;
325
-
326
- // Try to parse as JSON
327
- try {
328
- JsonElement parsed = JsonParser .parseString (str );
329
- Gson gson = new Gson ();
330
- if (parsed .isJsonObject ()) {
331
- result .put (key , gson .fromJson (parsed , Map .class ));
332
- } else if (parsed .isJsonArray ()) {
333
- result .put (key , gson .fromJson (parsed , List .class ));
334
- } else if (parsed .isJsonPrimitive ()) {
335
- JsonPrimitive primitive = parsed .getAsJsonPrimitive ();
336
- if (primitive .isBoolean ()) {
337
- result .put (key , primitive .getAsBoolean ());
338
- } else if (primitive .isNumber ()) {
339
- result .put (key , primitive .getAsNumber ());
340
- } else if (primitive .isString ()) {
341
- result .put (key , primitive .getAsString ());
342
- }
343
- }
344
- } catch (JsonSyntaxException e ) {
345
- // Invalid JSON: it is left as string
346
- result .put (key , str );
347
- }
348
- } else {
349
- // Any other object: convert to string
350
- result .put (key , value .toString ());
351
- }
352
- return result ;
353
- }
354
-
355
335
private static String getStackTrace () {
356
336
StringBuilder stackTrace ;
357
337
stackTrace = new StringBuilder ();
@@ -380,12 +360,6 @@ private static Map<String, Object> jsonStringToMap(String jsonString) {
380
360
return gson .fromJson (jsonString , type );
381
361
}
382
362
383
- private String toJson (String key , Object value ) {
384
- Map <String , Object > map = new HashMap <>();
385
- map .put (key , value );
386
- return new Gson ().toJson (map );
387
- }
388
-
389
363
private static boolean isJsonLogFormat () {
390
364
LoggerContext context = (LoggerContext ) LogManager .getContext (false );
391
365
Configuration config = context .getConfiguration ();
@@ -398,18 +372,7 @@ private static boolean isJsonLogFormat() {
398
372
}
399
373
}
400
374
}
401
-
402
375
return false ;
403
376
}
404
377
405
- public static boolean isNullOrBlank (Object obj ) {
406
- if (obj == null ) {
407
- return true ;
408
- }
409
- if (obj instanceof String ) {
410
- return ((String ) obj ).trim ().isEmpty ();
411
- }
412
- return false ; // It is not null, and it isn't an empty string
413
- }
414
-
415
378
}
0 commit comments