JSON Login

I am wondering if there is a slf4j implementation that comes in JSON format. Where each log message will be a JSON object, 1 per line.

eg. each line of the log file will look something like this:

{"severity":"WARN", "ts":12345678, "host":"myhostname.com", "message":"Failed to do something"} 
+4
source share
6 answers

I think this is what you are looking for: http://jsontools.berlios.de/articles/faq/what-is-in-log4j.html

I have not used this before, but the page mentioned says:

It contains a formatter for formatting log entries in JSON format. You can install it in your logging system and then analyze the log stream using basic tools.

+2
source

If you use Log4j as a backend:

If you use JUL (java.util.logging) as a backend:

Note that they all format a single log event as a JSON object. There is no valid JSON array.

+2
source

If you use Logback as a backend, check the logstash-logback-encoder . It contains encoders for entering json format.

You can include it as a dependency on Maven:

 <dependency> <groupId>net.logstash.logback</groupId> <artifactId>logstash-logback-encoder</artifactId> <version>4.4</version> </dependency> 

And in the logback.xml configuration file, enable the encoder. Example RollingFileAppender used with the following LogstashEncoder (Source - logstash-logback-encoder # encoder ):

 <?xml version="1.0" encoding="UTF-8"?> <configuration> <appender name="stash" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>info</level> </filter> <file>/some/path/to/your/file.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>/some/path/to/your/file.log.%d{yyyy-MM-dd}</fileNamePattern> <maxHistory>30</maxHistory> </rollingPolicy> <encoder class="net.logstash.logback.encoder.LogstashEncoder" /> </appender> <root level="all"> <appender-ref ref="stash" /> </root> </configuration> 
+2
source

I used Log4j2 and I had a similar requirement. It helps me -

https://github.com/maartenbosteels/log4j-jsonevent-layout

log4j2.xml:

 <Console name="Console" target="SYSTEM_OUT"> <JSONEventLayoutV1> </Console> 

I had to add the following execution to my maven-compiler-plugin build plugin:

 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <executions> <execution> <id>log4j-plugin-processor</id> <goals> <goal>compile</goal> </goals> <phase>process-classes</phase> <configuration> <proc>only</proc> <annotationProcessors> <annotationProcessor>org.apache.logging.log4j.core.config.plugins.processor.PluginProcessor</annotationProcessor> </annotationProcessors> </configuration> </execution> </executions> </plugin> 
+2
source

Some time ago I wrote MongoDBAppender for Logback (see also LBCLASSIC-261 ). As the name suggests, it uses MongoDB to store registration events, but since MongoDB makes it easy to store JSON documents, you might find it useful:

 { "_id" : ObjectId("4d9cbcbf7abb3abdaf9679cd"), "timeStamp" : ISODate("2011-04-06T19:19:27.006Z"), "level" : "ERROR", "thread" : "main", "logger" : "ch.qos.logback.classic.db.mongo.MongoDBAppenderTest", "message" : "D" } 
+1
source

Well, this thread needs to be updated a bit, as most of the answers are pretty old. All modern java log libraries support json layout out of the box. With log4j2, you don’t even need additional libs, just jackson in your class path, 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

0
source

Source: https://habr.com/ru/post/1391574/


All Articles