Skip to content

Commit

Permalink
more work on KVP
Browse files Browse the repository at this point in the history
Signed-off-by: Ceki Gulcu <ceki@qos.ch>
  • Loading branch information
ceki committed Aug 10, 2021
1 parent e2d51e9 commit ede0435
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 17 deletions.
Expand Up @@ -54,6 +54,7 @@ public void start() {
TimeZone tz = TimeZone.getTimeZone((String) optionList.get(1));
cachingDateFormatter.setTimeZone(tz);
}
super.start();
}

public String convert(ILoggingEvent le) {
Expand Down
Expand Up @@ -6,12 +6,13 @@

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.CoreConstants;
import ch.qos.logback.core.util.OptionHelper;

/**
* Convert the contents of {@link KeyValuePair} list to a String.
*
* Assuming the list contains the list {k1, v1}, {k2, v2}, the String
* output will be "k1=v1 k2=v2", without the quotes.
* Assuming the list contains the list {k1, v1}, {k2, v2}, the String output
* will be "k1=v1 k2=v2", without the quotes.
*
*
* @since 1.3.0
Expand All @@ -20,24 +21,70 @@
*/
public class KeyValuePairConverter extends ClassicConverter {

static final String DOUBLE_OPTION_STR = "DOUBLE";
static final String SINGLE_OPTION_STR = "SINGLE";
static final String NONE_OPTION_STR = "NONE";

enum ValueQuoteSpecification {
NONE, SINGLE, DOUBLE;

Character asChar() {
switch (this) {
case NONE:
return null;
case DOUBLE:
return '"';
case SINGLE:
return '\'';
default:
throw new IllegalStateException();
}
}
}

ValueQuoteSpecification valueQuoteSpec = ValueQuoteSpecification.DOUBLE;

public void start() {
String optStr = getFirstOption();
valueQuoteSpec = optionStrToSpec(optStr);
super.start();
}

private ValueQuoteSpecification optionStrToSpec(String optStr) {
if (optStr == null)
return ValueQuoteSpecification.DOUBLE;
if (DOUBLE_OPTION_STR.equalsIgnoreCase(optStr))
return ValueQuoteSpecification.DOUBLE;
if (SINGLE_OPTION_STR.equalsIgnoreCase(optStr))
return ValueQuoteSpecification.SINGLE;
if (NONE_OPTION_STR.equalsIgnoreCase(optStr))
return ValueQuoteSpecification.NONE;
return ValueQuoteSpecification.DOUBLE;
}

@Override
public String convert(ILoggingEvent event) {

List<KeyValuePair> kvpList = event.getKeyValuePairs();
if(kvpList == null || kvpList.isEmpty()) {
if (kvpList == null || kvpList.isEmpty()) {
return CoreConstants.EMPTY_STRING;
}

StringBuilder sb = new StringBuilder();
for (int i = 0; i < kvpList.size(); i++) {
KeyValuePair kvp = kvpList.get(i);
if(i != 0)
sb.append(' ');
sb.append(kvp.key);
sb.append('=');
sb.append(kvp.value);
}

for (int i = 0; i < kvpList.size(); i++) {
KeyValuePair kvp = kvpList.get(i);
if (i != 0)
sb.append(' ');
sb.append(String.valueOf(kvp.key));
sb.append('=');
Character c = valueQuoteSpec.asChar();
if (c != null)
sb.append(c);
sb.append(String.valueOf(kvp.value));
if (c != null)
sb.append(c);
}

return sb.toString();
}

Expand Down
Expand Up @@ -98,6 +98,7 @@ public void start() {
addError("failed to parse integer string [" + optStr + "]", nfe);
}
}
super.start();
}

public String convert(ILoggingEvent event) {
Expand Down
Expand Up @@ -53,9 +53,9 @@ protected void log(org.slf4j.event.LoggingEvent sle) {
lle.setKeyValuePairs(sle.getKeyValuePairs());


// Note that at this point, any calls makde with a logger disabled
// for a given level will be already filtered out. TurboFilters cannot
// prevent that.
// Note that at this point, any calls made with a logger disabled
// for a given level, will be already filtered out/in. TurboFilters cannot
// act at this point in the process.
logbackLogger.callAppenders(lle);
}

Expand Down
12 changes: 12 additions & 0 deletions logback-classic/src/test/input/joran/pattern/kvp.xml
@@ -0,0 +1,12 @@
<configuration>

<appender name="LIST" class="ch.qos.logback.core.testUtil.StringListAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%kvp %msg</Pattern>
</layout>
</appender>

<root level="debug">
<appender-ref ref="LIST"/>
</root>
</configuration>
Expand Up @@ -26,6 +26,7 @@
import org.junit.Ignore;
import org.junit.Test;
import org.slf4j.MDC;
import org.slf4j.event.KeyValuePair;

import ch.qos.logback.classic.AsyncAppender;
import ch.qos.logback.classic.ClassicTestConstants;
Expand Down Expand Up @@ -511,6 +512,34 @@ public void asynAppenderListAfter() throws JoranException {
assertTrue(asyncAppender.isStarted());
}

@Test
public void kvp() throws JoranException {
configure(ClassicTestConstants.JORAN_INPUT_PREFIX + "pattern/kvp.xml");

String msg = "hello kvp";

KeyValuePair kvp1 = new KeyValuePair("k"+diff, "v"+diff);
KeyValuePair kvp2 = new KeyValuePair("k"+(diff+1), "v"+(diff+1));
KeyValuePair kvpNullKey = new KeyValuePair(null, "v"+(diff+2));
KeyValuePair kvpNullValue = new KeyValuePair("k"+(diff+3), null);

logger.atDebug().addKeyValue(kvp1.key, kvp1.value).log(msg);
logger.atDebug().addKeyValue(kvp2.key, kvp2.value).log(msg);
logger.atDebug().addKeyValue(kvpNullKey.key, kvpNullKey.value).log(msg);
logger.atDebug().addKeyValue(kvpNullValue.key, kvpNullValue.value).log(msg);


StringListAppender<ILoggingEvent> slAppender = (StringListAppender<ILoggingEvent>) loggerContext
.getLogger("root").getAppender("LIST");
assertNotNull(slAppender);
assertEquals(4, slAppender.strList.size());
assertTrue(slAppender.strList.get(0).contains(kvp1.key+ "=\""+kvp1.value+"\" "+msg));
assertTrue(slAppender.strList.get(1).contains(kvp2.key+ "=\""+kvp2.value+"\" "+msg));
assertTrue(slAppender.strList.get(2).contains("null=\""+kvpNullKey.value+"\" "+msg));
assertTrue(slAppender.strList.get(3).contains(kvpNullValue.key+ "=\"null\" " +msg));
}


// @Test
// public void doTest() throws JoranException {
// int LIMIT = 0;
Expand Down

0 comments on commit ede0435

Please sign in to comment.