Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does log4j support JSON format?

Tags:

java

json

log4j

Is it possible to let log4j output its logging in JSON format by only altering the log4j.properties / xml configuration files?

I make use of an old application that uses log4j 1.2. I only see the standard layout but no JSON layout.

like image 258
Stefan Avatar asked Apr 01 '15 09:04

Stefan


People also ask

What is log4j format?

Apache log4j provides various Layout objects, each of which can format logging data according to various layouts. It is also possible to create a Layout object that formats logging data in an application-specific way. All Layout objects receive a LoggingEvent object from the Appender objects.

What is JSON logging?

JSON logging is a kind of structured logging, Meaning you'll have your log data parsed and stored in a structured JSON format.

What is difference between log4j and log4j2?

Community support: Log4j 1. x is not actively maintained, whereas Log4j 2 has an active community where questions are answered, features are added and bugs are fixed. Automatically reload its configuration upon modification without losing log events while reconfiguring.


3 Answers

this is official JSON layout

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

1) Add maven dependency https://mvnrepository.com/artifact/net.logstash.log4j/jsonevent-layout

<dependency>
    <groupId>net.logstash.log4j</groupId>
    <artifactId>jsonevent-layout</artifactId>
    <version>1.7</version>  
</dependency>

2) Add configuration to your log4j.properties file

 log4j.rootCategory=WARN, RollingLog
 log4j.appender.RollingLog=org.apache.log4j.DailyRollingFileAppender
 log4j.appender.RollingLog.Threshold=TRACE
 log4j.appender.RollingLog.File=api.log
 log4j.appender.RollingLog.DatePattern=.yyyy-MM-dd
 log4j.appender.RollingLog.layout=net.logstash.log4j.JSONEventLayoutV1
like image 152
Karpov S.Y. Avatar answered Oct 17 '22 13:10

Karpov S.Y.


just use buildin PatternLayout is ok:

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.encoding=UTF-8
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern={"debug_level":"%p","debug_timestamp":"%d{ISO8601}","debug_thread":"%t","debug_file":"%F", "debug_line":"%L","debug_message":"%m"}%n

will out put like:

{
    "debug_level" : "INFO",
    "debug_timestamp" : "2016-05-26 16:37:08,938",
    "debug_thread" : "main",
    "debug_file" : "TestLogOutPut.java",
    "debug_line" : "316",
    "debug_message" : "hello i am a log message"
}
like image 29
chad chen Avatar answered Oct 17 '22 13:10

chad chen


Yes It is possible. Take a look at this link It can generate

{
   "timestamp":1352412458890,
   "date":"Nov 8, 2012 10:07:38 PM",
   "hostname":"michael1",
   "username":"michael",
   "level":"INFO",
   "thread":"main",
   "classname":"uk.me.mjt.log4jjson.SimpleJsonLayoutTest",
   "filename":"SimpleJsonLayoutTest.java",
   "linenumber":25,
   "methodname":"testDemonstration",
   "message":"Example of some logging"
 }
like image 2
Anna Malai Avatar answered Oct 17 '22 15:10

Anna Malai