Skip to content
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
<pmd.skip>false</pmd.skip>

<!-- Project Dependencies Configuration -->
<spotbugs.version>4.9.3</spotbugs.version>
<spotbugs.version>4.9.5</spotbugs.version>
<commons.lang.version>3.18.0</commons.lang.version>
<commons.io.version>2.20.0</commons.io.version>
<byte-buddy.version>1.17.7</byte-buddy.version>
Expand Down
39 changes: 24 additions & 15 deletions src/main/java/edu/hm/hafner/util/FilteredLog.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package edu.hm.hafner.util;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;

import com.google.errorprone.annotations.FormatMethod;

import java.io.Serial;
import java.io.Serializable;
import java.util.ArrayList;
Expand All @@ -9,11 +14,6 @@
import java.util.Objects;
import java.util.concurrent.locks.ReentrantLock;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;

import com.google.errorprone.annotations.FormatMethod;

/**
* Provides a log of info messages and a limited number of error messages. If the number of errors exceeds this limit,
* then further error messages will be skipped. This class is thread-safe and can be used in a distributed
Expand Down Expand Up @@ -120,13 +120,7 @@ public void logInfo(final String format, final Object... args) {
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++;
logErrorWithGuard(message);
}
finally {
lock.unlock();
Expand Down Expand Up @@ -162,10 +156,9 @@ 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 {
logErrorWithGuard(format.formatted(args));
if (lines <= maxLines) {
errorMessages.addAll(Arrays.asList(ExceptionUtils.getRootCauseStackTrace(exception)));
}
Expand All @@ -175,13 +168,29 @@ public void logException(final Exception exception, final String format, final O
}
}

private void logErrorWithGuard(final String message) {
if (lines < maxLines) {
if (StringUtils.isNotBlank(title) && errorMessages.isEmpty()) {
errorMessages.add(title);
}
errorMessages.add(message);
}
lines++;
}

/**
* Returns the total number of errors that have been reported.
*
* @return the total number of errors
*/
public int size() {
return lines;
lock.lock();
try {
return lines;
}
finally {
lock.unlock();
}
}

/**
Expand Down
Loading