-
Notifications
You must be signed in to change notification settings - Fork 16
Fix logback incompatibility issues and copy com.latch.LengthSplittingAppender into project (#143) #144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Fix logback incompatibility issues and copy com.latch.LengthSplittingAppender into project (#143) #144
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
23a3fb3
add test for long message with JsonSplitter
JohannesRiegler 7c704b5
Fix logback incompatibility issues and copy com.latch.LengthSplitting…
JohannesRiegler f0fbd17
Add license information, use version catalog
JohannesRiegler 4196753
fix tests
JohannesRiegler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
...latform-spring-logging-server-config/src/main/java/com/latch/LengthSplittingAppender.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } | ||
56 changes: 56 additions & 0 deletions
56
...bom/platform-spring-logging-server-config/src/main/java/com/latch/LoggingEventCloner.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { | ||
JohannesRiegler marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| 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; | ||
| } | ||
| } | ||
80 changes: 80 additions & 0 deletions
80
.../platform-spring-logging-server-config/src/main/java/com/latch/SplittingAppenderBase.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> | ||
JohannesRiegler marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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); | ||
| } | ||
| } | ||
131 changes: 131 additions & 0 deletions
131
...orm-spring-logging-server-config/src/test/java/com/latch/LengthSplittingAppenderTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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("")); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.