Custom classes for use with Logback
Add through the JitPack repo to your project:
repositories {
exclusiveContent {
forRepository { maven { url "https://jitpack.io"} }
filter { includeGroup "com.github.apatchydev" }
}
}
dependencies {
implementation "com.github.apatchydev:logback-extra:1.0.0"
}
- Java 11 or higher
- Java 8 compatible if
build.gradle
is modified tosourceCompatibility = 8
and use logback 1.3.+
- Java 8 compatible if
Define custom conversion rules in logback.xml
as follows:
<configuration>
<conversionRule conversionWord="specifier-without-percent" converterClass="fully.qualified.class.name"/>
</configuration>
Code for examples below are available in the sample program
- Execute example program with
./gradlew example
A converter for PatternLayout to group all key-value pairs into a JSON object in order of addition,
falling back to Map::toString if JSON-stringify fails.
Accepts 1 argument pretty
to enable pretty-printing of JSON object.
pattern = "[%msg | %kvp]%n";
data1 = {"text": "abc", "object": null};
data2 = {"text" : "def", "object" : [ 1, 2, 3 ]};
log.atInfo()
.addKeyValue("data1", data1)
.addKeyValue("data2", data2)
.log("This log writes key-value pairs as JSON");
log.atInfo()
.log("This log has no key-value entries");
// ...
pattern = "[%msg | %kvp{pretty}]%n";
log.atInfo()
.addKeyValue("data1", data1)
.addKeyValue("data2", data2)
.log("This log writes key-value pairs as JSON with pretty print");
log.atInfo()
.log("This log has no key-value entries");
Result
[This log writes key-value pairs as JSON | {"data1":{"text":"abc","object":null},"data2":{"text":"def","object":[1,2,3]}}]
[This log has no key-value entries | ]
// ...
[This log writes key-value pairs as JSON with pretty print | {
"data1" : {
"text" : "abc",
"object" : null
},
"data2" : {
"text" : "def",
"object" : [ 1, 2, 3 ]
}
}]
[This log has no key-value entries | ]
A composite converter for PatternLayout that adds a prefix/suffix when input is not empty, otherwise put a placeholder.
Arguments are ordered: {prefix,suffix,placeholder}
with each defaulting to ch.qos.logback.core.CoreConstants.EMPTY_STRING
pattern = "[%if(%marker){<,'> '}%msg]%n";
log.atInfo()
.addMarker(MarkerFactory.getMarker("someMarker"))
.log("This log prepends the log message with '<marker> ' only if present");
log.atInfo()
.log("This log has no markers");
Result
[<someMarker> This log prepends the log message with '<marker> ' only if present]
[This log has no markers]