Skip to content

Commit 9fe1f5d

Browse files
committed
New methods added
1 parent 0390aff commit 9fe1f5d

File tree

4 files changed

+130
-129
lines changed

4 files changed

+130
-129
lines changed

common/src/main/java/com/genexus/diagnostics/LogLevel.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
public class LogLevel {
44

5-
static final int OFF = 0;
6-
static final int TRACE = 1;
7-
static final int DEBUG = 5;
8-
static final int INFO = 10;
9-
static final int WARNING = 15;
10-
static final int ERROR = 20;
11-
static final int FATAL = 30;
12-
5+
public static final int OFF = 0;
6+
public static final int TRACE = 1;
7+
public static final int DEBUG = 5;
8+
public static final int INFO = 10;
9+
public static final int WARNING = 15;
10+
public static final int ERROR = 20;
11+
public static final int FATAL = 30;
1312

1413
}

common/src/main/java/com/genexus/diagnostics/UserLog.java

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,6 @@ public static void debug(String message, String topic, Throwable ex) {
121121
getLogger(topic).debug(message, ex);
122122
}
123123

124-
125-
/******* Log Improvements *****/
126-
127124
public static void setContext(String key, Object value) {
128125
// Topic is ignored, also if you put something
129126
getLogger("$").setContext(key, value);
@@ -133,4 +130,41 @@ public static void write(String message, String topic, int logLevel, Object data
133130
getLogger(topic).write(message, logLevel, data, stackTrace);
134131
}
135132

133+
public static void write(String message, String topic, int logLevel, Object data) {
134+
getLogger(topic).write(message, logLevel, data, false);
135+
}
136+
137+
public static boolean isDebugEnabled() {
138+
return getLogger().isDebugEnabled();
139+
}
140+
141+
public static boolean isErrorEnabled() {
142+
return getLogger().isErrorEnabled();
143+
}
144+
145+
public static boolean isFatalEnabled() {
146+
return getLogger().isFatalEnabled();
147+
}
148+
149+
public static boolean isInfoEnabled() {
150+
return getLogger().isInfoEnabled();
151+
}
152+
153+
public static boolean isWarnEnabled() {
154+
return getLogger().isWarnEnabled();
155+
}
156+
157+
public static boolean isTraceEnabled() {
158+
return getLogger().isTraceEnabled();
159+
}
160+
161+
public static boolean isEnabled(int logLevel) {
162+
return getLogger().isEnabled(logLevel);
163+
}
164+
165+
public static boolean isEnabled(int logLevel, String topic) {
166+
return getLogger(topic).isEnabled(logLevel);
167+
}
168+
169+
136170
}

common/src/main/java/com/genexus/diagnostics/core/ILogger.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,20 @@ public interface ILogger {
6565
* msg); } }
6666
*/
6767

68+
void setContext(String key, Object value);
6869

69-
/******* START - Log Improvements *****/
70-
// A default implementation is added for AndroidLogger because it fails, it needs an
71-
// implementation, another solution is to declare the class as abstract, but it is
72-
// not possible because of the way the class is made.
70+
void write(String message, int logLevel, Object data, boolean stackTrace);
7371

74-
default void setContext(String key, Object value) {}
72+
boolean isFatalEnabled();
7573

76-
default void write(String message, int logLevel, Object data, boolean stackTrace) {}
74+
boolean isWarnEnabled();
75+
76+
boolean isInfoEnabled();
77+
78+
boolean isTraceEnabled();
79+
80+
boolean isEnabled(int logLevel);
81+
82+
//boolean isEnabled(int logLevel, String topic);
7783

78-
/******* END - Log Improvements *****/
7984
}

wrappercommon/src/main/java/com/genexus/diagnostics/core/provider/Log4J2Logger.java

Lines changed: 74 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
import com.google.gson.reflect.TypeToken;
88
import org.apache.logging.log4j.Level;
99
import org.apache.logging.log4j.LogManager;
10+
import org.apache.logging.log4j.MarkerManager;
1011
import org.apache.logging.log4j.ThreadContext;
1112
import org.apache.logging.log4j.core.Appender;
1213
import org.apache.logging.log4j.core.LoggerContext;
1314
import org.apache.logging.log4j.core.appender.AbstractAppender;
1415
import org.apache.logging.log4j.core.config.Configuration;
1516
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;
1718

1819
import java.lang.reflect.Type;
1920
import 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

Comments
 (0)