Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upgrading LogStash Log4j configuration to Log4j2

I am trying to migrate my Spring Boot app from Log4j to Log4j 2. I understand the Log4j 2 changed the syntax of .properties configurations from the original Log4j.

Here is a snippet of my original Log4j configuration:

log4j.appender.logstash=org.apache.log4j.net.SocketAppender
log4j.appender.logstash.layout=org.apache.log4j.PatternLayout
log4j.appender.logstash.layout.ConversionPattern=%d{HH:mm:ss.SSS} %-5p %c {2} %x - %m%n
log4j.appender.logstash.Port=4560
log4j.appender.logstash.RemoteHost=logs.example.com
log4j.appender.logstash.ReconnectionDelay=10000

and here is what I updated it to, for Log4j 2:

appender.logstash.type=SocketAppender # Or 'Socket'
appender.logstash.name=logStash
appender.logstash.layout.type=PatternLayout
appender.logstash.layout.pattern=%d{HH:mm:ss.SSS} %-5p %c {2} %x - %m%n
appender.logstash.host=logs.example.com
appender.logstash.port=4560
appender.logstash.reconnectionDelayMillis=10000

However, when I do this, my console and file appenders work just fine, but the LogStash appender throws a Broken pipe error.

I've looked into this an it seems that I may need to wrap the SocketAppender in an AsyncAppender and pointing the ref towards my logStash SocketAppender (as in this documentation), but then I get exceptions that the properties do not exist: appender.async.type=Async appender.async.ref=logStash # Or 'refs', 'appender-ref', etc.

Is a .properties file not the way to go for Async Socket Appenders? I could use some help in migrating from Log4j 1 to 2 via this properties file.

like image 673
Igor Avatar asked Oct 22 '25 12:10

Igor


1 Answers

There is a problem in older releases of logstash-log4j2. It was fixed in release 3.2. The newer release may be taken from this link.

Besides, the appender name is not SocketAppender but Socket - that is according to the documentation example, which uses the name Socket in log4j2.xml:

<Appenders>
    <Socket name="socket" host="localhost" port="9500">
      <SerializedLayout />
    </Socket>
</Appenders>

I suppose you should change you log4j2.properties accordingly:

appender.logstash.type=Socket
appender.logstash.name=logStash
appender.logstash.layout.type=PatternLayout
...

I know that this is the matter of habit and "do-not-change-the-working-code" approach but may be it worth to move to xml configuration format while migrating to log4j2, since vast of the examples in the documentation are using it.

like image 180
asch Avatar answered Oct 24 '25 21:10

asch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!