t0xa / gelfj

Graylog Extended Log Format (GELF) implementation in Java and log4j appender without any dependencies.
https://github.com/t0xa/gelfj/wiki
Other
186 stars 116 forks source link

GelfMessageFactory (1.1.12) does not handle MDC correctly #92

Closed martin-walsh closed 8 years ago

martin-walsh commented 8 years ago

GelfMessageFactory is assuming that GelfAppender is used directly and not wrapped in an AsyncAppender. The MDC access breaks if the used with AsyncAppender. The fix is pretty simple:

Replace

    // Get MDC and add a GELF field for each key/value pair
    Map<String, Object> mdc = MDC.getContext();

with:

    // Get MDC and add a GELF field for each key/value pair
    Map<String, Object> mdc = event.getProperties();

If there is an AsyncAppender, Log4J will have populated the mdcCopy within the LoggingEvent already. If there is no AsyncAppender, Log4J will populate the mdcCopy when getProperties() is invoked, and return the MDC.

martin-walsh commented 8 years ago

Added pull request. All existing tests pass including the MDC test case in GelfAppenderTest

t0xa commented 8 years ago

Hi Martin,

Thanks a lot for your contribution. I will release new version shortly.

Anton