diff --git a/src/main/java/net/logstash/logback/composite/loggingevent/LoggerNameJsonProvider.java b/src/main/java/net/logstash/logback/composite/loggingevent/LoggerNameJsonProvider.java index 6191a864..d299aa60 100644 --- a/src/main/java/net/logstash/logback/composite/loggingevent/LoggerNameJsonProvider.java +++ b/src/main/java/net/logstash/logback/composite/loggingevent/LoggerNameJsonProvider.java @@ -25,6 +25,7 @@ import net.logstash.logback.fieldnames.LogstashFieldNames; import ch.qos.logback.classic.pattern.Abbreviator; +import ch.qos.logback.classic.pattern.ClassNameOnlyAbbreviator; import ch.qos.logback.classic.pattern.TargetLengthBasedClassNameAbbreviator; import ch.qos.logback.classic.spi.ILoggingEvent; import com.fasterxml.jackson.core.JsonGenerator; @@ -63,11 +64,33 @@ public int getShortenedLoggerNameLength() { public void setShortenedLoggerNameLength(int length) { this.shortenedLoggerNameLength = length; - if (length >= 0) { - abbreviator = new CachingAbbreviator(new TargetLengthBasedClassNameAbbreviator(this.shortenedLoggerNameLength)); - } else { - abbreviator = NullAbbreviator.INSTANCE; - } } + @Override + public void start() { + this.abbreviator = createAbbreviator(); + super.start(); + } + + @Override + public void stop() { + super.stop(); + this.abbreviator = null; + } + + protected Abbreviator createAbbreviator() { + if (this.shortenedLoggerNameLength < 0) { + return NullAbbreviator.INSTANCE; + } + + Abbreviator abbreviator; + if (this.shortenedLoggerNameLength == 0) { + abbreviator = new ClassNameOnlyAbbreviator(); + } + else { + abbreviator = new TargetLengthBasedClassNameAbbreviator(this.shortenedLoggerNameLength); + } + + return new CachingAbbreviator(abbreviator); + } } diff --git a/src/test/java/net/logstash/logback/composite/loggingevent/LoggerNameJsonProviderTest.java b/src/test/java/net/logstash/logback/composite/loggingevent/LoggerNameJsonProviderTest.java index 441d7d76..47bb6f82 100644 --- a/src/test/java/net/logstash/logback/composite/loggingevent/LoggerNameJsonProviderTest.java +++ b/src/test/java/net/logstash/logback/composite/loggingevent/LoggerNameJsonProviderTest.java @@ -42,10 +42,9 @@ public class LoggerNameJsonProviderTest { @Test public void testFullName() throws IOException { - when(event.getLoggerName()).thenReturn(getClass().getName()); - provider.writeTo(generator, event); + writeEvent(); verify(generator).writeStringField(LoggerNameJsonProvider.FIELD_LOGGER_NAME, getClass().getName()); } @@ -56,7 +55,7 @@ public void testFieldName() throws IOException { when(event.getLoggerName()).thenReturn(getClass().getName()); - provider.writeTo(generator, event); + writeEvent(); verify(generator).writeStringField("newFieldName", getClass().getName()); } @@ -70,7 +69,7 @@ public void testFieldNames() throws IOException { when(event.getLoggerName()).thenReturn(getClass().getName()); - provider.writeTo(generator, event); + writeEvent(); verify(generator).writeStringField("newFieldName", getClass().getName()); } @@ -81,9 +80,27 @@ public void testShortName() throws IOException { when(event.getLoggerName()).thenReturn(getClass().getName()); - provider.writeTo(generator, event); + writeEvent(); verify(generator).writeStringField(LoggerNameJsonProvider.FIELD_LOGGER_NAME, "n.l.l.c.l.LoggerNameJsonProviderTest"); } + @Test + public void testShortName_zeroLength() throws IOException { + provider.setShortenedLoggerNameLength(0); + + when(event.getLoggerName()).thenReturn(getClass().getName()); + + writeEvent(); + + verify(generator).writeStringField(LoggerNameJsonProvider.FIELD_LOGGER_NAME, "LoggerNameJsonProviderTest"); + } + + protected void writeEvent() throws IOException { + if (!provider.isStarted()) { + provider.start(); + } + + provider.writeTo(generator, event); + } }