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

**URL:** <https://www.ruby-forum.com/t/how-to-use-log4r-to-generate-xml-format-log-file/147236>\
**Category:** Ruby\
**Created:** [August 29, 2008, 6:20pm UTC](https://www.ruby-forum.com/t/how-to-use-log4r-to-generate-xml-format-log-file/147236 "2008-08-29T18:20:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![oliviabean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/df705f/32.png) [@oliviabean](https://www.ruby-forum.com/u/oliviabean)\
**Post date:** [August 29, 2008, 6:20pm UTC](https://www.ruby-forum.com/t/how-to-use-log4r-to-generate-xml-format-log-file/147236/1 "2008-08-29T18:20:37Z")

</div>

Hi,  
I want to use log4r to generate xml formatted log file, but I’m not  
familiar with it and thus have no idea how to do.  
In my program, there’s some log output in each function, and I want  
the xml log file similar to this(there’s function stack here–Func1  
calls Func2 and Func2 calls Func3):  
  
log text1  
  
log text2  
  
log text3

Is it possible to do this?  
Thanks

---

<div class="post-metadata">

**Author:** ![oliviabean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/df705f/32.png) [@oliviabean](https://www.ruby-forum.com/u/oliviabean)\
**Post date:** [August 29, 2008, 8:36pm UTC](https://www.ruby-forum.com/t/how-to-use-log4r-to-generate-xml-format-log-file/147236/2 "2008-08-29T20:36:44Z")

</div>

Olivia D. wrote:

> Hi,  
> I want to use log4r to generate xml formatted log file, but I’m not  
> familiar with it and thus have no idea how to do.

Hi Olivia,

I’ve not done this either, but done quite a bit of ‘log4j’: seems the  
two packages are (superficially) similar, but there doesn’t appear to be  
a default XML appender (formatter I think in log4r?), but looks like the  
API is flexible enough to let you produce your own XML:

[http://log4r.sourceforge.net/rdoc/files/log4r/formatter/patternformatter\_rb.html](http://log4r.sourceforge.net/rdoc/files/log4r/formatter/patternformatter_rb.html)

…  
Pattern String  
…  
p = PatternFormatter.new(:pattern =\> “[%l] %d :: %.15m”)

You wanted your individual methods in there: not sure you can do that or  
not:

[http://log4r.sourceforge.net/rdoc/files/log4r/formatter/patternformatter\_rb.html](http://log4r.sourceforge.net/rdoc/files/log4r/formatter/patternformatter_rb.html)

…  
What the Directive Letters mean  
…  
Maybe this directive is close enough? (dunno)

t  
Produces the file and line number of the log event. The appearance  
varies by Ruby version, but it is the same output returned by  
Kernel#caller[0]. Slow

I know on ‘log4j’ it is NOT recommended to output Method calls : really  
grinds the program to almost halt.

Maybe this help, dunno,

John
