Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log multiline text in Logback/SLF4J?

I want to log "pretty printed" XML using Logback/SLF4J. Right now, what I get in logs is totally unreadable and I have to open something to parse it. I want to be able to configure logging for debug (because I want to see XML only in debug) to output XML in a human readable way.

Is it possible?

like image 876
Krystian Avatar asked Feb 25 '12 12:02

Krystian


People also ask

What is multi line logging?

Multiline processing is used to ensure a log message that is made up of multiple lines, separated by a line break or carriage return, are properly grouped as a single log message when ingested into Sumo Logic. Multiline processing requires your logs to have line breaks or carriage returns between messages.

Does SLF4J support Logback?

SLF4J supports popular logging frameworks, namely log4j, java. util. logging, Simple logging and NOP. The logback project supports SLF4J natively.

Is SLF4J same as Logback?

Logback and SLF4J can be primarily classified as "Log Management" tools. According to the StackShare community, Logback has a broader approval, being mentioned in 4 company stacks & 9 developers stacks; compared to SLF4J, which is listed in 5 company stacks and 7 developer stacks.

What is the default logging level in SLF4J?

Developers just need to include the logging framework dependency in the classpath and SLF4J will utilize it to append the logs. If no logging dependency is provided on the classpath, it will default to a no-operation implementation and nothing gets logged.


1 Answers

Simply add a newline \n in log statement:

log.info("Message id: {}\nContents: {}", id, xml);

UPDATE: In order to pretty-print XML have a look at: How to pretty print XML from Java?. One thing to keep in mind, there is no need to perform costly formatting if the XML is not going to be actually printed. Therefore this is one of the rare cases where is*Enabled() should be used:

if(log.isInfoEnabled())
  log.info("Message: {}", prettyFormat(xml));
like image 57
Tomasz Nurkiewicz Avatar answered Sep 26 '22 08:09

Tomasz Nurkiewicz