Java Exception and new line

Hi Everyone

I’ve been searching for an answer to multi-line Java stack traces in GrayLog and I have not been able to find a workable solution to my problem. I’ve updated log4j2 in my Spring Boot app to insert a \n character instead of a new line in my stack traces. With this, I get one message logged in GrayLog but the message is displayed as one big blob of text, this is what is displayed in the message field:

tomcat1.test - 2018-01-30 18:30:05.406 ERROR 1 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[.[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.util.NoSuchElementException: No value present] with root cause java.util.NoSuchElementException: No value present\n at java.util.Optional.get( ~[?:1.8.0_151]\n

How do I configure GrayLog to replace the \n in the above text with a <br> when displaying the stack trace?

Thanks for your help.


I personal would change the log shipper to something that can work with multilines …

You could also just use a proper GELF appender for Log4j 2 which will ship the stack traces completely and as a single log message to Graylog:

Thanks @jochen I’ve looked at that and I think I’ll git it a try. One thing I didn’t mention is my Spring Boot app is running inside a Docker container. Using Docker’s gelf log-driver, I get some Docker details in addition to my application’s log data. Perhaps the Docker data isn’t that useful, I’ll give the Log4j 2 appender a try.

Thanks again for your feedback.

The problem with the Docker logging drivers is that they’re line-based and do not support multiline logs.

Relate GitHub issue:

Thanks for the update @jochen, my apologies on the delayed response, I was on vacation. I like the suggestion of replacing \n with \r as described in the related issue you linked to this ticket.

In case anyone else is dealing with this, I’ve switched to use the GELF appender for log4j2 (there’s one for logback as well) and everything is working well. There’s some additional Docker data that is missing but I’m not sure if the docker container or related information was that useful in the end. What I really cared about is getting my log statements in GrayLog AND having stack traces show up as a one log event. Using the gelf appender with log4j2 does exactly what i needed.

Thanks for your help @jochen.

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