# Re: How to use log4r to generate xml format log file?

**URL:** <https://www.ruby-forum.com/t/re-how-to-use-log4r-to-generate-xml-format-log-file/147261>\
**Category:** Ruby\
**Created:** [August 29, 2008, 10:31pm UTC](https://www.ruby-forum.com/t/re-how-to-use-log4r-to-generate-xml-format-log-file/147261 "2008-08-29T22:31:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [August 29, 2008, 10:31pm UTC](https://www.ruby-forum.com/t/re-how-to-use-log4r-to-generate-xml-format-log-file/147261/1 "2008-08-29T22:31:57Z")

</div>

> I want to use log4r to generate xml formatted log file

Aside: is there a standard way of using XML for log files? I had a need  
for  
writing structured data to a log a while ago, and considered XML, but  
rejected it on the grounds that anything which appends is always going  
to  
generate invalid (i.e. not-well-formed) XML. For example:

 foo bar 

This is malformed because it has more than one root node, and therefore  
will  
be rejected by just about any XML parser. Similarly,

 foo bar 

is also malformed, until such time that you append the closing  
  
tag (but at that point you can no longer append any more logs to that  
file).

Regards,

Brian.

---

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [August 30, 2008, 12:49am UTC](https://www.ruby-forum.com/t/re-how-to-use-log4r-to-generate-xml-format-log-file/147261/2 "2008-08-30T00:49:18Z")

</div>

That’s a good point. Nevertheless log4j does offer an XMLAppender.

It logs in ‘XMLfragments’ though - its up to you to complete the XML doc  
when you come to parse the log though.

The Jabber chat/messaging (xml-router in fact) has the same issue: it  
also sends information back and forth as XML-frags - you have to put  
them back together at some point to make valid XML.

Brian C. wrote:

> > I want to use log4r to generate xml formatted log file
> 
> Aside: is there a standard way of using XML for log files? I had a need  
> for  
> writing structured data to a log a while ago, and considered XML, but  
> rejected it on the grounds that anything which appends is always going  
> to  
> generate invalid (i.e. not-well-formed) XML. For example:
> 
> foo bar 
> 
> This is malformed because it has more than one root node, and therefore  
> will  
> be rejected by just about any XML parser. Similarly,
> 
> foo bar 
> 
> is also malformed, until such time that you append the closing  
>   
> tag (but at that point you can no longer append any more logs to that  
> file).
> 
> Regards,
> 
> Brian.

---

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [August 30, 2008, 9:33pm UTC](https://www.ruby-forum.com/t/re-how-to-use-log4r-to-generate-xml-format-log-file/147261/3 "2008-08-30T21:33:12Z")

</div>

> That’s a good point. Nevertheless log4j does offer an XMLAppender.
> 
> It logs in ‘XMLfragments’ though - its up to you to complete the XML doc  
> when you come to parse the log though.

Does that mean it just appends fragments without a root element? So to  
parse  
it, you have to prepend a and append a first?

> The Jabber chat/messaging (xml-router in fact) has the same issue

Not exactly: it sends a root element, and because it’s a stream, the  
receiver blocks waiting for more data.

 ... ... \<---- at this point a stream parser waits for either the closing or another 

When the other side wants to close the connection it sends   
then  
disconnects.

So taking the entirety of the stream, from when it starts to when it is  
finished, it is valid XML and it can be parsed using a normal parser.

Regards,

Brian.
