Log4j Json Template Layout Config

250 views Asked by At

I am new to log4j logging and I have a scenario where I want to configure log4j to send http post request using JSON template layout for specific body structure. Below is the body format which is required for http request.

{
    "streams": [
        {
            "stream": {
                "label": "data"
            },
            "values": [
                [
                    "unix epoch till nano second", "strigified json log"
                ]
            ]
        }
    ]
}

How can I configure it using log4j with json template layout? Also any good references for better understanding log4j and Json template layout apart from official docs? TIA !

Basic log4j configuration and sample json template layout for provided json object

1

There are 1 answers

1
hamzaouni On

Add the log4j dependency to your project. If you are using Maven, add the following to your pom.xml:

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

Here's a sample log4j.properties configuration file:

log4j.rootLogger=INFO, HTTP_POST

log4j.appender.HTTP_POST=org.apache.log4j.net.SocketAppender
log4j.appender.HTTP_POST.Port=8080
log4j.appender.HTTP_POST.RemoteHost=example.com
log4j.appender.HTTP_POST.ReconnectionDelay=5000
log4j.appender.HTTP_POST.layout=org.apache.log4j.net.JSONLayout