10

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?

palacsint
  • 28,416
  • 10
  • 82
  • 109
Krystian
  • 3,193
  • 2
  • 33
  • 71

1 Answers1

16

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));
Community
  • 1
  • 1
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • Well this will simply create 2 lines. One with id and other with xml. That's not really a pretty print ;-) – Krystian Feb 25 '12 at 17:03
  • @Krystian: sorry, I misunderstood you. Have a look at my updated answer. – Tomasz Nurkiewicz Feb 25 '12 at 17:28
  • Uhh.. I've made a fatal mistake. I knew that in response I get a pretty printed XML, however it seems that reader I used to get the response was removing all the newline characters. It wasn't actually logging issue. Once I've changed the code to pass original response to the logger everything started to work fine. I have marked your answer as the correct one because for people who get a raw xml it will work just fine. Thanks. – Krystian Feb 25 '12 at 18:02