log4j2-json-layout


License

License

Categories

Categories

JSON Data
GroupId

GroupId

org.zalando
ArtifactId

ArtifactId

log4j2-json-layout
Last Version

Last Version

1.0.0-RC1
Release Date

Release Date

Type

Type

jar
Description

Description

log4j2-json-layout
Project URL

Project URL

https://github.com/lukasniemeier-zalando/log4j2-json-layout
Source Code Management

Source Code Management

https://github.com/lukasniemeier-zalando/log4j2-json-layout

Download log4j2-json-layout

How to add to project

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

Dependencies

compile (4)

Group / Artifact Type Version
org.jetbrains.kotlin : kotlin-stdlib-jdk8 jar 1.2.30
org.apache.logging.log4j : log4j-api jar 2.7
org.apache.logging.log4j : log4j-core jar 2.7
com.fasterxml.jackson.core : jackson-databind jar 2.8.10

test (3)

Group / Artifact Type Version
com.google.code.gson : gson jar 2.7
org.junit.jupiter : junit-jupiter-api jar 5.1.0
org.junit.jupiter : junit-jupiter-engine jar 5.1.0

Project Modules

There are no modules declared in this project.

Log4J 2 JSON Layout Plugin

Build Status Release Maven Central License

A Log4J 2 layout plugin rendering structured JSON log lines. The goal of this plugin is to offer a minimal, concise and ready-to-be-used JSON log layout.

{"time":"2018-03-21T17:50:20.868Z","severity":"INFO","logger":"MyLogger","message":"Hello World","thread":"main"}
{"time":"2018-03-21T18:00:46.175Z","severity":"WARN","logger":"MyLogger","message":"Huch","thrown":{"class":"java.lang.IllegalStateException","stack":"..."},"thread":"main"}

This plugin only supports Log4J 2.7. This is a perfect match in case you are using Spring Boot 1.

Usage

Use this layout by configuring the SimpleJsonLayout it on your appender of choice. Make sure to expose the plugin to your Log4J instance (see here).

<Configuration packages="koeln.niemeier.log4j2.json">
    <Appenders>
        <Console name="stdout">
            <SimpleJsonLayout/>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="stdout"/>
        </Root>
    </Loggers>
</Configuration>

Logging of Throwable

In case a Throwable is logged an additional object exception is rendered with the following fields.

  • thrown: the exception's class.
  • message: the exception's message (optional).
  • stack: the full stack trace serialized as an escaped string.
{
    "time": "2018-03-22T08:00:46.175Z"
    "logger": "logger",
    "severity": "WARN",
    "message": "Huch",
    "thread": "main",
    "exception": {
        "thrown": "java.lang.IllegalStateException",
        "message": "test",
        "stack":"java.lang.IllegalStateException: test\n\tat koeln.niemeier.log4j2.json.LoggingTest.testThrown(LoggingTest.kt:119) ~[classes/:?]\n\tat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_121]\n\tat sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_121]\n\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_121]\n\tat java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_121]\n\t... suppressed 46 lines\n\tat com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:65) [junit5-rt.jar:?]\n\tat com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) [junit-rt.jar:?]\n\tat com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) [junit-rt.jar:?]\n\tat com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) [junit-rt.jar:?]\n",
    }
}

Customization

Currently the following options are available:

  • ignoredStackTracePackages: a comma-separated list of packages to be ignored on rendering the stack trace.

Example XML configuration fragment showing all possible options:

<SimpleJsonLayout ignoredStackTracePackages="org.junit.,java.util.,org.gradle."/>

Alternatives

There are many alternatives which may fit your use case better.

As I did not get them to work with Log4J 2 easily, I took inspiration from all of them.

Building

This project uses Kotlin with Gradle.

    $ ./gradlew clean build

Versions

Version
1.0.0-RC1