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
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ guava = { module = "com.google.guava:guava", version.ref = "guava" }
kotlin-logging = { module = "io.github.microutils:kotlin-logging-jvm", version.ref = "kotlin-logging" }

logback-logstash-encoder = { module = "net.logstash.logback:logstash-logback-encoder", version.ref = "logstash" }
logback-length-splitter = { module = "com.latch:logback-length-splitting-appender", version = "0.4.0" }
logback-classic = { module = "ch.qos.logback:logback-classic" }

mockk = { module = "io.mockk:mockk-jvm", version = "1.13.11" }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ dependencies {

// encoder for JSON logging
runtimeOnly(libs.logback.logstash.encoder)
// log splitter
runtimeOnly(libs.logback.length.splitter)

implementation(libs.logback.classic)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.latch;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.LoggingEvent;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
* MIT License
*
* Copyright (c) 2019 Latchable, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
public class LengthSplittingAppender extends SplittingAppenderBase<ILoggingEvent> {

private int maxLength;
private String sequenceKey;

public int getMaxLength() {
return maxLength;
}

public void setMaxLength(int maxLength) {
this.maxLength = maxLength;
}

public String getSequenceKey() {
return sequenceKey;
}

public void setSequenceKey(String sequenceKey) {
this.sequenceKey = sequenceKey;
}

@Override
public boolean shouldSplit(ILoggingEvent event) {
return event.getFormattedMessage().length() > maxLength;
}

@Override
public List<ILoggingEvent> split(ILoggingEvent event) {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
List<String> logMessages = splitString(event.getFormattedMessage(), getMaxLength());

List<ILoggingEvent> splitLogEvents = new ArrayList<>(logMessages.size());
for (int i = 0; i < logMessages.size(); i++) {

LoggingEvent partition = LoggingEventCloner.clone(event, loggerContext);
Map<String, String> seqMDCPropertyMap = new HashMap<>(event.getMDCPropertyMap());
seqMDCPropertyMap.put(getSequenceKey(), Integer.toString(i));
partition.setMDCPropertyMap(seqMDCPropertyMap);
partition.setMessage(logMessages.get(i));

splitLogEvents.add(partition);
}

return splitLogEvents;
}

private List<String> splitString(String str, int chunkSize) {
int fullChunks = str.length() / chunkSize;
int remainder = str.length() % chunkSize;

List<String> results = new ArrayList<>(remainder == 0 ? fullChunks : fullChunks + 1);
for (int i = 0; i < fullChunks; i++) {
results.add(str.substring(i*chunkSize, i*chunkSize + chunkSize));
}
if (remainder != 0) {
results.add(str.substring(str.length() - remainder));
}
return results;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.latch;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.LoggingEvent;
import org.slf4j.Marker;

import java.util.List;

/*
* MIT License
*
* Copyright (c) 2019 Latchable, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
class LoggingEventCloner {

static LoggingEvent clone(ILoggingEvent event, LoggerContext loggerContext) {
LoggingEvent logEventPartition = new LoggingEvent();

logEventPartition.setLevel(event.getLevel());
logEventPartition.setLoggerName(event.getLoggerName());
logEventPartition.setTimeStamp(event.getTimeStamp());
logEventPartition.setLoggerContextRemoteView(event.getLoggerContextVO());
logEventPartition.setLoggerContext(loggerContext);
logEventPartition.setThreadName(event.getThreadName());

List<Marker> eventMarkers = event.getMarkerList();
if (eventMarkers != null && !eventMarkers.isEmpty()) {
logEventPartition.getMarkerList().addAll(eventMarkers);
}

if (event.hasCallerData()) {
logEventPartition.setCallerData(event.getCallerData());
}

return logEventPartition;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.latch;

import ch.qos.logback.core.Appender;
import ch.qos.logback.core.UnsynchronizedAppenderBase;
import ch.qos.logback.core.spi.AppenderAttachable;
import ch.qos.logback.core.spi.AppenderAttachableImpl;

import java.util.Iterator;
import java.util.List;

/*
* MIT License
*
* Copyright (c) 2019 Latchable, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
public abstract class SplittingAppenderBase<E> extends UnsynchronizedAppenderBase<E>
implements AppenderAttachable<E> {

private final AppenderAttachableImpl<E> aai = new AppenderAttachableImpl<>();

protected abstract List<E> split(E event);

protected abstract boolean shouldSplit(E eventObject);

@Override
protected void append(E eventObject) {
if (shouldSplit(eventObject)) {
split(eventObject).forEach(aai::appendLoopOnAppenders);
} else {
aai.appendLoopOnAppenders(eventObject);
}
}

public void addAppender(Appender<E> newAppender) {
addInfo("Attaching appender named [" + newAppender.getName() + "] to SplittingAppender.");
aai.addAppender(newAppender);
}

public Iterator<Appender<E>> iteratorForAppenders() {
return aai.iteratorForAppenders();
}

public Appender<E> getAppender(String name) {
return aai.getAppender(name);
}

public boolean isAttached(Appender<E> eAppender) {
return aai.isAttached(eAppender);
}

public void detachAndStopAllAppenders() {
aai.detachAndStopAllAppenders();
}

public boolean detachAppender(Appender<E> eAppender) {
return aai.detachAppender(eAppender);
}

public boolean detachAppender(String name) {
return aai.detachAppender(name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package com.latch;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.LoggingEvent;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/*
* MIT License
*
* Copyright (c) 2019 Latchable, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
public class LengthSplittingAppenderTest {
private static final int MAX_MESSAGE_LENGTH = 50;
private static final String BASE_STRING = "0123456789";
private static final String LOREM_PATH = "logging_message.txt";

private final LoggerContext loggerContext;
private final LengthSplittingAppender splitter;

public LengthSplittingAppenderTest() {
this.loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
this.splitter = new LengthSplittingAppender();
splitter.setMaxLength(MAX_MESSAGE_LENGTH);
splitter.setSequenceKey("seq");
Assertions.assertEquals(MAX_MESSAGE_LENGTH, splitter.getMaxLength());
}

@Test
public void testEmpty() {
LoggingEvent event = createLoggingEvent("");
Assertions.assertFalse(splitter.shouldSplit(event));
}

@Test
public void testLessThanMax() {
LoggingEvent event = createLoggingEvent(String.join("", Collections.nCopies(1, BASE_STRING)));
Assertions.assertFalse(splitter.shouldSplit(event));
}

@Test
public void testEqualToMax() {
LoggingEvent event = createLoggingEvent(String.join("", Collections.nCopies(5, BASE_STRING)));
Assertions.assertEquals(MAX_MESSAGE_LENGTH, 5 * BASE_STRING.length());
Assertions.assertFalse(splitter.shouldSplit(event));
}

@Test
public void testGreaterThanMaxAndMultipleOfMax() {
LoggingEvent event = createLoggingEvent(String.join("", Collections.nCopies(50, BASE_STRING)));
Assertions.assertTrue(splitter.shouldSplit(event));

List<ILoggingEvent> splitEvents = splitter.split(event);

Assertions.assertEquals(
event.getFormattedMessage().length() / MAX_MESSAGE_LENGTH,
splitEvents.size());
}

@Test
public void testGreaterThanMaxAndNotMultipleOfMax() {
LoggingEvent event = createLoggingEvent(String.join("", Collections.nCopies(51, BASE_STRING)));
Assertions.assertTrue(splitter.shouldSplit(event));

List<ILoggingEvent> splitEvents = splitter.split(event);

Assertions.assertEquals(
event.getFormattedMessage().length() / MAX_MESSAGE_LENGTH + 1,
splitEvents.size());
}

@Test
public void testSplitIntegrity() {
String loremIpsum = readTextFromResource(LOREM_PATH);
LoggingEvent event = createLoggingEvent(loremIpsum);

List<ILoggingEvent> splitEvents = splitter.split(event);

Assertions.assertEquals(event.getFormattedMessage(), recreateMessage(splitEvents));
}

private LoggingEvent createLoggingEvent(String message) {
LoggingEvent event = new LoggingEvent();
event.setMessage(message);
event.setLoggerContext(loggerContext);
return event;
}

private String recreateMessage(List<ILoggingEvent> splitEvents) {
StringBuilder sb = new StringBuilder();

for (ILoggingEvent splitEvent : splitEvents) {
sb.append(splitEvent.getFormattedMessage());
}

return sb.toString();
}

private String readTextFromResource(String fileName) {
InputStream is = getClass().getClassLoader().getResourceAsStream(fileName);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
return reader.lines().collect(Collectors.joining(""));
}
}
Loading
Loading