diff --git a/src/main/java/edu/hm/hafner/util/FilteredLog.java b/src/main/java/edu/hm/hafner/util/FilteredLog.java index 6dece00b..8173c53a 100644 --- a/src/main/java/edu/hm/hafner/util/FilteredLog.java +++ b/src/main/java/edu/hm/hafner/util/FilteredLog.java @@ -117,6 +117,9 @@ public void logError(final String message) { lock.lock(); try { if (lines < maxLines) { + if (StringUtils.isNotBlank(title) && errorMessages.isEmpty()) { + errorMessages.add(title); + } errorMessages.add(message); } lines++; @@ -155,13 +158,11 @@ public void logError(final String format, final Object... args) { */ @FormatMethod public void logException(final Exception exception, final String format, final Object... args) { + logError(format, args); + lock.lock(); try { - if (lines < maxLines) { - errorMessages.add(String.format(format, args)); - errorMessages.addAll(Arrays.asList(ExceptionUtils.getRootCauseStackTrace(exception))); - } - lines++; + errorMessages.addAll(Arrays.asList(ExceptionUtils.getRootCauseStackTrace(exception))); } finally { lock.unlock(); @@ -215,9 +216,6 @@ public List getErrorMessages() { if (errorMessages.isEmpty()) { return messages; } - if (StringUtils.isNotBlank(title)) { - messages.add(title); - } messages.addAll(errorMessages); if (lines > maxLines) { messages.add(String.format(" ... skipped logging of %d additional errors ...", lines - maxLines)); @@ -255,7 +253,6 @@ public void merge(final FilteredLog other) { try { infoMessages.addAll(other.getInfoMessages()); errorMessages.addAll(other.getErrorMessages()); - lines += other.lines; } finally { lock.unlock(); diff --git a/src/test/java/edu/hm/hafner/util/FilteredLogTest.java b/src/test/java/edu/hm/hafner/util/FilteredLogTest.java index 3bcfb423..d6b3b7db 100644 --- a/src/test/java/edu/hm/hafner/util/FilteredLogTest.java +++ b/src/test/java/edu/hm/hafner/util/FilteredLogTest.java @@ -98,7 +98,24 @@ void shouldMergeLogger() { assertThat(parent).hasOnlyInfoMessages("parent Info 1", "child Info 1") .hasOnlyErrorMessages("Parent Errors", "parent Error 1", "Child Errors", "child Error 1"); - assertThat(parent.size()).isEqualTo(2); + assertThat(parent.size()).isEqualTo(1); + } + + @Test + void shouldSkipEmptyErrorLogWhenMerging() { + var parent = new FilteredLog("Parent Errors"); + + parent.logInfo("parent Info 1"); + + var child = new FilteredLog("Child Errors"); + child.logInfo("child Info 1"); + child.logError("child Error 1"); + + parent.merge(child); + + assertThat(parent).hasOnlyInfoMessages("parent Info 1", "child Info 1") + .hasOnlyErrorMessages("Child Errors", "child Error 1"); + assertThat(parent.size()).isZero(); } @Test