Blame | Last modification | View Log | RSS feed
package net.sourceforge.stat4j.filter;import java.io.FileInputStream;import java.io.IOException;import java.util.ArrayList;import java.util.Date;import java.util.Enumeration;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Properties;public class LogInterceptor {private static Map<String, FilteredLogEvent> eventsToBeDiscardedForCalc;private static List<String> logRegexList;static {eventsToBeDiscardedForCalc = new HashMap<String, FilteredLogEvent>();logRegexList = new ArrayList<String>();processConfigParams();}public static boolean shouldProcessLog(String logMessage) {Date date = new Date();for(String regex : logRegexList) {if(logMessage.substring(Math.min(150, logMessage.length())).indexOf(regex)!=-1) {FilteredLogEvent filteredLogEvent = eventsToBeDiscardedForCalc.get(regex);if(filteredLogEvent!=null) {if(date.getHours() >= filteredLogEvent.getStartThresholdHour() &&date.getHours() <= filteredLogEvent.getEndThresholdHour() &&date.getMinutes() >=filteredLogEvent.getStartThresholdMinute() &&date.getMinutes() <=filteredLogEvent.getEndThresholdMinute()) {System.out.println("\n Should not Process log " + new Date());return false;}}}}System.out.println("\n Should Process log \n" + new Date());return true;}private static void processConfigParams() {try {Properties filterProp = new Properties();FileInputStream in = new FileInputStream("logFilters.properties");filterProp.load(in);Enumeration em = filterProp.keys();while(em.hasMoreElements()){String logRegex = (String)em.nextElement();String startTime = ((String)filterProp.get(logRegex)).split("/")[0];String endTime = ((String)filterProp.get(logRegex)).split("/")[1];String startHour = startTime.split(":")[0];String startMin = startTime.split(":")[1];String endHour = endTime.split(":")[0];String endMin = endTime.split(":")[1];FilteredLogEvent filteredLogEvent = new FilteredLogEvent();filteredLogEvent.setLogRegex(logRegex);filteredLogEvent.setStartThresholdHour(Integer.parseInt(startHour));filteredLogEvent.setStartThresholdMinute(Integer.parseInt(startMin));filteredLogEvent.setEndThresholdHour(Integer.parseInt(endHour));filteredLogEvent.setEndThresholdMinute(Integer.parseInt(endMin));eventsToBeDiscardedForCalc.put(logRegex, filteredLogEvent);logRegexList.add(logRegex);System.out.println("Interval is \n StartThresholdHour : " + startHour);System.out.println("Interval is \n StartThresholdMin : " + startMin);System.out.println("Interval is \n EndThresholdHour : " + endHour);System.out.println("Interval is \n EndThresholdMin : " + endMin);}} catch(IOException ex) {System.out.println("Exception while initializing logFilter properties");ex.printStackTrace();}}public static void main(String[] args) {System.out.println("");processConfigParams();}}