Skip to content
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

Skip logging of exceptions if the limit has been reached #970

Merged
merged 1 commit into from
Mar 30, 2024
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
4 changes: 3 additions & 1 deletion src/main/java/edu/hm/hafner/util/FilteredLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ public void logException(final Exception exception, final String format, final O

lock.lock();
try {
errorMessages.addAll(Arrays.asList(ExceptionUtils.getRootCauseStackTrace(exception)));
if (lines <= maxLines) {
errorMessages.addAll(Arrays.asList(ExceptionUtils.getRootCauseStackTrace(exception)));
}
}
finally {
lock.unlock();
Expand Down
34 changes: 24 additions & 10 deletions src/test/java/edu/hm/hafner/util/FilteredLogTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,28 +51,42 @@ void shouldLogAllErrors() {
void shouldSkipAdditionalErrors() {
FilteredLog filteredLog = create5ErrorsLogWithTitle(StringUtils.EMPTY);

assertThat(filteredLog).hasOnlyErrorMessages("1", "2", "3", "4", "5",
" ... skipped logging of 2 additional errors ...");
verifyFiveErrorMessages(filteredLog);
}

@Test
void shouldSkipAdditionalErrorsWithTitle() {
FilteredLog filteredLog = create5ErrorsLogWithTitle(TITLE);

assertThat(filteredLog).hasOnlyErrorMessages(TITLE, "1", "2", "3", "4", "5",
assertThat(filteredLog).hasErrorMessages(TITLE);

verifyFiveErrorMessages(filteredLog);
}

private void verifyFiveErrorMessages(final FilteredLog filteredLog) {
assertThat(filteredLog).hasErrorMessages(
"1", "2", "3", "4", "5",
"java.lang.IllegalStateException: 1",
"java.lang.IllegalStateException: 2",
"java.lang.IllegalStateException: 3",
"java.lang.IllegalStateException: 4",
"java.lang.IllegalStateException: 5",
" ... skipped logging of 2 additional errors ...");

assertThat(filteredLog).doesNotHaveErrorMessages(
"6",
"java.lang.IllegalStateException: 6",
"7",
"java.lang.IllegalStateException: 7");
}

private FilteredLog create5ErrorsLogWithTitle(final String title) {
var filteredLog = new FilteredLog(title, 5);

filteredLog.logError("1");
filteredLog.logError("2");
filteredLog.logError("3");
filteredLog.logError("4");
filteredLog.logError("5");
filteredLog.logError("6");
filteredLog.logError("7");
for (int i = 1; i < 8; i++) {
var number = String.valueOf(i);
filteredLog.logException(new IllegalStateException(number), number);
}

assertThat(filteredLog.size()).isEqualTo(7);

Expand Down