1

I have an application which uses log4j 1.2.17 with MDC. I manage to configure remote logging using org.apache.log4j.net.SocketAppender, which uses TCP protocol:

<appender name="EXTLOG" class="org.apache.log4j.net.SocketAppender">
    <param name="RemoteHost" value="host" />
    <param name="ReconnectionDelay" value="60000" />
    <param name="Threshold" value="TRACE" />
</appender>

Now I want to switch to UDP protocol. I switched to org.apache.log4j.net.SyslogAppender, but SyslogAppender doesn't support MDC (in structured way). I know MDC has much more support in log4j2, is there a way to expose logging with MDC using UDP in log4j 1.2.*?

Patrick Kostjens
  • 5,065
  • 6
  • 29
  • 46
Aram Aslanyan
  • 745
  • 3
  • 11
  • 18

2 Answers2

0

I haven't found solution for this problem on log4j level. Seems log4j2 has better support of described functionality, but its syntax is not compatible with log4j. So, I decided to migrate my logging to Logback. I have used “net.logstash.logback.appender.LogstashSocketAppender”. It is a UDP appender and it fully supports MDC (you just need to configure JSON parser on Logstash).

Aram Aslanyan
  • 745
  • 3
  • 11
  • 18
0

Take a look here: logstash-gelf. This library can transport your log events over various transports (TCP, UDP and Redis) using the GELF spec which is available on logstash. It's usable with log4j, log4j2, logback, java.util.logging and JBossAS 7/Wildfly 8.

You have full MDC support and you're able to configure the MDC features so you can control what you get:

<appender name="gelf" class="biz.paluch.logging.gelf.log4j.GelfLogAppender">
    <param name="Host" value="udp:localhost" />
    ...

    <!-- This are static fields -->
    <param name="AdditionalFields" value="fieldName1=fieldValue1,fieldName2=fieldValue2" />

    <!-- This are fields using MDC -->
    <param name="MdcFields" value="mdcField1,mdcField2" />
    <param name="DynamicMdcFields" value="mdc.*,(mdc|MDC)fields" />
    <param name="IncludeFullMdc" value="true" />
</appender>
mp911de
  • 17,546
  • 2
  • 55
  • 95