From 005d4a7a3d31e09f04b7ff909fee38afcd203d6a Mon Sep 17 00:00:00 2001 From: Bart Vercammen Date: Fri, 26 Jul 2019 13:59:45 +0200 Subject: [PATCH] update log fields --- pom.xml | 2 +- .../java/com/batch/escalog/LogFmtLayout.java | 93 +++++-------------- .../com/batch/escalog/LogFmtLayoutTest.java | 13 ++- .../java/com/batch/escalog/LogFmtTest.java | 6 +- 4 files changed, 33 insertions(+), 81 deletions(-) diff --git a/pom.xml b/pom.xml index bc11794..aeab9c1 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.batch.escalog escalog - 1.1.2 + 1.1.3 UTF-8 diff --git a/src/main/java/com/batch/escalog/LogFmtLayout.java b/src/main/java/com/batch/escalog/LogFmtLayout.java index cfa0d3c..78a56fa 100644 --- a/src/main/java/com/batch/escalog/LogFmtLayout.java +++ b/src/main/java/com/batch/escalog/LogFmtLayout.java @@ -20,7 +20,7 @@ public class LogFmtLayout extends LayoutBase private static final String LOGFMT_CLASS = com.batch.escalog.LogFmt.class.getName(); private static final String LOGFMTBUILDER_CLASS = com.batch.escalog.LogFmtBuilder.class.getName(); - private static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss"; + private static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'"; // -----------------------------------> @@ -61,7 +61,13 @@ public class LogFmtLayout extends LayoutBase /** * Formats the time field */ - private ThreadLocal simpleDateFormat = ThreadLocal.withInitial(() -> new SimpleDateFormat(timeFormat != null ? timeFormat : DATE_FORMAT)); + private SimpleDateFormat newDf(String strformat) { + SimpleDateFormat sdf = new SimpleDateFormat(strformat); + sdf.setTimeZone(TimeZone.getTimeZone("UTC")); + return sdf; + } + + private ThreadLocal simpleDateFormat = ThreadLocal.withInitial(() -> newDf(timeFormat != null ? timeFormat : DATE_FORMAT)); @@ -70,16 +76,12 @@ public LogFmtLayout() appenders.put(TIME.toString(), this::timeAppender); appenders.put(LEVEL.toString(), this::levelAppender); appenders.put(MESSAGE.toString(), this::msgAppender); - appenders.put(THREAD.toString(), this::threadAppender); - appenders.put("package", this::packageAppender); - appenders.put("module", this::moduleAppender); - appenders.put("mdc", this::mdcAppender); - appenders.put("custom", this::customFieldsAppender); - appenders.put(ERROR.toString(), this::errorAppender); + appenders.put(TNAME.toString(), this::threadAppender); + appenders.put(EXCEPTION.toString(), this::errorAppender); + appenders.put(LOGGER.toString(), this::classAppender); this.defaultAppenders = new ArrayList<>(Arrays.asList( - this::timeAppender, this::levelAppender,this::threadAppender, this::packageAppender, this::moduleAppender, - this::msgAppender, this::mdcAppender, this::customFieldsAppender, this::errorAppender + this::timeAppender, this::levelAppender,this::threadAppender, this::classAppender, this::msgAppender, this::errorAppender )); } @@ -133,7 +135,7 @@ public String doLayout(ILoggingEvent iLoggingEvent) // app_name if ( appName != null ) { - appendKeyValueAndEscape(sb, APP.toString(), appName); + appendKeyValueAndEscape(sb, PNAME.toString(), appName); } for ( KeyValueAppender keyValueAppender : customAppenders != null ? customAppenders : defaultAppenders ) @@ -160,7 +162,7 @@ private void timeAppender(StringBuilder sb, ILoggingEvent iLoggingEvent) private void threadAppender(StringBuilder sb, ILoggingEvent iLoggingEvent) { - appendKeyValueAndEscape(sb, THREAD.toString(), iLoggingEvent.getThreadName()); + appendKeyValueAndEscape(sb, TNAME.toString(), iLoggingEvent.getThreadName()); } private void msgAppender(StringBuilder sb, ILoggingEvent iLoggingEvent) @@ -168,65 +170,20 @@ private void msgAppender(StringBuilder sb, ILoggingEvent iLoggingEvent) appendKeyValueAndEscape(sb, MESSAGE.toString(), iLoggingEvent.getFormattedMessage()); } - private void mdcAppender(StringBuilder sb, ILoggingEvent iLoggingEvent) - { - Map mdc = iLoggingEvent.getMDCPropertyMap(); - if ( mdc != null ) - { - mdc.forEach((k, v) -> - { - if ( !isNativeKey(k) ) - { - appendKeyValueAndEscape(sb, k, v); - } - }); - } - } - - private void customFieldsAppender(StringBuilder sb, ILoggingEvent iLoggingEvent) - { - Marker marker = iLoggingEvent.getMarker(); - if ( marker != null && marker instanceof LogFmtMarker ) - { - LogFmtMarker keyValueMarker = (LogFmtMarker) marker; - keyValueMarker.forEach((k, v) -> - { - if ( !isNativeKey(k) ) - { - appendKeyValueAndEscape(sb, k, v); - } - }); - } - } - private void errorAppender(StringBuilder sb, ILoggingEvent iLoggingEvent) { if ( iLoggingEvent.getThrowableProxy() != null ) { - appendKeyValueAndEscape(sb, ERROR.toString(), ThrowableProxyUtil.asString(iLoggingEvent.getThrowableProxy())); - } - } - - private void packageAppender(StringBuilder sb, ILoggingEvent iLoggingEvent) - { - String className = getLastClassName(iLoggingEvent.getCallerData()); - if ( className != null ) - { - int lastPointPosition = className.lastIndexOf('.'); - String pkg = lastPointPosition >= 0 ? className.substring(0, lastPointPosition) : ""; - appendKeyValueAndEscape(sb, PACKAGE.toString(), pkg); + appendKeyValueAndEscape(sb, EXCEPTION.toString(), ThrowableProxyUtil.asString(iLoggingEvent.getThrowableProxy())); } - } - private void moduleAppender(StringBuilder sb, ILoggingEvent iLoggingEvent) + private void classAppender(StringBuilder sb, ILoggingEvent iLoggingEvent) { String className = getLastClassName(iLoggingEvent.getCallerData()); if ( className != null ) { - int lastPointPosition = className.lastIndexOf('.'); - String module = lastPointPosition >= 0 ? className.substring(lastPointPosition + 1, className.length()) : className; - appendKeyValueAndEscape(sb, MODULE.toString(), module); + appendKeyValueAndEscape(sb, LOGGER.toString(), className); } } @@ -338,11 +295,6 @@ public static StringBuilder escapeValue(String string) private static String formatLogLevel(Level level) { - if ( level == Level.WARN ) - { - return "warning"; - } - return level.toString().toLowerCase(); } @@ -355,11 +307,12 @@ enum NativeKey TIME("time"), LEVEL("level"), MESSAGE("msg"), - APP("app"), - THREAD("thread"), - PACKAGE("package"), - MODULE("module"), - ERROR("error"); + PNAME("pname"), + TNAME("tname"), + LOGGER("logger"), + PID("pid"), + TID("tid"), + EXCEPTION("exception"); // -----------------------------------> diff --git a/src/test/java/com/batch/escalog/LogFmtLayoutTest.java b/src/test/java/com/batch/escalog/LogFmtLayoutTest.java index 2cdad86..78dfadb 100644 --- a/src/test/java/com/batch/escalog/LogFmtLayoutTest.java +++ b/src/test/java/com/batch/escalog/LogFmtLayoutTest.java @@ -8,10 +8,8 @@ import org.slf4j.Marker; import java.text.SimpleDateFormat; -import java.util.Calendar; -import java.util.Date; -import java.util.HashMap; -import java.util.Map; +import java.time.ZoneId; +import java.util.*; import static com.batch.escalog.LogFmtLayout.escapeValue; import static com.batch.escalog.LogFmtMarker.with; @@ -27,7 +25,7 @@ public class LogFmtLayoutTest @Test public void escapeTest() { - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); System.out.println(sdf.format(new Date())); assertEquals("the \\\"message\\\"", escapeValue("the \"message\"").toString()); assertEquals("the \\n carriage \\n return", escapeValue("the \n carriage \n return").toString()); @@ -48,13 +46,14 @@ public void logFmtLayoutTest() logFmtLayout.setPrefix(prefix); Calendar calendar = Calendar.getInstance(); + calendar.setTimeZone(TimeZone.getTimeZone("UTC")); calendar.set(2017, Calendar.NOVEMBER, 30, 15, 10, 25); ILoggingEvent loggingEvent = createLoggingEvent("thread0", Level.DEBUG, calendar.getTime(), with("key1", "value1").and("key2", "val ue2"), "message with \"double quotes\"", null); assertEquals( - "prefix=\"prefix\" app=escalog time=\"2017-11-30T15:10:25\" level=debug thread=thread0 msg=\"message with \\\"double quotes\\\"\" key1=value1 key2=\"val ue2\"\n", + "prefix=\"prefix\" pname=escalog time=\"2017-11-30T15:10:25Z\" level=debug tname=thread0 msg=\"message with \\\"double quotes\\\"\"\n", logFmtLayout.doLayout(loggingEvent) ); @@ -77,7 +76,7 @@ public void fieldsConfigTest() with("key1", "value1").and("key2", "val ue2"), "message with \"double quotes\"", mdc); assertEquals( - "time=2017 mdckey=\"mdc value\" key1=value1 key2=\"val ue2\" level=debug msg=\"message with \\\"double quotes\\\"\"\n", + "time=2017 level=debug msg=\"message with \\\"double quotes\\\"\"\n", logFmtLayout.doLayout(loggingEvent) ); } diff --git a/src/test/java/com/batch/escalog/LogFmtTest.java b/src/test/java/com/batch/escalog/LogFmtTest.java index 45328b6..509662b 100644 --- a/src/test/java/com/batch/escalog/LogFmtTest.java +++ b/src/test/java/com/batch/escalog/LogFmtTest.java @@ -50,15 +50,15 @@ public void logOutputTest() { logger.with("key1", "value1").info("here is the message"); String line = getEndLog(layoutProducer.consume()); - assertEquals("msg=\"here is the message\" key1=value1\n", line); + assertEquals("msg=\"here is the message\"\n", line); logger.with("key with spaces", "value with spaces").info("message with sequences : \n \r \\"); line = getEndLog(layoutProducer.consume()); - assertEquals("msg=\"message with sequences : \\n \\r \\\\\" key with spaces=\"value with spaces\"\n", line); + assertEquals("msg=\"message with sequences : \\n \\r \\\\\"\n", line); logger.with("key1", "value1").and("keyInt", 143).and("keyBool", true).info("message-without-spaces"); line = getEndLog(layoutProducer.consume()); - assertEquals("msg=message-without-spaces key1=value1 keyInt=143 keyBool=true\n", line); + assertEquals("msg=message-without-spaces\n", line); }