-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStructuredLoggingTest.java
More file actions
39 lines (27 loc) · 1.23 KB
/
StructuredLoggingTest.java
File metadata and controls
39 lines (27 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package fi.hsl.common.logging;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.read.ListAppender;
import org.junit.Test;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.assertEquals;
public class StructuredLoggingTest {
private static final Logger log = (Logger) LoggerFactory.getLogger(StructuredLoggingTest.class);
@Test
public void testStructuredLoggingWithMDC() {
ListAppender<ILoggingEvent> listAppender = new ListAppender<>();
listAppender.start();
log.addAppender(listAppender);
Map<String, String> fields = Map.of("inFlight", "42", "userId", "KaladinStormblessed");
LogUtils.withFields(fields, () -> log.info("Processing messages"));
List<ILoggingEvent> logsList = listAppender.list;
assertEquals(1, logsList.size());
ILoggingEvent logEvent = logsList.get(0);
assertEquals("Processing messages", logEvent.getFormattedMessage());
assertEquals("42", logEvent.getMDCPropertyMap().get("inFlight"));
assertEquals("KaladinStormblessed", logEvent.getMDCPropertyMap().get("userId"));
listAppender.stop();
}
}