Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce log level filtering support in LogRecordListener
Issue: #1047
- Loading branch information
Showing
5 changed files
with
37 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
|
@@ -15,6 +15,7 @@ | ||
|
|
||
import java.util.ArrayList; | import java.util.ArrayList; | ||
import java.util.List; | import java.util.List; | ||
import java.util.logging.Level; | |||
import java.util.logging.LogRecord; | import java.util.logging.LogRecord; | ||
|
|
||
import org.apiguardian.api.API; | import org.apiguardian.api.API; | ||
|
@@ -78,4 +79,35 @@ public List<LogRecord> getLogRecords(Class<?> clazz) { | ||
// @formatter:on | // @formatter:on | ||
} | } | ||
|
|
||
/** | |||
* Get the list of {@link LogRecord log records} that have been | |||
* {@linkplain #logRecordSubmitted submitted} to this listener | |||
* for the given class at the given log level. | |||
* | |||
* <p>As stated in the JavaDoc for {@code LogRecord}, a submitted | |||
* {@code LogRecord} should not be updated by the client application. Thus, | |||
* the {@code LogRecords} in the returned list should only be inspected for | |||
* testing purposes and not modified in any way. | |||
* | |||
* @param clazz the class for which to get the log records; never {@code null} | |||
* @param level the log level for which to get the log records; never {@code null} | |||
*/ | |||
public List<LogRecord> getLogRecords(Class<?> clazz, Level level) { | |||
// NOTE: we cannot use org.junit.platform.commons.util.Preconditions here | |||
// since that would introduce a package cycle. | |||
if (clazz == null) { | |||
throw new JUnitException("Class must not be null"); | |||
} | |||
if (level == null) { | |||
throw new JUnitException("Level must not be null"); | |||
} | |||
|
|||
// @formatter:off | |||
return this.logRecords.stream() | |||
.filter(logRecord -> logRecord.getLoggerName().equals(clazz.getName())) | |||
.filter(logRecord -> logRecord.getLevel() == level) | |||
.collect(toList()); | |||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
jbduncan
Contributor
|
|||
// @formatter:on | |||
} | |||
|
|||
} | } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@sbrannen I think the list returned here (as well as the list returned by
getLogRecords(Class)
) should explicitly be made either mutable (.collect(toCollection(ArrayList::new))
) or unmodifiable (.collect(CollectionUtils.toUnmodifiableList())
) to ensure that its mutability is consistent across all JDKs.