diff --git a/docs/guide/censor.md b/docs/guide/censor.md
index 019bff36..8a3a1d1c 100644
--- a/docs/guide/censor.md
+++ b/docs/guide/censor.md
@@ -62,7 +62,8 @@ If you are working with a componentized framework, you'll want to use the `censo
Note that this does not filter out marker text or additional information related to the event, * i.e. it does not filter out exception text. * + * Note also that the censor converter only picks out one censor from the list. + * *
{@code *diff --git a/logback-censor/src/main/java/com/tersesystems/logback/censor/CensorRefAction.java b/logback-censor/src/main/java/com/tersesystems/logback/censor/CensorRefAction.java index f2fb7eca..70f77f7f 100644 --- a/logback-censor/src/main/java/com/tersesystems/logback/censor/CensorRefAction.java +++ b/logback-censor/src/main/java/com/tersesystems/logback/censor/CensorRefAction.java @@ -72,7 +72,7 @@ public void begin(InterpretationContext ec, String tagName, Attributes attribute + censorAttachable + "at " + getLineNumber(ec)); - censorAttachable.setCensor(censor); + censorAttachable.addCensor(censor); } public void end(InterpretationContext ec, String n) {} diff --git a/logback-censor/src/main/java/com/tersesystems/logback/censor/CensoringJsonGeneratorDecorator.java b/logback-censor/src/main/java/com/tersesystems/logback/censor/CensoringJsonGeneratorDecorator.java index 0bc570fa..5b1a647f 100644 --- a/logback-censor/src/main/java/com/tersesystems/logback/censor/CensoringJsonGeneratorDecorator.java +++ b/logback-censor/src/main/java/com/tersesystems/logback/censor/CensoringJsonGeneratorDecorator.java @@ -27,7 +27,7 @@ public class CensoringJsonGeneratorDecorator extends ContextAwareBase implements CensorAttachable, JsonGeneratorDecorator, LifeCycle { - private CensorContextAware censor; + private final List censors = new ArrayList<>(); private boolean started; @Override @@ -38,13 +38,12 @@ public JsonGenerator decorate(JsonGenerator generator) { substitutionDelegate, new CensoringTokenFilter(), true, true); } - public CensorContextAware getCensor() { - return censor; + public List getCensors() { + return censors; } - @Override - public void setCensor(CensorContextAware censor) { - this.censor = censor; + public void addCensor(CensorContextAware censor) { + this.censors.add(censor); } @Override @@ -55,6 +54,7 @@ public void start() { @Override public void stop() { filterKeys.clear(); + censors.clear(); started = false; } @@ -63,7 +63,7 @@ public boolean isStarted() { return started; } - private List filterKeys = new ArrayList<>(); + private final List filterKeys = new ArrayList<>(); public void addFilterKey(String filterKey) { this.filterKeys.add(filterKey); @@ -85,7 +85,7 @@ public TokenFilter includeProperty(String name) { } private boolean shouldFilter(String name) { - return filterKeys != null && filterKeys.contains(name); + return filterKeys.contains(name); } @Override @@ -101,11 +101,13 @@ public CensoringJsonGeneratorDelegate(JsonGenerator d) { } private String censorSensitiveMessage(String original) { - if (censor != null) { - return String.valueOf(censor.censorText(original)); - } else { - return original; + String value = original; + final List censors = getCensors(); + for (CensorContextAware censor : censors) { + value = String.valueOf(censor.censorText(value)); } + + return value; } @Override diff --git a/logback-censor/src/test/java/com/tersesystems/logback/censor/CensorActionTest.java b/logback-censor/src/test/java/com/tersesystems/logback/censor/CensorActionTest.java index 9f50bc7f..b4aa5931 100644 --- a/logback-censor/src/test/java/com/tersesystems/logback/censor/CensorActionTest.java +++ b/logback-censor/src/test/java/com/tersesystems/logback/censor/CensorActionTest.java @@ -61,8 +61,8 @@ public void testJsonTest3() throws JoranException { ch.qos.logback.classic.Logger root = loggerContext.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME); TestAppender test = (TestAppender) root.getAppender("TEST3"); assertThat(test).isNotNull(); - byte[] bytes = test.getEncoder().encode(createLoggingEvent(root, "hunter3")); - assertThat(new String(bytes, StandardCharsets.UTF_8)).contains("\"message\":\"[CENSOR3]\""); + byte[] bytes = test.getEncoder().encode(createLoggingEvent(root, "hunter3 hunter4")); + assertThat(new String(bytes, StandardCharsets.UTF_8)).contains("\"message\":\"[CENSOR3] [CENSOR4]\""); } @Test diff --git a/logback-censor/src/test/java/com/tersesystems/logback/censor/CensoringJsonGeneratorDecoratorTest.java b/logback-censor/src/test/java/com/tersesystems/logback/censor/CensoringJsonGeneratorDecoratorTest.java index c8b9b37f..f15affed 100644 --- a/logback-censor/src/test/java/com/tersesystems/logback/censor/CensoringJsonGeneratorDecoratorTest.java +++ b/logback-censor/src/test/java/com/tersesystems/logback/censor/CensoringJsonGeneratorDecoratorTest.java @@ -24,15 +24,23 @@ public class CensoringJsonGeneratorDecoratorTest { @Test public void basicCensor() throws Exception { LoggerContext context = new LoggerContext(); - RegexCensor censor = new RegexCensor(); - censor.setContext(context); - censor.setReplacementText("*******"); - censor.setRegex("hunter2"); - censor.start(); + RegexCensor censor1 = new RegexCensor(); + censor1.setContext(context); + censor1.setReplacementText("*******"); + censor1.setRegex("hunter2"); + censor1.start(); + + RegexCensor censor2 = new RegexCensor(); + censor2.setContext(context); + censor2.setReplacementText("!!!!!!"); + censor2.setRegex("message"); + censor2.start(); + CensoringJsonGeneratorDecorator decorator = new CensoringJsonGeneratorDecorator(); decorator.setContext(context); - decorator.setCensor(censor); + decorator.addCensor(censor1); + decorator.addCensor(censor2); decorator.start(); StringWriter writer = new StringWriter(); @@ -44,7 +52,7 @@ public void basicCensor() throws Exception { generator.writeEndObject(); generator.flush(); - assertThat(writer.toString()).isEqualTo("{\"message\":\"My ******* message\"}"); + assertThat(writer.toString()).isEqualTo("{\"message\":\"My ******* !!!!!!\"}"); } @Test @@ -76,7 +84,7 @@ public void prettyPrintCensor() throws Exception { CensoringJsonGeneratorDecorator decorator = new CensoringPrettyPrintingJsonGeneratorDecorator(); decorator.setContext(context); - decorator.setCensor(censor); + decorator.addCensor(censor); decorator.start(); StringWriter writer = new StringWriter(); diff --git a/logback-censor/src/test/resources/test3.xml b/logback-censor/src/test/resources/test3.xml index 6c553b74..3ddc4f5e 100644 --- a/logback-censor/src/test/resources/test3.xml +++ b/logback-censor/src/test/resources/test3.xml @@ -18,15 +18,21 @@ - + + hunter3 [CENSOR3] + +hunter4 +[CENSOR4] +- + +