How to format logs dynamically with Grafana

I use Loki and Grafana to show logs I receive from an application.

The logs have a field that uses variables and those variables are in other fields of the log. I want Grafana to re-format the message field so it appears without the placeholders in the dashboard and with the actual variable values!

Here is a clear example of what I am trying to achieve:

{ "timestamp": "ZZ:YY:XX", "message": "The user: {.User} has a code: {.Code}", "properties": { "User": "username123", "Code": "762" }}

Another log could be:

{ "timestamp": "ZZ:YY:XX", "message": "A new project: {.Project} has been named: {.Name}", "properties": { "Project": "SuperNiceProject", "Name": "ARandomName"}}

I want those logs to show in the dashboard as:

{ "timestamp": "ZZ:YY:XX", "message": "The user: username123 has a code: 762", "properties": { "User": "username123", "Code": "762" }}

{ "timestamp": "ZZ:YY:XX", "message": "A new project: SuperNiceProject has been named: ARandomName", "properties": { "Project": "SuperNiceProject", "Name": "ARandomName"}}

There are multiple ways to manipulate logs but I have not been able to find a way to do this “dynamically”, has anyone done something similar?

Try this:

{SELECTOR}
  | json
  | line_format `{{ .message | replace "{.User}" .properties_User | replace "{.Code}" .properties_Code }}`

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.