77import com .google .gson .reflect .TypeToken ;
88import org .apache .logging .log4j .Level ;
99import org .apache .logging .log4j .LogManager ;
10+ import org .apache .logging .log4j .MarkerManager ;
1011import org .apache .logging .log4j .ThreadContext ;
1112import org .apache .logging .log4j .core .Appender ;
1213import org .apache .logging .log4j .core .LoggerContext ;
1314import org .apache .logging .log4j .core .appender .AbstractAppender ;
1415import org .apache .logging .log4j .core .config .Configuration ;
1516import org .apache .logging .log4j .layout .template .json .JsonTemplateLayout ;
16- import org .apache .logging .log4j .message .ObjectMessage ;
17+ import org .apache .logging .log4j .message .MapMessage ;
1718
1819import java .lang .reflect .Type ;
1920import java .util .*;
@@ -206,82 +207,102 @@ public boolean isErrorEnabled() {
206207 return log .isErrorEnabled ();
207208 }
208209
210+ public boolean isFatalEnabled () {
211+ return log .isFatalEnabled ();
212+ }
209213
214+ public boolean isWarnEnabled () {
215+ return log .isWarnEnabled ();
216+ }
210217
218+ public boolean isInfoEnabled () {
219+ return log .isInfoEnabled ();
220+ }
211221
222+ public boolean isTraceEnabled () {
223+ return log .isTraceEnabled ();
224+ }
212225
226+ public boolean isEnabled (int logLevel ) {
227+ return log .isEnabled (getLogLevel (logLevel ));
228+ }
213229
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+ }
216233
217234 public void setContext (String key , Object value ) {
218235 // Add entry to the MDC (only works for JSON log format)
219236 ThreadContext .put (key , fromObjectToString (value ));
220237 }
221238
222239 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+ }
225259
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+ }
229263
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 = "{} - {}" ;
234266
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 );
257268
269+ }
258270
271+ private void writeJsonFormat (String message , int logLevel , Object data , boolean stackTrace ) {
272+ String dataKey = "data" ;
273+ MapMessage <?, ?> mapMessage = new MapMessage <>().with ("message" , message );
259274
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+ }
262284
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+ }
271288
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 );
279290 }
280291
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+ }
282303
283304 private static String fromObjectToString (Object value ) {
284- String res = "" ;
305+ String res ;
285306 if (value == null ) {
286307 res = "null" ;
287308 } else if (value instanceof String && isJson ((String ) value )) {
@@ -311,47 +332,6 @@ private static boolean isJson(String input) {
311332 }
312333 }
313334
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-
355335 private static String getStackTrace () {
356336 StringBuilder stackTrace ;
357337 stackTrace = new StringBuilder ();
@@ -380,12 +360,6 @@ private static Map<String, Object> jsonStringToMap(String jsonString) {
380360 return gson .fromJson (jsonString , type );
381361 }
382362
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-
389363 private static boolean isJsonLogFormat () {
390364 LoggerContext context = (LoggerContext ) LogManager .getContext (false );
391365 Configuration config = context .getConfiguration ();
@@ -398,18 +372,7 @@ private static boolean isJsonLogFormat() {
398372 }
399373 }
400374 }
401-
402375 return false ;
403376 }
404377
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-
415378}
0 commit comments