Skip to content

Commit 6064830

Browse files
committed
Refactoring
(cherry picked from commit 33f00db)
1 parent 22cad39 commit 6064830

File tree

4 files changed

+57
-50
lines changed

4 files changed

+57
-50
lines changed

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,25 @@ public class Log {
77
private static ILogger getLogger() {
88
return getLogger("");
99
}
10-
10+
1111
public static ILogger getMainLogger() {
1212
return LogManager.getLogger("com.genexus.logging");
1313
}
14-
14+
1515
private static ILogger getLogger(String topic) {
1616
ILogger log;
1717
if (topic != null && topic.length() > 0) {
1818
log = LogManager.getLogger(topic);
19-
}
20-
else {
19+
} else {
2120
log = getMainLogger();
2221
}
2322
return log;
2423
}
25-
24+
2625
public static void write(int logLevel, String message, String topic) {
2726
write(message, topic, logLevel);
2827
}
29-
28+
3029
public static void write(String message, String topic, int logLevel) {
3130
ILogger log = getLogger(topic);
3231
LogLevel level = LogLevel.fromInt(logLevel);
@@ -51,17 +50,17 @@ public static void write(String message, String topic, int logLevel) {
5150
break;
5251
default:
5352
log.debug(message);
54-
}
53+
}
5554
}
56-
55+
5756
public static void write(String message) {
5857
getLogger().debug(message);
5958
}
60-
59+
6160
public static void write(String message, String topic) {
6261
getLogger(topic).debug(message);
6362
}
64-
63+
6564
public static void error(String message) {
6665
getLogger().error(message);
6766
}
@@ -85,7 +84,7 @@ public static void fatal(String message, String topic) {
8584
public static void fatal(String message, String topic, Throwable ex) {
8685
getLogger(topic).fatal(message, ex);
8786
}
88-
87+
8988
public static void warning(String message) {
9089
getLogger().warn(message);
9190
}
@@ -113,7 +112,7 @@ public static void debug(String message) {
113112
public static void debug(String message, String topic) {
114113
getLogger(topic).debug(message);
115114
}
116-
115+
117116
public static void debug(String message, String topic, Throwable ex) {
118117
getLogger(topic).debug(message, ex);
119118
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ public static ILogger getMainLogger() {
1919
private static ILogger getLogger(String topic) {
2020
ILogger log;
2121
if (topic != null && topic.length() > 0) {
22-
String loggerName = topic.startsWith("$") ? topic.substring(1): String.format("%s.%s", defaultUserLogNamespace, topic.trim());
22+
String loggerName = topic.startsWith("$") ? topic.substring(1) : String.format("%s.%s", defaultUserLogNamespace, topic.trim());
2323
log = LogManager.getLogger(loggerName);
24-
}
25-
else {
24+
} else {
2625
log = getMainLogger();
2726
}
2827
return log;

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
11
package com.genexus.diagnostics.core;
22

33
public interface ILogger {
4-
4+
55
void fatal(String msg, Throwable ex);
66

77
void fatal(String msg1, String msg2, Throwable ex);
88

99
void fatal(Throwable ex, String[] list);
1010

1111
void fatal(String[] list);
12-
12+
1313
void fatal(String msg);
14-
14+
1515
void error(String msg, Throwable ex);
1616

1717
void error(String msg1, String msg2, Throwable ex);
1818

1919
void error(Throwable ex, String[] list);
2020

2121
void error(String[] list);
22-
22+
2323
void error(String msg);
2424

2525
void warn(String msg);
26-
26+
2727
void warn(Throwable ex, String[] list);
2828

2929
void warn(String[] list);
3030

3131
void warn(String msg, Throwable ex);
3232

3333
void debug(String msg);
34-
34+
3535
void debug(Throwable ex, String[] list);
3636

3737
void debug(String[] list);
@@ -41,19 +41,19 @@ public interface ILogger {
4141
void debug(String msg, Throwable ex);
4242

4343
void info(String[] list);
44-
44+
4545
void info(String msg);
4646

4747
void trace(String msg);
48-
48+
4949
void trace(Throwable ex, String[] list);
5050

5151
void trace(String[] list);
5252

5353
void trace(String msg1, String msg2, Throwable ex);
5454

5555
void trace(String msg, Throwable ex);
56-
56+
5757
boolean isDebugEnabled();
5858

5959
boolean isErrorEnabled();

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

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.genexus.diagnostics.core.ILogger;
55
import org.apache.logging.log4j.Level;
66
import org.apache.logging.log4j.LogManager;
7-
import org.apache.logging.log4j.MarkerManager;
87
import org.apache.logging.log4j.ThreadContext;
98
import org.apache.logging.log4j.core.Appender;
109
import org.apache.logging.log4j.core.LoggerContext;
@@ -21,9 +20,13 @@
2120
import java.util.Map;
2221

2322
public class Log4J2Logger implements ILogger {
24-
private org.apache.logging.log4j.Logger log;
23+
private static final String STACKTRACE_KEY = "stackTrace";
24+
private static final String MESSAGE_KEY = "message";
25+
private static final String DATA_KEY = "data";
2526
private static final boolean IS_JSON_FORMAT = isJsonLogFormat();
2627

28+
private final org.apache.logging.log4j.Logger log;
29+
2730
public Log4J2Logger(final Class<?> clazz) {
2831
log = org.apache.logging.log4j.LogManager.getLogger(clazz);
2932
}
@@ -166,7 +169,7 @@ public void warn(String msg) {
166169
public void trace(String msg) {
167170
log.trace(msg);
168171
}
169-
172+
170173
public void trace(Throwable ex, String[] list) {
171174
if (log.isTraceEnabled()) {
172175
StringBuilder msg = new StringBuilder();
@@ -209,57 +212,56 @@ public boolean isErrorEnabled() {
209212
return log.isErrorEnabled();
210213
}
211214

215+
@Override
212216
public boolean isFatalEnabled() {
213217
return log.isFatalEnabled();
214218
}
215219

220+
@Override
216221
public boolean isWarnEnabled() {
217222
return log.isWarnEnabled();
218223
}
219224

225+
@Override
220226
public boolean isInfoEnabled() {
221227
return log.isInfoEnabled();
222228
}
223229

230+
@Override
224231
public boolean isTraceEnabled() {
225232
return log.isTraceEnabled();
226233
}
227234

235+
@Override
228236
public boolean isEnabled(int logLevel) {
229237
return log.isEnabled(getLogLevel(logLevel));
230238
}
231239

232-
public boolean isEnabled(int logLevel, String marker) {
233-
return log.isEnabled(getLogLevel(logLevel), MarkerManager.getMarker(marker));
234-
}
235-
240+
@Override
236241
public void setContext(String key, Object value) {
237242
// Add entry to the MDC (only works for JSON log format)
238243
ThreadContext.put(key, fromObjectToString(value));
239244
}
240245

246+
@Override
241247
public void write(String message, int logLevel, Object data, boolean stackTrace) {
242248
if (isEnabled(logLevel)) {
243249
if (IS_JSON_FORMAT)
244250
writeJsonFormat(message, logLevel, data, stackTrace);
245251
else
246252
writeTextFormat(message, logLevel, data, stackTrace);
247-
}
253+
}
248254
}
249255

250-
private static final String STACKTRACE_KEY = "stackTrace";
251-
private static final String MESSAGE_KEY = "message";
252-
253256
private void writeTextFormat(String message, int logLevel, Object data, boolean stackTrace) {
254-
String dataKey = "data";
255257
Map<String, Object> mapMessage = new LinkedHashMap<>();
256258

257259
if (data == null || (data instanceof String && "null".equals(data.toString()))) {
258-
mapMessage.put(dataKey, JSONObject.NULL);
260+
mapMessage.put(DATA_KEY, JSONObject.NULL);
259261
} else if (data instanceof String && isJson((String) data)) { // JSON Strings
260-
mapMessage.put(dataKey, jsonStringToMap((String)data));
262+
mapMessage.put(DATA_KEY, jsonStringToMap((String) data));
261263
} else {
262-
mapMessage.put(dataKey, data);
264+
mapMessage.put(DATA_KEY, data);
263265
}
264266

265267
if (stackTrace) {
@@ -272,15 +274,14 @@ private void writeTextFormat(String message, int logLevel, Object data, boolean
272274
}
273275

274276
private void writeJsonFormat(String message, int logLevel, Object data, boolean stackTrace) {
275-
String dataKey = "data";
276277
MapMessage<?, ?> mapMessage = new MapMessage<>().with(MESSAGE_KEY, message);
277278

278279
if (data == null || (data instanceof String && "null".equals(data.toString()))) {
279-
mapMessage.with(dataKey, (Object) null);
280+
mapMessage.with(DATA_KEY, (Object) null);
280281
} else if (data instanceof String && isJson((String) data)) { // JSON Strings
281-
mapMessage.with(dataKey, jsonStringToMap((String)data));
282+
mapMessage.with(DATA_KEY, jsonStringToMap((String) data));
282283
} else {
283-
mapMessage.with(dataKey, data);
284+
mapMessage.with(DATA_KEY, data);
284285
}
285286

286287
if (stackTrace) {
@@ -293,13 +294,20 @@ private void writeJsonFormat(String message, int logLevel, Object data, boolean
293294
private Level getLogLevel(int logLevel) {
294295
LogLevel level = LogLevel.fromInt(logLevel);
295296
switch (level) {
296-
case OFF: return Level.OFF;
297-
case TRACE: return Level.TRACE;
298-
case INFO: return Level.INFO;
299-
case WARN: return Level.WARN;
300-
case ERROR: return Level.ERROR;
301-
case FATAL: return Level.FATAL;
302-
default: return Level.DEBUG;
297+
case OFF:
298+
return Level.OFF;
299+
case TRACE:
300+
return Level.TRACE;
301+
case INFO:
302+
return Level.INFO;
303+
case WARN:
304+
return Level.WARN;
305+
case ERROR:
306+
return Level.ERROR;
307+
case FATAL:
308+
return Level.FATAL;
309+
default:
310+
return Level.DEBUG;
303311
}
304312
}
305313

@@ -320,6 +328,7 @@ private static String fromObjectToString(Object value) {
320328
res = new JSONArray((List<?>) value).toString();
321329
} else {
322330
// Any other object → serialize as JSON
331+
// You never enter here from GX
323332
res = JSONObject.quote(value.toString());
324333
}
325334
return res;

0 commit comments

Comments
 (0)