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>
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)"
]
}
]
}