mirror of
https://github.com/tiennm99/tjahzi.git
synced 2026-10-14 00:17:02 +00:00
Test for MDC logging.
This commit is contained in:
1 parent
50a7c7f5ac
commit
9729d2bd2c
2 files changed
+69
No files matched your search
+39
@@ -0,0 +1,39 @@
|
||||
package pl.tkowalcz.tjahzi.log4j2;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.apache.logging.log4j.ThreadContext;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import pl.tkowalcz.tjahzi.log4j2.infra.IntegrationTest;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
|
||||
import static pl.tkowalcz.tjahzi.log4j2.infra.LokiAssert.assertThat;
|
||||
|
||||
class MdcLogLabelConfigurationTest extends IntegrationTest {
|
||||
|
||||
@Test
|
||||
void shouldSendMdcLogLabelAsConfigured() {
|
||||
// Given
|
||||
loadConfig("appender-test-with-md-log-label-set.xml");
|
||||
Logger logger = LogManager.getLogger(MdcLogLabelConfigurationTest.class);
|
||||
|
||||
// When
|
||||
String tid = UUID.randomUUID().toString();
|
||||
ThreadContext.put("tid", tid);
|
||||
|
||||
logger.info("Test");
|
||||
|
||||
// Then
|
||||
assertThat(loki)
|
||||
.returns(response -> response
|
||||
.body("data.result.size()", greaterThanOrEqualTo(1))
|
||||
.body("data.result[0].stream.server", equalTo("127.0.0.1"))
|
||||
.body("data.result[0].stream.tid1", equalTo(tid))
|
||||
.body("data.result[0].stream.tid2", equalTo(tid))
|
||||
.body("data.result[0].stream.tid3", equalTo(tid))
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration packages="pl.tkowalcz.tjahzi.log4j2">
|
||||
<Loggers>
|
||||
<Root level="OFF">
|
||||
</Root>
|
||||
|
||||
<Logger name="pl.tkowalcz" level="ALL">
|
||||
<AppenderRef ref="Loki"/>
|
||||
</Logger>
|
||||
</Loggers>
|
||||
|
||||
<appenders>
|
||||
<Loki name="Loki">
|
||||
<host>${sys:loki.host}</host>
|
||||
<port>${sys:loki.port}</port>
|
||||
|
||||
<ThresholdFilter level="ALL"/>
|
||||
<PatternLayout>
|
||||
<Pattern>%X{tid} [%t] %d{MM-dd HH:mm:ss.SSS} %5p %c{1} - %m%n%exception{full}</Pattern>
|
||||
</PatternLayout>
|
||||
|
||||
<Header name="server" value="127.0.0.1"/>
|
||||
<Label name="server" value="127.0.0.1"/>
|
||||
|
||||
<Label name="tid1" pattern="%X{tid}"/>
|
||||
<Label name="tid2" value="${ctx:tid}"/>
|
||||
<Label name="tid3" pattern="%mdc{tid}"/>
|
||||
</Loki>
|
||||
</appenders>
|
||||
</configuration>
|
||||
Reference in new issue
Block a user