Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4j 2 JSON Configuration

I have a configuration in XML that I would like to convert to JSON. The JSON version is not being loaded by Log4j and I cannot find any typos. My test code simply logs an ERROR level and a DEBUG level message. Only ERROR messages are being displayed and no file output is being generated - I'm assuming the framework falls back to the default initialization instead of the JSON file.

Note: The log4j2-test.json file is on the classpath.

I'm using apache-log4j-2.0-beta9 binary found here.

The XML configuration is the following:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration name="Test">
    <Properties>
        <Property name="Directory">${sys:user.home}/logs</Property>
        <Property name="Filename">test.log</Property>
    </Properties>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
        <RollingFile name="File" 
            fileName="${Directory}/${Filename}" 
            filePattern="${Directory}/${date:yyyy-MM}/test-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout>
                <pattern>%d %p %logger{36} [%t] %m%n</pattern>
            </PatternLayout>
            <Policies>
                <SizeBasedTriggeringPolicy size="1 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="10"/>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Root level="debug">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="File"/>
        </Root>
    </Loggers>
</Configuration>

and the JSON configuration is:

{
   "configuration": {
      "name": "Default",
      "properties": {
         "property": {
            "name":"Directory",
            "value":"${sys:user.home}/logs"
         },
         "property": {
            "name":"FileName",
            "value":"test.log"
         }
      },
      "appenders": {
         "Console": {
            "name":"Console",
            "target":"SYSTEM_OUT",
            "PatternLayout": {
               "pattern":"%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"
            }
         },
         "RollingFile": {
            "name":"File",
            "fileName":"${Directory}/${FileName}",
            "filePattern":"${Directory}/${date:yyyy-MM}/test-%d{MM-dd-yyyy}-%i.log.gz",
            "PatternLayout": {
               "pattern":"%d %p %logger{36} [%t] %m%n"
            },
            "Policies": {
               "SizeBasedTriggeringPolicy": {
                  "size":"1 MB"
               }
            },
            "DefaultRolloverStrategy": {
               "max":"10"
            }
         }
      },
      "loggers": {
         "root": {
            "level":"debug",
            "appender-ref": {
               "ref":"Console"
            },
            "appender-ref": {
              "ref":"File"
            }
         }
      }
   }
}
like image 374
bblincoe Avatar asked Jan 29 '14 15:01

bblincoe


People also ask

Does log4j support JSON format?

Conclusion. We've seen here how we can easily configure Log4j2 and Logback have a JSON output format. We've delegated all the complexity of the parsing to the logging library, so we don't need to change any existing logger calls.

What is configuration status in log4j2?

Configuration: the root element of a log4j2 configuration file; the status attribute represents the level at which internal log4j events should be logged. Appenders: this element contains a list of appenders; in our example, an appender corresponding to the System console is defined.

Where is log4j configuration file?

The file is named log4j. properties and is located in the $DGRAPH_HOME/dgraph-hdfs-agent/lib directory. The file defines the ROLLINGFILE appenders for the root logger and also sets the log level for the file.

What is pattern layout in log4j2?

The PatternLayout class extends the abstract org. apache. log4j. Layout class and overrides the format() method to structure the logging information according to a supplied pattern.


1 Answers

I found a solution to the problem.

It turns out that the Log4j 2 Configuration doesn't document all the required dependencies:

The JSON support uses the Jackson Data Processor to parse the JSON files. These dependencies must be added to a project that wants to use JSON for configuration:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.8.7</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.8.7</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.8.7</version>
</dependency>
like image 147
bblincoe Avatar answered Sep 20 '22 23:09

bblincoe