Display stacktrace in grafana log browser

Hello,
I’m trying to display properly java stacktrace in grafana log/loki explorer.

What I do :
My logs are JSON line like this :

{"time":"2021-08-19T14:07:56.802Z","level":"ERROR","message":"Bla bla bla \n More Bla bla", "exception":{"class":"some.java.class","message":"This is an error","stacktrace":"stack line 1\n\t at stack line 2\n\t at stack line 3\n\t at stack line 4\n\t ..."}}

My loki query is like this :
{container="api"} | json | line_format "{{.message}}"

What I have :

  1. the json is correctly returned by the server and contains all the \n
  2. the json is parsed properly and I have the exra labels “time”, “level”, “message”, “execption_message” and “exception_stacktrace” parsed properly.
  3. but all the \n in message, exception_stacktrace are removed and ignored in display.
  4. when I toggle “Wrap lines”, it only wrap long lines and doesn’t care about my newlines
  5. as a consequence, my stack trace is unreadable and appears like this : "stack line 1 at stack line 2 at stack line 3 at stack line 4 ..." with wraps when en of screen is reached.

What I expect:

  • I want the \n to be replaced by newlines so that the stacktraces and messages apear as they are meant to when linewrap is unabled.

I read there is a “proper way” to escape newline but I didn’t find any documentation on this.
Can one help ?
Thanks

I’m running

  • grafana 8.1.1, Loki 2.
  • Springboot 2.2.10
  • Log4J with JsonTemplateLayout for log formating

– EDIT –
I found adding white-space: pre-wrap to wordBreakAll css fixes the formating and all \n appears as new line as expected.
I also found there is a wrapLogMessage property that is suppose to set the css above.
however, I found nowhere this wrapLogMessage can be set to true.
any hint ?

Hi @pdelorme

Glad you got unblocked on this.

On 8.1.2 I see the wrap lines feature right here in Explore:

You can also set wrapLogMessage as an option in the Logs Panel. You’ll see the option in your dashboard JSON like so:

      "datasource": "LokiNginxLogs",
      "gridPos": {
        "h": 8,
        "w": 12,
        "x": 0,
        "y": 0
      },
      "id": 6,
      "options": {
        "dedupStrategy": "none",
        "enableLogDetails": true,
        "prettifyLogMessage": false,
        "showCommonLabels": false,
        "showLabels": false,
        "showTime": false,
        "sortOrder": "Descending",
        "wrapLogMessage": true
      },

Well, I am still blocked.
The button and the option wrap lines based on line length and doesn’t care about newlines (/n) actually present in the message.
However when I play with css in chrom debugger, the newline can be taken into account.
To me it is a bug in grafana ! unless I’m missing something.

to be clearer, I’d like message like hello\nworld to be displayed like this:

hello
world

thanks.

2 Likes

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