Logback JSON Formatter using Google GSON library
Alternative Logback JSON Formatter which can be used as a replacement for Logback JSON.
Use this library if you cannot or do not want to use logbacks jackson extension (logback-ext-jackson (https://github.com/qos-ch/logback-contrib/wiki/Jackson) for JSON formatting (e.g. due to classpath conflicts, GSON is already in use and you don't want to add additional libraries to you classpath).
To use the formatter add the Logback JSON libraries (https://github.com/qos-ch/logback-contrib/wiki/JSON) to your classpath. Except for logback-ext-jackson add this library.
JAR | Maven Dependency |
---|---|
logback-ext-json-core-version.jar |
pulled automatically when logack-ext-json-classic is added |
logback-ext-json-classic-version.jar |
<dependency> <groupId>ch.qos.logback.extensions</groupId> <artifactId>logback-ext-json-classic</artifactId> <version>version</version> <scope>runtime</scope> </dependency> |
logback-gson-formatter-version.jar |
<dependency> <groupId>de.gessnerfl.logback</groupId> <artifactId>logback-gson-formatter</artifactId> <version>version</version> <scope>runtime</scope> </dependency> |
gson-version.jar |
pulled automatically when logback-gson-formatter is added |
<appender name="file" class="ch.qos.Logback.core.FileAppender">
<file>my-log-file.json</file>
<encoder class="ch.qos.Logback.core.encoder.LayoutWrappingEncoder">
<layout class="ch.qos.Logback.contrib.json.classic.JsonLayout">
<jsonFormatter class="de.gessnerfl.logback.gson.GsonFormatter"/>
</layout>
</encoder>
</appender>
or if you prefer pretty print
<appender name="file" class="ch.qos.Logback.core.FileAppender">
<file>my-log-file.json</file>
<encoder class="ch.qos.Logback.core.encoder.LayoutWrappingEncoder">
<layout class="ch.qos.Logback.contrib.json.classic.JsonLayout" appendLineSeparator="true">
<jsonFormatter class="de.gessnerfl.logback.gson.GsonFormatter" prettyPrint="true"/>
</layout>
</encoder>
</appender>