0

I have two appenders, file and console, in my project. I would like to configure my application to perform as such:

all loggers with name "my.app.*":

1. log events DEBUG and higher to fileA
2. log events DEBUG and higher to fileB

all other loggers:

1. log events WARN and higher to fileA
2. log events DEBUG and higher to fileB

Ideally, the configuration would look something like this:

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="fileA" class="org.apache.log4j.FileAppender">
    <!-- configuration -->
</appender>

<appender name="fileB" class="org.apache.log4j.FileAppender">
    <!-- configuration -->
</appender>

<logger name="my.app" additivity="false">
    <level="DEBUG"/>
    <appender-ref ref="fileA"/>
</logger>

<logger name="" additivity="true">
    <level="DEBUG"/>
    <appender-ref ref="fileB"/>
</logger>

<root>
    <level="WARN"/>
    <appender-ref ref="fileA"/>
</root>
</log4j>

However, this setup causes loggers named "my.app" to only log to console, and all other loggers to log to console on WARN and above. Essentially, <logger name=""> is being ignored. Is there another way to emulate this behavior with log4j?

PS. I apologize for the poor formatting, really struggling to get this to work tonight :/

Josh
  • 1,574
  • 1
  • 15
  • 36
  • 1
    did you forget to type the second appender in root element? – guido Feb 03 '12 at 07:55
  • logger name="" does nothing, the rootlogger does what you think name="" does. – oers Feb 03 '12 at 07:57
  • @guido: if I put a ref to fileB in root, then all other loggers will only write to it on WARN and above, where I want them to write to it on DEBUG and above – Josh Feb 03 '12 at 08:01
  • @oers: that's what I was thinking. Is there a way to emulate rootlogger so I can use it in the ancestry chain? – Josh Feb 03 '12 at 08:02

1 Answers1

2

You need:

<logger name="my.app" additivity="false">
    <level="DEBUG"/>
    <appender-ref ref="console"/>
    <appender-ref ref="file"/>
</logger>

<root>
    <appender-ref ref="console-warn"/>
    <appender-ref ref="file-debug"/>
</root>

For the root-appender you need two new console/file appenders, that have the desired level restrictions.

<appender name="file-debug" class="org.apache.log4j.FileAppender">
    <param name="Threshold" value="DEBUG"/>
</appender>

<appender name="console-warn" class="org.apache.log4j.ConsoleAppender">
    <param name="Threshold" value="WARN"/>
</appender>
oers
  • 18,436
  • 13
  • 66
  • 75
  • I apologize, but I had a typo in the original post. Both are actually file appenders. Is it possible to have two different file appenders write to the same file? – Josh Feb 03 '12 at 08:04
  • @Josh I don't think so: http://stackoverflow.com/questions/4046825/log4j-have-multiple-appenders-write-to-the-same-file-with-one-that-always-logs – oers Feb 03 '12 at 08:06
  • Hm thanks. But you are right, your solution would work for my original problem of console and file. – Josh Feb 03 '12 at 08:08