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

Commit

Permalink
Derive MDC from LoggingEvent #145
Browse files Browse the repository at this point in the history
The MDC is now attempted to be looked up from the LoggingEvent to include the message context across Thread boundaries.
  • Loading branch information
mp911de committed May 18, 2018
1 parent d43c2dc commit 84164b8
Showing 1 changed file with 45 additions and 16 deletions.
61 changes: 45 additions & 16 deletions src/main/java/biz/paluch/logging/gelf/log4j/Log4jLogEvent.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
package biz.paluch.logging.gelf.log4j;

import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Level;
import org.apache.log4j.MDC;
import org.apache.log4j.NDC;
import org.apache.log4j.spi.LocationInfo;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.spi.ThrowableInformation;

import biz.paluch.logging.gelf.DynamicMdcMessageField;
import biz.paluch.logging.gelf.GelfUtil;
import biz.paluch.logging.gelf.LogEvent;
import biz.paluch.logging.gelf.LogMessageField;
import biz.paluch.logging.gelf.MdcMessageField;
import biz.paluch.logging.gelf.MessageField;
import biz.paluch.logging.gelf.Values;
import biz.paluch.logging.gelf.*;
import biz.paluch.logging.gelf.intern.GelfMessage;

/**
Expand All @@ -26,10 +20,30 @@
*/
class Log4jLogEvent implements LogEvent {

private LoggingEvent loggingEvent;
private static final Field mdcCopy = getMdcCopyField();

private final LoggingEvent loggingEvent;
private final Map mdc;

public Log4jLogEvent(LoggingEvent loggingEvent) {
this.loggingEvent = loggingEvent;
this.mdc = getMdc(loggingEvent);
}

private Map getMdc(LoggingEvent loggingEvent) {
Map mdc = null;

try {
if (mdcCopy != null) {
mdc = (Map) mdcCopy.get(loggingEvent);
}
} catch (IllegalAccessException e) {
}

if (mdc == null) {
mdc = MDC.getContext();
}
return mdc;
}

@Override
Expand Down Expand Up @@ -129,7 +143,7 @@ public String getValue(LogMessageField field) {
case LoggerName:
return loggingEvent.getLoggerName();
case NDC:
String ndc = NDC.get();
String ndc = loggingEvent.getNDC();
if (ndc != null && !"".equals(ndc)) {
return ndc;
}
Expand Down Expand Up @@ -170,9 +184,12 @@ private String getValue(MdcMessageField field) {

@Override
public String getMdcValue(String mdcName) {
Object value = MDC.get(mdcName);
if (value != null) {
return value.toString();

if (mdc != null) {
Object value = mdc.get(mdcName);
if (value != null) {
return value.toString();
}
}
return null;
}
Expand All @@ -195,9 +212,9 @@ private Values getMdcValues(DynamicMdcMessageField field) {

private Set<String> getAllMdcNames() {
Set<String> mdcNames = new HashSet<String>();
Map context = MDC.getContext();
if (context != null) {
mdcNames.addAll(context.keySet());

if (mdc != null) {
mdcNames.addAll(mdc.keySet());
}
return mdcNames;
}
Expand All @@ -217,4 +234,16 @@ private Set<String> getMatchingMdcNames(DynamicMdcMessageField field, Set<String
public Set<String> getMdcNames() {
return getAllMdcNames();
}

private static Field getMdcCopyField() {

try {
Field mdcCopy = LoggingEvent.class.getDeclaredField("mdcCopy");
mdcCopy.setAccessible(true);

return mdcCopy;
} catch (Exception e) {
return null;
}
}
}

0 comments on commit 84164b8

Please sign in to comment.