Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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());
}
Expand All @@ -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());
}
Expand All @@ -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);
}
}