extended-jsonlayout

A customizable JSONLayout for Log4j2

License

License

Categories

Categories

JSON Data
GroupId

GroupId

org.laurell.log4j2
ArtifactId

ArtifactId

extended-jsonlayout
Last Version

Last Version

1.0.4
Release Date

Release Date

Type

Type

jar
Description

Description

extended-jsonlayout
A customizable JSONLayout for Log4j2
Project URL

Project URL

https://github.com/tomilaurell/log4j2-extended-jsonlayout
Source Code Management

Source Code Management

https://github.com/tomilaurell/log4j2-extended-jsonlayout

Download extended-jsonlayout

How to add to project

<!-- https://jarcasting.com/artifacts/org.laurell.log4j2/extended-jsonlayout/ -->
<dependency>
    <groupId>org.laurell.log4j2</groupId>
    <artifactId>extended-jsonlayout</artifactId>
    <version>1.0.4</version>
</dependency>
// https://jarcasting.com/artifacts/org.laurell.log4j2/extended-jsonlayout/
implementation 'org.laurell.log4j2:extended-jsonlayout:1.0.4'
// https://jarcasting.com/artifacts/org.laurell.log4j2/extended-jsonlayout/
implementation ("org.laurell.log4j2:extended-jsonlayout:1.0.4")
'org.laurell.log4j2:extended-jsonlayout:jar:1.0.4'
<dependency org="org.laurell.log4j2" name="extended-jsonlayout" rev="1.0.4">
  <artifact name="extended-jsonlayout" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.laurell.log4j2', module='extended-jsonlayout', version='1.0.4')
)
libraryDependencies += "org.laurell.log4j2" % "extended-jsonlayout" % "1.0.4"
[org.laurell.log4j2/extended-jsonlayout "1.0.4"]

Dependencies

compile (3)

Group / Artifact Type Version
org.apache.logging.log4j : log4j-api jar 2.11.2
org.apache.logging.log4j : log4j-core jar 2.11.2
com.fasterxml.jackson.core : jackson-databind jar 2.9.9

test (1)

Group / Artifact Type Version
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

log4j2-extended-jsonlayout

Customizable JSONLayout for Log4j2

Include the artifact

<dependency>
    <groupId>org.laurell.log4j2</groupId>
    <artifactId>extended-jsonlayout</artifactId>
    <version>1.0.4</version>
</dependency>

Example Usage

Inside your log4j2 configuration, you can configure the new pattern "ExtendedJsonLayout"

<Appenders>
    <Console name="STDOUT" target="SYSTEM_OUT">
        <ExtendedJsonLayout complete="false" properties="true"/>
    </Console>
</Appenders>
	

The default ExtendedJson implementation Produces output like below -

{
  "instant" : {
    "epochSecond" : 1556795579,
    "nanoOfSecond" : 277000000
  },
  "thread" : "main",
  "level" : "INFO",
  "loggerName" : "org.hibernate.validator.internal.xml.ValidationXmlParser",
  "message" : "HV000007: META-INF/validation.xml found. Parsing XML based configuration.",
  "endOfBatch" : false,
  "loggerFqcn" : "org.hibernate.validator.internal.util.logging.Log_$logger",
  "contextMap" : { },
  "threadId" : 1,
  "threadPriority" : 5,
  "hostname" : "MY-LAPTOP"
}

Add custom Json Fields

Implement the ExtendedJson interface
And configure the class name in your log configuration -

<ExtendedJsonLayout complete="false" properties="true" 
            jsonAdapterClassName="com.example.MyCustomJsonLogger">
</ExtendedJsonLayout>

Example Custom Implementation

package com.example;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.layout.ExtendedJson;

public class MyCustomJsonLogger implements ExtendedJson{
	
	HashMap<String, Object> mixedIn = new HashMap<>();
	Random r = new Random();
	
	public MyCustomJsonLogger() {
		mixedIn.put("myCustomKey", "myCustomValue");
		mixedIn.put("anotherKey", r.nextInt(200));
	}

	@Override
	public Map<String, Object> getMixedFields(LogEvent logEvent) {
		return mixedIn;
	}

}

Each item in the Map<String,Object> are added to the root logging message so that it appears like this -

{
  "instant" : {
      "epochSecond" : 1556795579,
      "nanoOfSecond" : 277000000
  },
  "thread" : "main",
  "level" : "INFO",
  "loggerName" : "org.springframework.data.repository.config.RepositoryConfigurationDelegate",
  "message" : "Multiple Spring Data modules found, entering strict repository configuration mode!",
  "endOfBatch" : false,
  "loggerFqcn" : "org.apache.logging.slf4j.Log4jLogger",
  "contextMap" : { },
  "threadId" : 1,
  "threadPriority" : 5,
  "anotherKey" : 36,
  "myCustomKey" : "myCustomValue"
}

Versions

Version
1.0.4
1.0.3