sebastian
sebastian

Reputation: 2348

Grafana Loki pretty print json log

I have an app logging json output. Grafana displays the log entries in a single line json format. How can I get grafana to display it in a pretty print type of format instead?

Upvotes: 2

Views: 9920

Answers (1)

In Grafana 8 you can use the "Prettify JSON" option in the "Explore" screen or in the "Logs" panel of a dashboard:

enter image description here

If you don't have the Grafana 8 yet you can format the log line using the "line_format" expression, like in the following example:

{filename="/var/log/nginx/xxxxx-access.log"} | json | line_format "{\n  \"remote_addr\": \"{{.remote_addr}}\",\n  \"request\": \"{{.request}}\"\n}"

Which will produce the following output:

{
  "remote_addr": "10.210.55.125",
  "request": "GET /api/prometheus/metrics HTTP/1.1"
}
{
  "remote_addr": "192.168.120.20",
  "request": "GET /api/developers/search_events?projects=br.com.xxxxx%3Ayyyyy-zzzzz HTTP/1.1"
}

Upvotes: 1

Related Questions