Skip to content


Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Latest commit


Git stats


Failed to load latest commit information.

Logstash json_event pattern for log4j

Build Status

What is it?

If you've used log4j, you know that certain appenders support things called "Layouts". These are basically ways for you to control the formatting/rendering of a log event.

There's a full list of formatting macros here if you're curious.

The PatternLayout an organization uses is pretty much begging to be bikeshedded. This library is essentially a "preformatted" layout that just happens to be the exact format that Logstash uses for json_event.


I recently came to a situation where I needed consider parsing some log4j-generated log events with Logstash. I had another appender already but unfortunately the external dependency on ZeroMQ itself was a no go. So there were two options:

  • The experimental log4j input
  • file input

The log4j input is actually pretty cool but needs to be refactored a little bit. It's based around log4j's SocketAppender which actually sends a serialized version of the LoggingEvent object over the wire. This has a few issues that are easily worked out but it's also experimental. I needed something a bit more stable. I also wanted to avoid any filtering if at all possible.

Then I remembered the "hack" that would let you dump your apache or nginx logs in json_event format.

I probably could have pulled this off using a complicated PatternLayout but decided I wanted a turnkey "solution" that did the work for you. That's what this library is.


This is just a quick snippit of a file:

log4j.rootCategory=WARN, RollingLog

If you use this, your logfile will now have one line per event and it will look something like this:

  "message":"Got request from 0:0:0:0:0:0:0:1%0 using Mozilla\/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/32.0.1700.77 Safari\/537.36",

If you point logstash to this file and set the format to json, you'll basically get the same thing in your output because no filtering was needed to get this data into the right places.

Nothing really groundbreaking here. However you can now use this same prefab PatternLayout with ANY appender that supports layouts. If that appender matches up with a logstash input, you've now got flexibility in your transport with reduced filtering impact (since you don't need to parse the logs as much). In fact, if you want to use RabbitMQ, you could use this layout with @jbrisbin's amqp-appender or Ryan Tenney's redis appender


If there is throwable information available in your event, it will be populated under the key exception like so:

    "exception_message":"Something broke!",
    "stacktrace":"java.lang.IllegalArgumentException: Something broke!\n\tat org.eclipse.jetty.examples.logging.EchoFormServlet.doPost(\n\tat javax.servlet.http.HttpServlet.service(\n\tat javax.servlet.http.HttpServlet.service(\n\tat org.eclipse.jetty.servlet.ServletHolder.handle(\n\tat org.eclipse.jetty.servlet.ServletHandler.doHandle(\n\tat org.eclipse.jetty.server.handler.ScopedHandler.handle(\n\tat\n\tat org.eclipse.jetty.server.session.SessionHandler.doHandle(\n\tat org.eclipse.jetty.server.handler.ContextHandler.doHandle(\n\tat org.eclipse.jetty.servlet.ServletHandler.doScope(\n\tat org.eclipse.jetty.server.session.SessionHandler.doScope(\n\tat org.eclipse.jetty.server.handler.ContextHandler.doScope(\n\tat org.eclipse.jetty.server.handler.ScopedHandler.handle(\n\tat org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(\n\tat org.eclipse.jetty.server.handler.HandlerCollection.handle(\n\tat org.eclipse.jetty.server.handler.HandlerWrapper.handle(\n\tat org.eclipse.jetty.server.Server.handle(\n\tat org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(\n\tat org.eclipse.jetty.server.AbstractHttpConnection.content(\n\tat org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.content(\n\tat org.eclipse.jetty.http.HttpParser.parseNext(\n\tat org.eclipse.jetty.http.HttpParser.parseAvailable(\n\tat org.eclipse.jetty.server.AsyncHttpConnection.handle(\n\tat\n\tat$\n\tat org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(\n\tat org.eclipse.jetty.util.thread.QueuedThreadPool$\n\tat"
  "message":"[exception] description = \"kaboom\"",

Easy access to the exception class and exception message let's you work with those....easier.

Sample XML configuration

If you use the XML format for your log4j configuration (and there are valid reasons thanks to AsyncAppender - fml), changing your layout class for your appender would look like this


   <appender name="Console" class="org.apache.log4j.ConsoleAppender">
     <param name="Threshold" value="TRACE" />
     <layout class="org.apache.log4j.PatternLayout">
       <param name="ConversionPattern" value="[%d{dd MMM yyyy HH:mm:ss.SSS}] [%p.%c] %m%n" />


   <appender name="Console" class="org.apache.log4j.ConsoleAppender">
     <param name="Threshold" value="TRACE" />
     <layout class="net.logstash.log4j.JSONEventLayoutV1" />

Any appender that supports defining the layout can use this.


Originally the layout class was called JSONEventLayout. This was originally written back when there was no versioned event format for logstash. As of Logstash 1.2 and forward, the event format is now versioned. The current version is 1 and defined the following required fields:

  • @version
  • @timestamp (optional - will be inferred from event receipt time

Because of this, when adding support for the new format, JSONEventLayoutV1 was used to allow backwards compatibility. As of 1.6 of the jsonevent-layout library, we've now gone to fully versioned appenders. There is no longer a JSONEventLayout. Instead there is:

  • JSONEventLayoutV0
  • JSONEventLayoutV1

Work has stopped on V0 but it won't be removed. No new features are added to V0 (custom UserFields for instance).

Custom User Fields

As of version 1.6, you can now add your own metadata to the event in the form of comma-separated key:value pairs. This can be set in either the log4jconfig OR set on the java command-line:

log4j config

<layout class="net.logstash.log4j.JSONEventLayoutV1" >
  <param name="UserFields" value="foo:bar,baz:quz" />




Note that the command-line version will OVERRIDE any values specified in the config file should there be a key conflict!

java -Dnet.logstash.log4j.JSONEventLayoutV1.UserFields="field3:prop3,field4:prop4" -jar .....

A warning will be logged should you attempt to set values in both places.

Pull Requests

Pull requests are welcome for any and all things - documentation, bug fixes...whatever.


A prefab PatternLayout for log4j that generates logstash json_event formatted data







No packages published