Your log is not complex, it looks like a string followed by a json document, but it is not a valid json because the data
is not inside double quotes, is this a typo or is this how it looks in your message?
Considering that this is how it looks, you can transform it in a valid json and use dissect and the json filter to parse it.
For example, using this as sample messages:
INFO {"datetime": "2021-06-1 22:13:29.469000", data:{"val1":3.14, "val2": 2.17}}
INFO {"datetime": "2021-06-1 21:14:00.469000", data:{"val3":9}}
The following filter will parse those messages:
filter {
mutate {
gsub => ["message", 'data:', '"data":']
}
dissect {
mapping => {
"message" => "%{logLevel} %{jsonData}"
}
}
json {
source => "jsonData"
}
}
The mutate
filter with gsub
will transform the json part in a valid json, that can be parsed with the json
filter.
The dissect
filter will split your message in two, the first parte will have the log level information, in this case INFO
, and the second will have the json data.
The json
filter will parse your json, so the end result will be something like this:
{
"logLevel": "INFO",
"@timestamp": "2021-06-02T15:06:34.822Z",
"datetime": "2021-06-1 22:13:29.469000",
"data": {
"val1": 3.14,
"val2": 2.17
},
"jsonData": "{\"datetime\": \"2021-06-1 22:13:29.469000\", \"data\":{\"val1\":3.14, \"val2\": 2.17}}",
"message": "INFO {\"datetime\": \"2021-06-1 22:13:29.469000\", \"data\":{\"val1\":3.14, \"val2\": 2.17}}"
}
{
"logLevel": "INFO",
"@timestamp": "2021-06-02T15:06:34.883Z",
"datetime": "2021-06-1 21:14:00.469000",
"data": {
"val3": 9
},
"jsonData": "{\"datetime\": \"2021-06-1 21:14:00.469000\", \"data\":{\"val3\":9}}",
"message": "INFO {\"datetime\": \"2021-06-1 21:14:00.469000\", \"data\":{\"val3\":9}}"
}