views:

635

answers:

1

I have a normal INFO level log for application. What I need is to additionally log all ERROR level events to separate error log. I am using configuration like this:

<logger name="com.acme">
  <level value="error"/>
  <appender-ref ref="error"/>
</logger>

<logger name="com.acme">
  <level value="info"/>
</logger>

<root>
  <level value="warn"/>
  <appender-ref ref="general"/>
</root>

This configuration logs errors only. If I put info level logger first, then it will log only to general appender, but error logger will not work. I would like to have them both working.

+7  A: 

What you need to do is have a single <logger> definition with a defined level of INFO, but in your two appender definitions, you set their thresholds accordingly, e.g.

<appender name="ERROR_FILE>
   <param name="Threshold" value="ERROR"/>
</appender>

<appender name="GENERAL">
   <param name="Threshold" value="INFO"/>
</appender>

You then add both appenders to your logger:

<logger name="com.acme">
  <level value="INFO"/>
  <appender-ref ref="ERROR_FILE"/>
  <appender-ref ref="GENERAL"/>
</logger>

Log entries now going to the logger will get sent to both appenders, but since they have different independent thresholds, the ERROR_FILE appender will only log ERROR and above.

skaffman
@vilmonts If you define the same logger (com.acme) twice, one of them gets overwritten, and that's why you can't see the INFO messages: the first one (with "error" level) erases the next (with "info" messages).
Alexandre Brasil
Thanks, skaffman. You saved my life.
vilmonts