Skip to content

Commit

Permalink
Error titles should not be printed for merged messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhafner committed Dec 21, 2023
1 parent 523cefc commit 4eb1cf6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
15 changes: 6 additions & 9 deletions src/main/java/edu/hm/hafner/util/FilteredLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -215,9 +216,6 @@ public List<String> 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));
Expand Down Expand Up @@ -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();
Expand Down
19 changes: 18 additions & 1 deletion src/test/java/edu/hm/hafner/util/FilteredLogTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4eb1cf6

Please sign in to comment.