raw Json Field
Adds a log field (structured key-value data) to the log, with the given pre-serialized JSON value.
When outputting logs as JSON, this becomes a field in the logged JSON object (see example below). This allows you to filter and query on the field in the log analysis tool of your choice, in a more structured manner than if you were to just use string concatenation.
By default, this function checks that the given JSON string is actually valid JSON. The reason for this is that giving raw JSON to our log encoder when it is not in fact valid JSON can break our logs. So if the given JSON string is not valid JSON, we escape it as a string. If you are 100% sure that the given JSON string is valid and you want to skip this check, you can set validJson to true.
Example
import dev.hermannm.devlog.getLogger
private val log = getLogger()
fun example() {
val eventJson = """{"id":1001,"type":"ORDER_PLACED"}"""
log.info {
rawJsonField("event", eventJson)
"Processing event"
}
}
This gives the following output (using logstash-logback-encoder
):
{
"message": "Processing event",
"event": {"id":1001,"type":"ORDER_PLACED"},
// ...timestamp etc.
}
Parameters
Set this true if you are 100% sure that json is valid JSON, and you want to save the performance cost of validating it.