Skip to content

Commit

Permalink
migrate conversionWord to model based configuration, add MaskedKeyVal…
Browse files Browse the repository at this point in the history
…uePairConverter

Signed-off-by: Ceki Gulcu <[email protected]>
  • Loading branch information
ceki committed Aug 9, 2024
1 parent 500203f commit e7d9459
Show file tree
Hide file tree
Showing 8 changed files with 324 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ public class PatternLayout extends PatternLayoutBase<ILoggingEvent> {
DEFAULT_CONVERTER_MAP.put("kvp", KeyValuePairConverter.class.getName());
CONVERTER_CLASS_TO_KEY_MAP.put(KeyValuePairConverter.class.getName(), "kvp");

DEFAULT_CONVERTER_MAP.put("maskedKvp", MaskedKeyValuePairConverter.class.getName());
CONVERTER_CLASS_TO_KEY_MAP.put(MaskedKeyValuePairConverter.class.getName(), "maskedKvp");

DEFAULT_CONVERTER_MAP.put("property", PropertyConverter.class.getName());

DEFAULT_CONVERTER_MAP.put("n", LineSeparatorConverter.class.getName());
Expand Down
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&uuml;lc&uuml;
* @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();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Logback: the reliable, generic, fast and flexible logging framework.
* Copyright (C) 1999-2021, QOS.ch. All rights reserved.
* 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
Expand Down Expand Up @@ -44,6 +44,14 @@ public void tearDown() throws Exception {
converter = null;
}

@Test
public void smoke() {
event.addKeyValuePair(new KeyValuePair("a", "b"));
event.addKeyValuePair(new KeyValuePair("k", "v"));
String result = converter.convert(event);
assertEquals("a=\"b\" k=\"v\"", result);
}

@Test
public void testWithNullKVPList() {
// event.getKeyValuePairs().add(new KeyValuePair("k", "v"));
Expand Down
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -129,30 +129,7 @@ public SaxEventInterpretationContext getInterpretationContext() {

@Override
protected void addModelHandlerAssociations(DefaultProcessor defaultProcessor) {
// code moved to ModelClassToModelHandlerLinkerBase
// the code below is inactive
defaultProcessor.addHandler(ImportModel.class, ImportModelHandler::makeInstance);

defaultProcessor.addHandler(ShutdownHookModel.class, ShutdownHookModelHandler::makeInstance);
defaultProcessor.addHandler(SequenceNumberGeneratorModel.class, SequenceNumberGeneratorModelHandler::makeInstance);

defaultProcessor.addHandler(EventEvaluatorModel.class, EventEvaluatorModelHandler::makeInstance);
defaultProcessor.addHandler(DefineModel.class, DefineModelHandler::makeInstance);
defaultProcessor.addHandler(IncludeModel.class, IncludeModelHandler::makeInstance);


defaultProcessor.addHandler(ParamModel.class, ParamModelHandler::makeInstance);
defaultProcessor.addHandler(PropertyModel.class, PropertyModelHandler::makeInstance);
defaultProcessor.addHandler(TimestampModel.class, TimestampModelHandler::makeInstance);
defaultProcessor.addHandler(StatusListenerModel.class, StatusListenerModelHandler::makeInstance);
defaultProcessor.addHandler(ImplicitModel.class, ImplicitModelHandler::makeInstance);

defaultProcessor.addHandler(IfModel.class, IfModelHandler::makeInstance);
defaultProcessor.addHandler(ThenModel.class, ThenModelHandler::makeInstance);
defaultProcessor.addHandler(ElseModel.class, ElseModelHandler::makeInstance);

defaultProcessor.addHandler(SiftModel.class, SiftModelHandler::makeInstance);

// Please note that code previously here moved to ModelClassToModelHandlerLinkerBase
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,11 @@
package ch.qos.logback.core.joran;

import ch.qos.logback.core.Context;
import ch.qos.logback.core.model.DefineModel;
import ch.qos.logback.core.model.EventEvaluatorModel;
import ch.qos.logback.core.model.ImplicitModel;
import ch.qos.logback.core.model.ImportModel;
import ch.qos.logback.core.model.IncludeModel;
import ch.qos.logback.core.model.ParamModel;
import ch.qos.logback.core.model.PropertyModel;
import ch.qos.logback.core.model.SequenceNumberGeneratorModel;
import ch.qos.logback.core.model.SerializeModelModel;
import ch.qos.logback.core.model.ShutdownHookModel;
import ch.qos.logback.core.model.SiftModel;
import ch.qos.logback.core.model.StatusListenerModel;
import ch.qos.logback.core.model.TimestampModel;
import ch.qos.logback.core.model.*;
import ch.qos.logback.core.model.conditional.ElseModel;
import ch.qos.logback.core.model.conditional.IfModel;
import ch.qos.logback.core.model.conditional.ThenModel;
import ch.qos.logback.core.model.processor.DefaultProcessor;
import ch.qos.logback.core.model.processor.DefineModelHandler;
import ch.qos.logback.core.model.processor.EventEvaluatorModelHandler;
import ch.qos.logback.core.model.processor.ImplicitModelHandler;
import ch.qos.logback.core.model.processor.ImportModelHandler;
import ch.qos.logback.core.model.processor.IncludeModelHandler;
import ch.qos.logback.core.model.processor.NOPModelHandler;
import ch.qos.logback.core.model.processor.PropertyModelHandler;
import ch.qos.logback.core.model.processor.SequenceNumberGeneratorModelHandler;
import ch.qos.logback.core.model.processor.SerializeModelModelHandler;
import ch.qos.logback.core.model.processor.ShutdownHookModelHandler;
import ch.qos.logback.core.model.processor.StatusListenerModelHandler;
import ch.qos.logback.core.model.processor.TimestampModelHandler;
import ch.qos.logback.core.model.processor.*;
import ch.qos.logback.core.model.processor.conditional.ElseModelHandler;
import ch.qos.logback.core.model.processor.conditional.IfModelHandler;
import ch.qos.logback.core.model.processor.conditional.ThenModelHandler;
Expand Down Expand Up @@ -74,6 +50,8 @@ public void link(DefaultProcessor defaultProcessor) {
defaultProcessor.addHandler(SerializeModelModel.class, SerializeModelModelHandler::makeInstance);

defaultProcessor.addHandler(EventEvaluatorModel.class, EventEvaluatorModelHandler::makeInstance);
defaultProcessor.addHandler(ConversionRuleModel.class, ConversionRuleModelHandler::makeInstance);

defaultProcessor.addHandler(DefineModel.class, DefineModelHandler::makeInstance);
defaultProcessor.addHandler(IncludeModel.class, IncludeModelHandler::makeInstance);

Expand Down
Loading

0 comments on commit e7d9459

Please sign in to comment.