'How to test logstash Marker in Junit
I have to create a Junit test for my class logging
Map<String, Object> mapMarker = new HashMap<>();
mapMarker.put("input_source", "test_input_source");
LogstashMarker logstashMarker = Markers.appendEntries(mapMarker);
log.info(logstashMarker, "logging test OK");
What can I do to show that in my log, there is the Marker "input_source"
with the value "test_input_source"
. (there is no problem with the log message, the Marker isn't in the message log) ?
I try using ILoggingEvent
to catch the log, but I can't get the value of my Marker "input_source"
from this.
Updated : I use LogstashEncoder
to encode the log, than ObjectMapper
to parse this log to a map. With this way, i can get the marker in the map.
Thanks
Solution 1:[1]
You can configure your logger like so for testing purposes:
// src/test/resources/logback-test.xml
<configuration>
<appender name="LIST" class="ch.qos.logback.core.read.ListAppender"/>
<root level="info">
<appender-ref ref="LIST"/>
</root>
</configuration>
and then stream through the LIST
appender for events:
private void assertLogsDoNotContain() {
Logger logger = (Logger) LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
ListAppender<ILoggingEvent> loggingEventStream = (ListAppender<ILoggingEvent>) logger.getAppender("LIST");
assertThat(loggingEventStream.list.stream()
.filter(x -> x.toString().contains("logging test OK"))
.filter(y -> y.getMarker().contains(Markers.append("input_source", "test_input_source")))
).isNotEmpty();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |