-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
migrate conversionWord to model based configuration, add MaskedKeyVal…
…uePairConverter Signed-off-by: Ceki Gulcu <[email protected]>
- Loading branch information
Showing
8 changed files
with
324 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
...ack-classic/src/main/java/ch/qos/logback/classic/pattern/MaskedKeyValuePairConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
* Logback: the reliable, generic, fast and flexible logging framework. | ||
* Copyright (C) 1999-2024, QOS.ch. All rights reserved. | ||
* | ||
* This program and the accompanying materials are dual-licensed under | ||
* either the terms of the Eclipse Public License v1.0 as published by | ||
* the Eclipse Foundation | ||
* | ||
* or (per the licensee's choosing) | ||
* | ||
* under the terms of the GNU Lesser General Public License version 2.1 | ||
* as published by the Free Software Foundation. | ||
*/ | ||
|
||
package ch.qos.logback.classic.pattern; | ||
|
||
|
||
import ch.qos.logback.classic.spi.ILoggingEvent; | ||
import ch.qos.logback.core.CoreConstants; | ||
import org.slf4j.event.KeyValuePair; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static ch.qos.logback.classic.pattern.KeyValuePairConverter.*; | ||
|
||
/** | ||
* Similar to {@link KeyValuePairConverter} with the added ability to mask the values of specified keys. | ||
* <p> | ||
* Assuming the specified key is k2, and the kvp list of an event contains {k1, v1}, {k2, v2}, the String output | ||
* will be "k1=v1 k2=XXX", without the quotes. | ||
* | ||
* @author Ceki Gülcü | ||
* @since 1.5.7 | ||
*/ | ||
|
||
|
||
public class MaskedKeyValuePairConverter extends ClassicConverter { | ||
public static final String MASK = "XXX"; | ||
List<String> optionList; | ||
List<String> maskList = new ArrayList<>(); | ||
KeyValuePairConverter.ValueQuoteSpecification valueQuoteSpec = KeyValuePairConverter.ValueQuoteSpecification.DOUBLE; | ||
|
||
public void start() { | ||
this.optionList = getOptionList(); | ||
KeyValuePairConverter.ValueQuoteSpecification extractedSpec = extractSpec(this.optionList); | ||
if (extractedSpec == null) { | ||
maskList = optionList; | ||
} else { | ||
valueQuoteSpec = extractedSpec; | ||
maskList = optionList.subList(1, optionList.size()); | ||
} | ||
|
||
checkMaskListForExtraQuoteSpecs(maskList); | ||
|
||
super.start(); | ||
} | ||
|
||
private void checkMaskListForExtraQuoteSpecs(List<String> maskList) { | ||
if(maskList == null || maskList.isEmpty()) | ||
return; | ||
if(maskList.contains(DOUBLE_OPTION_STR)) { | ||
addWarn("quote spec "+DOUBLE_OPTION_STR+ " found in the wrong order"); | ||
} | ||
if(maskList.contains(SINGLE_OPTION_STR)) { | ||
addWarn("extra quote spec "+SINGLE_OPTION_STR+ " found in the wrong order"); | ||
} | ||
if(maskList.contains(NONE_OPTION_STR)) { | ||
addWarn("extra quote spec "+NONE_OPTION_STR+ " found in the wrong order"); | ||
} | ||
} | ||
|
||
|
||
KeyValuePairConverter.ValueQuoteSpecification extractSpec(List<String> optionList) { | ||
|
||
if (optionList == null || optionList.isEmpty()) { | ||
return null; | ||
} | ||
|
||
String firstOption = optionList.get(0); | ||
|
||
if (DOUBLE_OPTION_STR.equalsIgnoreCase(firstOption)) { | ||
return KeyValuePairConverter.ValueQuoteSpecification.DOUBLE; | ||
} else if (SINGLE_OPTION_STR.equalsIgnoreCase(firstOption)) { | ||
return KeyValuePairConverter.ValueQuoteSpecification.SINGLE; | ||
} else if (NONE_OPTION_STR.equalsIgnoreCase(firstOption)) { | ||
return KeyValuePairConverter.ValueQuoteSpecification.NONE; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
public String convert(ILoggingEvent event) { | ||
|
||
List<KeyValuePair> kvpList = event.getKeyValuePairs(); | ||
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(String.valueOf(kvp.key)); | ||
sb.append('='); | ||
Character quoteChar = valueQuoteSpec.asChar(); | ||
if (quoteChar != null) | ||
sb.append(quoteChar); | ||
if (maskList.contains(kvp.key)) | ||
sb.append(MASK); | ||
else | ||
sb.append(String.valueOf(kvp.value)); | ||
if (quoteChar != null) | ||
sb.append(quoteChar); | ||
} | ||
|
||
return sb.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
...classic/src/test/java/ch/qos/logback/classic/pattern/MaskedKeyValuePairConverterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* Logback: the reliable, generic, fast and flexible logging framework. | ||
* Copyright (C) 1999-2024, QOS.ch. All rights reserved. | ||
* | ||
* This program and the accompanying materials are dual-licensed under | ||
* either the terms of the Eclipse Public License v1.0 as published by | ||
* the Eclipse Foundation | ||
* | ||
* or (per the licensee's choosing) | ||
* | ||
* under the terms of the GNU Lesser General Public License version 2.1 | ||
* as published by the Free Software Foundation. | ||
*/ | ||
|
||
package ch.qos.logback.classic.pattern; | ||
|
||
import ch.qos.logback.classic.Level; | ||
import ch.qos.logback.classic.Logger; | ||
import ch.qos.logback.classic.LoggerContext; | ||
import ch.qos.logback.classic.spi.LoggingEvent; | ||
import ch.qos.logback.core.status.Status; | ||
import ch.qos.logback.core.status.testUtil.StatusChecker; | ||
import ch.qos.logback.core.util.StatusPrinter2; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.slf4j.event.KeyValuePair; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class MaskedKeyValuePairConverterTest { | ||
|
||
LoggerContext lc = new LoggerContext(); | ||
MaskedKeyValuePairConverter converter; | ||
LoggingEvent event; | ||
|
||
StatusChecker statusChecker = new StatusChecker(lc); | ||
StatusPrinter2 statusPrinter2 = new StatusPrinter2(); | ||
|
||
@BeforeEach | ||
public void setUp() throws Exception { | ||
converter = new MaskedKeyValuePairConverter(); | ||
converter.setContext(lc); | ||
} | ||
|
||
@AfterEach | ||
public void tearDown() throws Exception { | ||
lc = null; | ||
converter.stop(); | ||
converter = null; | ||
} | ||
|
||
@Test | ||
public void smoke() { | ||
event = createLoggingEvent(); | ||
converter.setOptionList(List.of("k1")); | ||
converter.start(); | ||
|
||
event.addKeyValuePair(new KeyValuePair("k1", "v1")); | ||
event.addKeyValuePair(new KeyValuePair("k2", "v2")); | ||
|
||
String result = converter.convert(event); | ||
assertEquals("k1=\""+MaskedKeyValuePairConverter.MASK+"\" k2=\"v2\"", result); | ||
} | ||
|
||
@Test | ||
public void smokeSingle() { | ||
event = createLoggingEvent(); | ||
converter.setOptionList(List.of("SINGLE", "k1")); | ||
converter.start(); | ||
|
||
event.addKeyValuePair(new KeyValuePair("k1", "v1")); | ||
event.addKeyValuePair(new KeyValuePair("k2", "v2")); | ||
|
||
String result = converter.convert(event); | ||
assertEquals("k1='"+MaskedKeyValuePairConverter.MASK+"' k2='v2'", result); | ||
} | ||
|
||
@Test | ||
public void wrongOrder() { | ||
event = createLoggingEvent(); | ||
converter.setOptionList(List.of("k1", "SINGLE")); | ||
converter.start(); | ||
|
||
event.addKeyValuePair(new KeyValuePair("k1", "v1")); | ||
event.addKeyValuePair(new KeyValuePair("k2", "v2")); | ||
|
||
statusPrinter2.print(lc); | ||
statusChecker.assertContainsMatch(Status.WARN, "extra quote spec SINGLE found in the wrong order"); | ||
String result = converter.convert(event); | ||
assertEquals("k1=\""+MaskedKeyValuePairConverter.MASK+"\" k2=\"v2\"", result); | ||
} | ||
|
||
@Test | ||
public void testWithOnelKVP() { | ||
event = createLoggingEvent(); | ||
converter.setOptionList(List.of("k")); | ||
converter.start(); | ||
event.addKeyValuePair(new KeyValuePair("k", "v")); | ||
String result = converter.convert(event); | ||
assertEquals("k=\""+MaskedKeyValuePairConverter.MASK+"\"", result); | ||
} | ||
|
||
|
||
|
||
private LoggingEvent createLoggingEvent() { | ||
LoggingEvent le = new LoggingEvent(this.getClass().getName(), lc.getLogger(Logger.ROOT_LOGGER_NAME), | ||
Level.DEBUG, "test message", null, null); | ||
return le; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.