This repository has been archived by the owner on Jun 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix context data with non-String values #261
- Loading branch information
1 parent
2e231c7
commit ef37a6c
Showing
2 changed files
with
117 additions
and
2 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
111 changes: 111 additions & 0 deletions
111
src/test/java/biz/paluch/logging/gelf/log4j2/GelfLogAppenderNonStringMdcTests.java
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package biz.paluch.logging.gelf.log4j2; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.Map; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.apache.logging.log4j.ThreadContext; | ||
import org.apache.logging.log4j.core.LoggerContext; | ||
import org.apache.logging.log4j.core.config.ConfigurationFactory; | ||
import org.apache.logging.log4j.spi.ObjectThreadContextMap; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import biz.paluch.logging.gelf.GelfTestSender; | ||
import biz.paluch.logging.gelf.JsonUtil; | ||
import biz.paluch.logging.gelf.intern.GelfMessage; | ||
|
||
/** | ||
* @author Mark Paluch, Daniel Lundsgaard Skovenborg | ||
*/ | ||
class GelfLogAppenderNonStringMdcTests { | ||
private static final String LOG_MESSAGE = "foo bar test log message"; | ||
|
||
private static LoggerContext loggerContext; | ||
|
||
@BeforeAll | ||
static void setupClass() { | ||
System.setProperty("log4j2.threadContextMap", "org.apache.logging.log4j.spi.CopyOnWriteSortedArrayThreadContextMap"); | ||
System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, "log4j2/log4j2-dynamic-mdc.xml"); | ||
loggerContext = (LoggerContext) LogManager.getContext(false); | ||
loggerContext.reconfigure(); | ||
} | ||
|
||
@AfterAll | ||
static void afterClass() throws Exception { | ||
System.clearProperty("log4j2.threadContextMap"); | ||
System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY); | ||
loggerContext.reconfigure(); | ||
// Assert that setup worked. | ||
assert ThreadContext.getThreadContextMap() instanceof ObjectThreadContextMap; | ||
} | ||
|
||
@BeforeEach | ||
void before() throws Exception { | ||
GelfTestSender.getMessages().clear(); | ||
ThreadContext.clearAll(); | ||
} | ||
|
||
/** | ||
* Copy of GelfLogAppenderDynamicMdcTests#testWithMdcFieldTypes() with raw types instead of String values | ||
*/ | ||
@Test | ||
void testWithRawMdcFieldTypes() throws Exception { | ||
|
||
Logger logger = loggerContext.getLogger(getClass().getName()); | ||
ObjectThreadContextMap contextMap = (ObjectThreadContextMap) ThreadContext.getThreadContextMap(); | ||
contextMap.putValue("myMdcs", "String"); | ||
contextMap.putValue("myMdcl", 1); | ||
contextMap.putValue("myMdci", 2L); | ||
contextMap.putValue("myMdcd", 2.1); | ||
contextMap.putValue("myMdcf", 2.2d); | ||
|
||
logger.info(LOG_MESSAGE); | ||
assertThat(GelfTestSender.getMessages()).hasSize(1); | ||
|
||
GelfMessage gelfMessage = GelfTestSender.getMessages().get(0); | ||
Map<String, Object> jsonObject = JsonUtil.parseToMap(gelfMessage.toJson("")); | ||
|
||
assertThat(jsonObject.get("myMdcs")).isEqualTo("String"); | ||
assertThat(jsonObject.get("myMdcl")).isEqualTo(1); | ||
assertThat(jsonObject.get("myMdci")).isEqualTo(2); | ||
|
||
assertThat(jsonObject.get("myMdcd")).isEqualTo(2.1); | ||
assertThat(jsonObject.get("myMdcf")).isEqualTo(2.2); | ||
|
||
ThreadContext.put("myMdcl", "1.1"); | ||
ThreadContext.put("myMdci", "2.1"); | ||
ThreadContext.put("myMdcd", "wrong"); | ||
ThreadContext.put("myMdcf", "wrong"); | ||
|
||
GelfTestSender.getMessages().clear(); | ||
logger.info(LOG_MESSAGE); | ||
assertThat(GelfTestSender.getMessages()).hasSize(1); | ||
|
||
gelfMessage = GelfTestSender.getMessages().get(0); | ||
jsonObject = JsonUtil.parseToMap(gelfMessage.toJson("")); | ||
|
||
assertThat(jsonObject.get("myMdcl")).isEqualTo(1); | ||
assertThat(jsonObject.get("myMdci")).isEqualTo(2); | ||
|
||
assertThat(jsonObject.get("myMdcd")).isNull(); | ||
assertThat(jsonObject.get("myMdcf")).isEqualTo(0.0); | ||
|
||
ThreadContext.put("myMdcl", "b"); | ||
ThreadContext.put("myMdci", "a"); | ||
|
||
GelfTestSender.getMessages().clear(); | ||
logger.info(LOG_MESSAGE); | ||
assertThat(GelfTestSender.getMessages()).hasSize(1); | ||
|
||
gelfMessage = GelfTestSender.getMessages().get(0); | ||
jsonObject = JsonUtil.parseToMap(gelfMessage.toJson("")); | ||
|
||
assertThat(jsonObject.get("myMdcl")).isNull(); | ||
assertThat(jsonObject.get("myMdci")).isEqualTo(0); | ||
} | ||
} |