Skip to content

Commit

Permalink
update log fields
Browse files Browse the repository at this point in the history
  • Loading branch information
duke-bartholomew committed Jul 26, 2019
1 parent bfdb36d commit 005d4a7
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 81 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.batch.escalog</groupId>
<artifactId>escalog</artifactId>
<version>1.1.2</version>
<version>1.1.3</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
93 changes: 23 additions & 70 deletions src/main/java/com/batch/escalog/LogFmtLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class LogFmtLayout extends LayoutBase<ILoggingEvent>
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'";


// ----------------------------------->
Expand Down Expand Up @@ -61,7 +61,13 @@ public class LogFmtLayout extends LayoutBase<ILoggingEvent>
/**
* Formats the time field
*/
private ThreadLocal<SimpleDateFormat> 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> simpleDateFormat = ThreadLocal.withInitial(() -> newDf(timeFormat != null ? timeFormat : DATE_FORMAT));



Expand All @@ -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
));
}

Expand Down Expand Up @@ -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 )
Expand All @@ -160,73 +162,28 @@ 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)
{
appendKeyValueAndEscape(sb, MESSAGE.toString(), iLoggingEvent.getFormattedMessage());
}

private void mdcAppender(StringBuilder sb, ILoggingEvent iLoggingEvent)
{
Map<String, String> 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);
}

}
Expand Down Expand Up @@ -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();
}

Expand All @@ -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");

// ----------------------------------->

Expand Down
13 changes: 6 additions & 7 deletions src/test/java/com/batch/escalog/LogFmtLayoutTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
Expand All @@ -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)
);

Expand All @@ -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)
);
}
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/com/batch/escalog/LogFmtTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}

Expand Down

0 comments on commit 005d4a7

Please sign in to comment.