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.
If you are using log4j2 and config with XML format, ThresholdFilter is a good solution.
Here is a sample:
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.You then add both appenders to your 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.
Full working solution including the date in the filename:
You need to use log4j filters:
That way you can create log files for each level separately.