Log4j2 JSON Configuration for Java Logging Severity Levels, Formatting and Appenders


In below previous post you read about Log4j2 XML configuration for Appenders, formatters and Loggers for Console and File Logging. I have also explained about RollingFile appeneders and there management.

Below is Log4j2 JSON configuration equivalent to XML configuration and dependency required for JSON. For more info in detail and configuration steps follow previous post for Log4J2 XML configuration.

You have to add below JSON dependency in your class path or pom.xml

POM.XML

<!-- basic Log4j2 dependency -->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.6.1</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.6.1</version>
</dependency>
<!-- Asynchronous logging for multithreaded env -->
<dependency>
    <groupId>com.lmax</groupId>
    <artifactId>disruptor</artifactId>
    <version>3.3.4</version>
</dependency>

<!-- Jackson JSON Processor -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.4.1</version>
    </dependency>

create file log4j2.json in your root folder or resource folder.

{
   "configuration": {
       "status": "info",
       "monitorInterval": "60",
       "name": "FacingIssuesOnIT",
      "properties": {
         "property": [
            {
               "name": "filename",
               "value": "target/FacingIssueOnIT.log"
            },
            {
               "name": "log-path",
               "value": "C:/logs/"
            }
         ]
      },
      "appenders": {
         "Console": {
            "PatternLayout": {
               "pattern": "%d{yyyyMMdd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"
            },
            "name": "STDOUT",
            "target": "SYSTEM_OUT"
         },
         "File": {
            "PatternLayout": {
               "pattern": "%d %p %c{1.} [%t] %m%n"
            },
            "name": "file",
            "fileName": "${filename}"
         },
         "RollingFile": {
            "PatternLayout": {
               "pattern": "%d{yyyyMMdd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"
            },

                "Policies": {
               "SizeBasedTriggeringPolicy": {
                  "size": "100 MB"
               },
               "TimeBasedTriggeringPolicy": {
                  "interval": "1",
                  "modulate": "true"
               }
            },
            "DefaultRolloverStrategy": {
               "Delete": {
                  "IfFileName": {
                     "glob": "*/FacingIssueOnIT-*.log.gz"
                  },
                  "IfLastModified": {
                     "age": "1h"
                  },
                  "basePath": "${log-path}",
                  "maxDepth": "2"
               }
            },
            "name": "RollingFile",
            "fileName": "${log-path}/FacingIssueOnIT.log",
            "filePattern": "${log-path}/$${date:yyyy-MM-dd}/FacingIssueOnIT-%d{yyyy-MM-dd}-%i.log.gz"
         }
      },
      "Loggers": {
         "Logger": [
            {
               "name": "com.logging",
               "level": "debug"
            },
            {
               "appender-ref": {
                  "ref": "RollingFile",
                  "level": "debug"
               },
               "name": "root",
               "level": "debug",
               "additivity": "false"
            }
         ],
         "Root": {
            "AppenderRef": [
               {
                  "ref": "file",
                  "level": "error"
               },
               {
                  "ref": "STDOUT",
                  "level": "debug"
               },
               {
                  "ref": "RollingFile"
               }
            ],
            "level": "trace"
         }
      }

   }
}

About Saurabh Gupta

My Name is Saurabh Gupta, I have approx. 10 Year of experience in Information Technology World manly in Java/J2EE. During this time I have worked with multiple organization with different client, so many technology, frameworks etc.
This entry was posted in Centralize logging, Java Logging, LOG4J2, Logstash and tagged , , , , . Bookmark the permalink.

One Response to Log4j2 JSON Configuration for Java Logging Severity Levels, Formatting and Appenders

  1. Pingback: Log4j2 XML Configuration for Java Logging Example Tutorial – , Severity Levels, Formatting and Appenders | Facing Issues On IT

Comments are closed.