Gson Log4J2 JSON Layout

Log4J2 JSON Layout using Google Gson

License

License

Categories

Categories

Gson Data JSON
GroupId

GroupId

com.github.mvh77
ArtifactId

ArtifactId

gson-log4j2-layout
Last Version

Last Version

1.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

Gson Log4J2 JSON Layout
Log4J2 JSON Layout using Google Gson
Project URL

Project URL

http://github.com/mvh77/gson-log4j2-layout
Source Code Management

Source Code Management

http://github.com/mvh77/gson-log4j2-layout

Download gson-log4j2-layout

How to add to project

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

Dependencies

compile (2)

Group / Artifact Type Version
com.google.code.gson : gson jar 2.8.5
org.apache.logging.log4j : log4j-core jar 2.11.1

Project Modules

There are no modules declared in this project.

Gson Log4J2 JSON Layout

Log4J2 JSON layout generated and sanitized using Gson. This library has dependencies on gson and log4j-core.

Import from Maven Central:

pom.xml
<dependency>
    <groupId>com.github.mvh77</groupId>
    <artifactId>gson-log4j2-layout</artifactId>
    <version>1.1.0</version>
</dependency>

Use it in your log4j2.xml file with <GsonJsonLayout/> and be sure to add packages="com.github.mvh77.log4j2layout" to the configuration.

log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info" name="spring-boot-kafka-log"
                             packages="com.github.mvh77.log4j2layout">
    <Appenders>
        <Kafka name="Kafka" topic="system.logging" key="test-app">
            <GsonJsonLayout/>
            <Property name="bootstrap.servers">kafka-1:9092</Property>
        </Kafka>
        <Async name="Async">
            <AppenderRef ref="Kafka"/>
        </Async>

        <Console name="stdout" target="SYSTEM_OUT">
            <GsonJsonLayout pretty="true"/>
        </Console>

    </Appenders>
    <Loggers>
        <Root level="INFO">
            <AppenderRef ref="Kafka"/>
            <AppenderRef ref="stdout"/>
        </Root>
        <!-- MUST BE INFO OR GREATER TO PREVENT RECURSIVE LOGGING -->
        <Logger name="org.apache.kafka" level="WARN" />
    </Loggers>
</Configuration>
Table 1. Layout Configuration
Attribute Default Description

charset

UTF-8

Charset to use (probably always UTF-8)

pretty

false

Pretty printed JSON

json-stacktrace

false

Format stack traces as JSON

max-stack-size

20

Maximum number of nested exceptions

logger-pattern

none

The conversion pattern for the name of the logger value, for example '1.' would rename the logger from org.apache.commons.Foo to o.a.c.Foo. See Patterns in PatternLayout.

Example JSON that has an exception with a root cause. Implemented with Elastic Stack in mind (@timestamp).

Example log
{
  "@timestamp": "2018-12-07T11:15:33.350Z",
  "thread": "main",
  "level": "WARN",
  "logger": "hello.Application",
  "message": "here\u0027s an exception",
  "stack-trace": [
    {
      "exception": "java.lang.RuntimeException",
      "message": "test",
      "frames": [
        "at hello.Application.run(Application.java:31)",
        "at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:813)",
        "at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:797)",
        "at org.springframework.boot.SpringApplication.run(SpringApplication.java:324)",
        "at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)",
        "at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)",
        "at hello.Application.main(Application.java:24)"
      ]
    },
    {
      "caused by": "java.lang.IllegalStateException",
      "message": "root cause",
      "frames": [
        "at hello.Application.run(Application.java:31)",
        "at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:813)",
        "at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:797)",
        "at org.springframework.boot.SpringApplication.run(SpringApplication.java:324)",
        "at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)",
        "at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)",
        "at hello.Application.main(Application.java:24)"
      ]
    }
  ]
}

Versions

Version
1.1.0
1.0.0