Skip to content
This repository has been archived by the owner on Jun 29, 2023. It is now read-only.

Commit

Permalink
Fix context data with non-String values
Browse files Browse the repository at this point in the history
I kept the defensive null checks but removed the call to containsKey
because Map#get will return null anyway.
  • Loading branch information
waldeinburg committed Feb 5, 2021
1 parent f44c903 commit 4b2aab2
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/main/java/biz/paluch/logging/gelf/log4j2/Log4j2LogEvent.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package biz.paluch.logging.gelf.log4j2;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.util.ReadOnlyStringMap;

import biz.paluch.logging.gelf.*;
import biz.paluch.logging.gelf.intern.GelfMessage;
import org.apache.logging.log4j.util.ReadOnlyStringMap;

/**
* @author Mark Paluch
Expand Down Expand Up @@ -172,11 +173,19 @@ private String getValue(MdcMessageField field) {
@Override
public String getMdcValue(String mdcName) {
ReadOnlyStringMap contextData = logEvent.getContextData();
if (null != contextData && contextData.containsKey(mdcName)) {
return contextData.getValue(mdcName);
if (null == contextData) {
return null;
}

// Values in the context data are not guaranteed to be String, e.g. if
// log4j2.threadContextMap is set to
// org.apache.logging.log4j.spi.CopyOnWriteSortedArrayThreadContextMap
Map<String, String> contextDataMap = contextData.toMap();
if (null == contextDataMap) {
return null;
}

return null;
return contextDataMap.get(mdcName);
}

@Override
Expand Down
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);
}
}

0 comments on commit 4b2aab2

Please sign in to comment.