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

Error titles should not be printed for merged messages #883

Merged
merged 1 commit into from
Dec 21, 2023
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
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