I'm wondering if there's an implementation of slf4j that logs into a JSON format. Where each log messages would be a JSON object, 1 per row.
e.g. each line of the log file would look like something this:
{"severity":"WARN", "ts":12345678, "host":"myhostname.com", "message":"Failed to do something"}
I was using Log4j2 and I had a similar requirement. This helped me -
https://github.com/maartenbosteels/log4j-jsonevent-layout
log4j2.xml:
I had to add the following execution to my maven-compiler-plugin build plugin:
If you are using Logback as the backend, check out logstash-logback-encoder. It contains encoders to log in json format.
You can include it as a Maven dependency:
And in your logback.xml configuration file, include the encoder. RollingFileAppender example used with LogstashEncoder mentioned below (Source - logstash-logback-encoder#encoder):
If you are using Log4j as backend:
If you are using JUL (java.util.logging) as backend:
Note that they all format a single log event as JSON object. No valid JSON array is created.
Ok, this thread needs to be updated a bit since most of the answers are quite old. All modern java log liibraries support json layout out of the box. With log4j2 you don't even need any additional libs, just jackson in your classpath, which you most likely already have.
If this is not enough, you can also take a look at this repo: https://github.com/savoirtech/slf4j-json-logger
I guess this is what you are looking for: http://jsontools.berlios.de/articles/faq/what-is-in-log4j.html
I haven't used this before but on the page i mentioned says:
Some time ago I wrote a
MongoDBAppender
for Logback (see also LBCLASSIC-261). As the name implies it uses MongoDB to store logging events, but since MongoDB facilitates JSON-like document store, you might find it useful: