18

I'm trying to remove newline from Java stacktraces.

I've following logback pattern -

<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %replace(%msg){'\n', ''}%n</pattern>

I expect it to replace newlines in messages but it is not doing so. I see stacktraces printed out with newlines.

However, if I use following pattern (for testing purpose only)-

<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %replace(%msg){'.*', 'x'}%n</pattern>

I find that messages are getting replaced with character x but stack traces are still getting printed as is.

This makes me believe that logback treats stacktraces indepdentantly. I've gone through the logback documentation it says that one can refer to stacktraes via (%ex).

But then, I can have only one pattern active at a time for the appender. How do I go about making sure that I don't get newline in stacktraces?

user375868
  • 1,288
  • 4
  • 21
  • 45

1 Answers1

28

After looking at how Logback formatter builds the string on my Windows box, I saw that the stack trace contains \r\n for each new line, so if you are using Windows, try this instead:

<pattern>........ %replace(%ex){'[\r\n]+', '\\n'}%nopex%n</pattern>

Explanations:

  • \r\n : At first I tried with only %replace(%ex){'\n', 'X'} but then I had in the logs the new line character then a 'X'. Instead, using %replace(%ex){'[\r\n]+', ''} removes all occurrences of \r and \n. If you want to display the string "\n" to indicate that a new line was here, you can add '\\n' in the %replace second parameter.

  • %nopex : I don't know why, but if you use %replace on %ex, Logback adds a second stack trace to your entry, this time with all the new lines. Using %nopex (see documentation), it will suppress this second stack by faking the use of an actual stack without any %replace.

xav
  • 5,452
  • 7
  • 48
  • 57
  • 1
    how can I replace new line AND also filter by evaluate clause e.g. dont include tomcat packages – Kalpesh Soni Jun 03 '19 at 20:15
  • @KalpeshSoni Not sure what you mean by _"evaluate clause"_ but if you want to disable the logs for a specific package/class, see https://stackoverflow.com/questions/47397442, and if you want to try to remove some text from the logs you can use `%replace` multiple times, including on the same log entry field (see https://stackoverflow.com/questions/51441574) – xav Jun 03 '19 at 23:40
  • not disable logs, but filter the stack trace https://stackoverflow.com/questions/35076096/how-to-filter-stacktrace-frames-in-logback – Kalpesh Soni Jun 04 '19 at 15:16
  • 1
    I think `%nopex` behavior somehow changed or it was actually a bug back then. When I try this with *logback-classic v1.2.3*, it does NOT print any *ERROR* logs at all. But when I remove the `%nopex` symbolic, it prints the same *ERROR* log twice (one with replaced newline characters, other with just as it is) – emrekgn Aug 04 '21 at 11:24
  • @emrekgn how did you fix the %nopex issue and prevent duplicate logs? – Vishakha Lall Sep 14 '21 at 11:20
  • 1
    @VishakhaLall I couldn't fix it. I'm pretty sure there is a bug in certain versions. Instead, I ended up using [logstash-logback-encoder](https://github.com/logstash/logstash-logback-encoder) to print a JSON-formatted, one-line log string. (My requirement was to configure logs for a log collector - fluentd - so that was the most painless solution for me.) – emrekgn Sep 15 '21 at 12:06