Skip to content

Commit

Permalink
Skip logging of exceptions if the limit has been reached.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhafner committed Mar 30, 2024
1 parent 8a7df00 commit e7876b0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
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

0 comments on commit e7876b0

Please sign in to comment.