You can use a class JsonLog4jLayout to configure your Log4j logger to achieve JSON format.

Here is an example of the log4j.properties file:

log4j.rootLogger=INFO, FILE

log4j.appender.FILE=org.apache.log4j.RollingFileAppender  
log4j.appender.FILE.layout=org.javalite.logging.JsonLog4jLayout

Such a configuration will convert every log line into a self-contained JSON document with the same values as a regular log line. For example, this code:

1
2
3
Logger logger = LoggerFactory.getLogger(getClass());
logger.info("hello");
logger.error("world");

will print the following into a log:

{"level":"INFO","timestamp":"Fri Feb 24 15:20:15 CST 2017","thread":"main","logger":"org.javalite.activejdbc.logging.JsonLog4jLayoutSpec","message":"hello"}
{"level":"ERROR","timestamp":"Fri Feb 24 15:20:15 CST 2017","thread":"main","logger":"org.javalite.activejdbc.logging.JsonLog4jLayoutSpec","message":"world"}

Such information id easy to ship to a log analyzer such as Splunk as well as various ELK for easy search and analysis.

Custom date format

In order to format the timestamp in a log line you can add an optional parameter: DateFormatPattern:

log4j.appender.FILE.layout=org.javalite.logging.JsonLog4jLayout
log4j.appender.FILE.layout.DateFormatPattern=yyyy-MM-dd HH:mm:ss.SSS

The format for this parameter is the same as for SimpleDateFormat


How to comment

The comment section below is to discuss documentation on this page.

If you have an issue, or discover bug, please follow instructions on the Support page