|
4 | 4 | */
|
5 | 5 | package org.hibernate.validator.testutils;
|
6 | 6 |
|
| 7 | +import static java.util.Collections.unmodifiableList; |
| 8 | + |
7 | 9 | import java.util.ArrayList;
|
8 | 10 | import java.util.Collections;
|
9 | 11 | import java.util.List;
|
| 12 | +import java.util.function.Predicate; |
10 | 13 |
|
| 14 | +import org.apache.logging.log4j.Level; |
11 | 15 | import org.apache.logging.log4j.core.LogEvent;
|
12 | 16 | import org.apache.logging.log4j.core.appender.AbstractAppender;
|
13 | 17 | import org.apache.logging.log4j.core.config.Property;
|
14 | 18 | import org.apache.logging.log4j.core.impl.MutableLogEvent;
|
| 19 | +import org.apache.logging.log4j.message.Message; |
15 | 20 |
|
16 | 21 | /**
|
17 | 22 | * A simple log appender for tests.
|
@@ -39,6 +44,25 @@ public void clear() {
|
39 | 44 | }
|
40 | 45 |
|
41 | 46 | public List<LogEvent> getEvents() {
|
42 |
| - return Collections.unmodifiableList( this.events ); |
| 47 | + return unmodifiableList( this.events ); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Returns all logged messages |
| 52 | + */ |
| 53 | + public List<String> getMessages() { |
| 54 | + return getMessages(logEvent -> true); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Returns logged messages matching the specified {@link Level} |
| 59 | + */ |
| 60 | + public List<String> getMessages(Level level) { |
| 61 | + return getMessages(logEvent -> logEvent.getLevel().equals(level)); |
| 62 | + } |
| 63 | + |
| 64 | + private List<String> getMessages(Predicate<LogEvent> filter) { |
| 65 | + return events.stream().filter(filter).map(LogEvent::getMessage) |
| 66 | + .map(Message::getFormattedMessage).toList(); |
43 | 67 | }
|
44 | 68 | }
|
0 commit comments