I want to send log events to Loggly as JSON objects with parameterized string messages. Our project currently has a lot of code that looks like this:
String someParameter = "1234";
logger.log("This is a log message with a parameter {}", someParameter);
We're currently using Logback as our SLF4J backend, and Logback's JsonLayout to serialize our ILogEvent objects into JSON. Consequentially, by they time our log events are shipped to Loggly, they look like this:
{
"message": "This is a log message with a parameter 1234",
"level": INFO,
....
}
While this does work, it sends a different message
string for every value of someParameter
, which renders Loggly's automatic filters next to useless.
Instead, I'd like to have a Layout that creates JSON that looks like this:
{
"message": "This is a log message with a parameter {}",
"level": INFO,
"parameters": [
"1234"
]
}
This format would allow Loggly to group all log events with the message This is a log message with a parameter
together, regardless of the value of someParameter
.
It looks like Logstash's KV filter does something like this - is there any way to accomplish this task with Logback, short of writing my own layout that performs custom serialization of the ILogEvent object?
You could use a Mapped Diagnostic Context to set a stamp for each of those type of log messages that you could then filter on once in loggly.
According to the source of JsonLayout the stamp is stored as a separate value in the JSON.
So for me I was trying to log execution times, I created a pojo called ExecutionTime with name, method, class, duration.
I was then able to create it:
For logging I then used:
Make sure you have:
This will log something like this:
Might be a different set up as I was using logback-spring.xml to output my logs to json:
There is a JSON logstash encoder for Logback, logstash-logback-encoder
Here's a recently created project that provides a JSON-specific logging API and works with SLF4J:
https://github.com/savoirtech/slf4j-json-logger
Like already answered you'll get a one-dimensional JSON tree with MDC and/or using a Marker with logstash-logback-encoder.
If you are also looking for the following:
then try a project I've created: json-log-domain. It defines a simple YAML-format definition from which the above can be generated.
An example helper-code statement would be
while generated markdown would like something like this.