How to write multiline text in Logback / SLF4J?

I want to write "pretty printed" XML using Logback / SLF4J. Right now, what I get in the magazines is completely unreadable, and I have to open something to take it apart. I want to be able to configure logging for debugging (because I want to see XML only in debugging) to output XML in a user-friendly way.

Is it possible?

+6
source share
1 answer

Just add a new line \n to the log statement:

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

UPDATE: in order to print XML, see: How is it pretty to print XML with Java? . One thing to keep in mind is that there is no need to do expensive formatting unless XML is actually printed. Therefore, this is one of the rare cases when you should use is*Enabled() :

 if(log.isInfoEnabled()) log.info("Message: {}", prettyFormat(xml)); 
+9
source

Source: https://habr.com/ru/post/909338/


All Articles