Are log files not meant to be read by machines but by users only? I wonder if there are file appenders for any logging framework that write their output to XML.
1 Answers
"Logging to XML" is quite a general requirement, because there is no such thing as the standard log file format. But since XML files are text files, which logging frameworks can write, and since many of those frameworks allow configuring log line format, I see no problem in defining your log output with XML tags of choice.
For log4j, it might be something like this:
log4j.appender.A1.layout.ConversionPattern=<line>%n<date>%d</date>%n<threadName>%t</threadName>%n<level>%p</level>%n<logger>%c</logger>%n<text>%m</text>%n</line>%n
yielding example output:
<line>
<date>2011-08-28 08:27:33,727</date>
<threadName>main</threadName>
<level>INFO</level>
<logger>com.log4jeval.Main</logger>
<text>Entering application.</text>
</line>
This looks quite like XML, doesn't it? It will lack XML preamble, though, so technically it won't be valid. If it's critical that it is, I recommend writing a custom appender extending org.apache.log4j.FileAppender
(or any of its subclasses) that would handle any additional opening/closing text in every log file.
The problem with writing logs to XML, that does not exist in plain text files, is that you have to enforce that not one possible log statement would print XML forbidden characters, otherwise you'll end up with a non well-formed XML. That's hard to achieve without writing a custom appender — see org.apache.log4j.HTMLAppender
sources for example.

- 10,511
- 9
- 46
- 84
-
Is there an existing appender which does write the output as XML? – the_drow Aug 28 '11 at 06:51
-
@the_drow: Not in Log4j 1.2.x. The closest I can think of is HTMLAppender, but still, it's not XML. – MaDa Aug 28 '11 at 07:34
-
@MaDa Thanks for the hint, extending the HTMLAppender might be a good way. I ended up writing some custom code to collect log data, serialize it to xml and transform it to any output format I need – mamuesstack Aug 29 '11 at 14:09
-
@mamuesstack Hmmm, I would be careful with extending, I was thinking more of being inspired by it. It contains a lot of hard-coded formatting directives (i.e. colors, font sizes) to be used as the base for an XML formatter. – MaDa Aug 29 '11 at 14:22