Friday, October 16, 2015

Filter logback events by time


Example:

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<target>System.out</target>
<encoder>
<pattern>${common.log.conversionpattern}</pattern>
</encoder>
<!-- perform more precise filtering, you can suppress particular messages here -->
<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
<evaluator>
<expression><![CDATA[

     java.util.Calendar cal = java.util.Calendar.getInstance(java.util.TimeZone.getDefault());
     cal.setTimeInMillis(timeStamp);
     int hour = cal.get(java.util.Calendar.HOUR_OF_DAY); //HOUR_OF_DAY is required for 24h format
   
if (hour > 14) {
return false;
}

return true;
]]></expression>
</evaluator>
<OnMatch>NEUTRAL</OnMatch> <!-- we may want to add more filters later -->
<OnMismatch>DENY</OnMismatch>
</filter>

</appender>

No comments:

Post a Comment