On Wed, Jun 20, 2012 at 1:34 PM, George, Kenneth V [NTK] < Kenneth.V.George@sprint.com> wrote:
Good afternoon.
I have a situation where I need to log information to 2 separate files, but under different conditions, for the same context/class files.
Basically, I have 2 files: 1 log, 1 alarm_log.
I need to set the level for reporting to INFO, but only log WARN/ERROR messages to alarm_log when a MDC value of “SEND_ALARM” is set. I have tried to place a <filter> of type “ch.qos.logback.core.filter.EvaluatorFilter” and cannot seem to get it to work, messages of Level INFO show-up in the alarm_log.
Can this even be done (maybe I am just using it wrong)? Here is the section of my logback.xml file specifying the appender:
<appender name="ALARM" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
<evaluator>
<expression>
if (mdc == null || mdc.get("SEND_ALARM") == null)
No need to check for (mdc == null) because it's guaranteed to not be null in versions 0.9.30 and later.
return false;
return true;
</expression>
</evaluator>
</filter>
<file>logs/alarms.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>logs/archive/logFile.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- keep 30 days' worth of history -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%date{MMM dd, yyyy hh:mm:ss} %-5level %msg%n</pattern>
</encoder>
</appender>
Try this: <appender name="ALARM" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.core.filter.JaninoEvaluatorFilter"> <evaluator> <expression>return (mdc.get("SEND_ALARM") != null) && (level > INFO);</expression> </evaluator> <OnMismatch>DENY</OnMismatch> <OnMatch>ACCEPT</OnMatch> </filter> <file>logs/alarms.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- daily rollover --> <fileNamePattern>logs/archive/logFile.%d{yyyy-MM-dd}.log</fileNamePattern> <!-- keep 30 days' worth of history --> <maxHistory>30</maxHistory> </rollingPolicy> <encoder> <pattern>%date{MMM dd, yyyy hh:mm:ss} %-5level %msg%n</pattern> </encoder> </appender>