Log messages from different packages to different files

Suppose I have these packages in my application - foo.bar and foo.foobar. And I want to send all the log4j log messages that come from the foo.bar package to the foobar.log file, and the log messages coming from foo. foobar to foofoobar.log file, how do I configure the log4j.xml file?

+4
source share
3 answers

You can use appender-ref in the logger configuration:

<logger name="foo.bar"> <level value="debug"/> <appender-ref ref="FILE1" /> </logger> 

Look here for complete examples.

+5
source

I suggest also setting additivty to false when you overlap hierarchies and don't want to duplicate log messages.

i.e:

 <logger name="foo.bar"> <level value="debug"/> <appender-ref ref="FILE1" /> </logger> <logger name="foo.bar.xyz" additivity="false"> <level value="debug"/> <appender-ref ref="FILE2" /> </logger> 

Thus, messages sent to FILE2 will not be recorded to FILE1

+1
source

I just figured it out later and wrote a blog explanation that could help other people reading this page.

How to send log messages to different log files

+1
source

All Articles