3

I am trying to remove all white spaces and new lines from the application logs.Is there any way I can remove all next lines from the appending logs using logback pattern?

PraveenG
  • 35
  • 1
  • 7
  • Which patterns have you tried so far? Please post the pattern and the resulting output. And then an explanation what you expected. – JanTheGun Nov 23 '17 at 11:21
  • Have you tried modifying in the pattern `%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n` This link will help you for modifying the pattern. https://logback.qos.ch/manual/layouts.html – Arvind Katte Nov 23 '17 at 11:35
  • If one of the answers/comments was helpful for you, please upvote it and/or mark it as "Accepted", to let other people know that it worked for you, it will help them :) (see https://stackoverflow.com/help/someone-answers) – xav Nov 24 '17 at 15:27

1 Answers1

6

Have you tried with %replace? For example:

<pattern>%d [%thread] %level %logger %replace(%msg){'[\s\n\r]',''}%n</pattern>

The above pattern will remove all spaces and new lines contained in the log entry message.

You can also remove spaces and new lines from multiple log entry fields like this:

<pattern>%d [%thread] %level %logger %replace(%logger %msg){'[\s\n\r]',''}%n</pattern>

See https://logback.qos.ch/manual/layouts.html#replace

Also, to remove new lines from stacktraces, please see How do I remove newline from Java stacktraces in logback?

xav
  • 5,452
  • 7
  • 48
  • 57
  • If you are mitigating CRLF vulnerabilities, then try coalescing all white space to a single space `%d [%thread] %level %logger %replace(%msg){'[\s\n\r]+',' '}%n` – Iain Henderson Aug 19 '19 at 17:01