Search code examples

How to configure log4j to log different log levels to different files for the same logger

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 name="com.acme">
    <level value="info"/>

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

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.


  • 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 name="GENERAL">
       <param name="Threshold" value="INFO"/>

    You then add both appenders to your logger:

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

    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.