| 5827 |
amar.kumar |
1 |
package net.sourceforge.stat4j.filter;
|
|
|
2 |
|
|
|
3 |
import java.io.FileInputStream;
|
|
|
4 |
import java.io.IOException;
|
|
|
5 |
import java.util.ArrayList;
|
|
|
6 |
import java.util.Date;
|
|
|
7 |
import java.util.Enumeration;
|
|
|
8 |
import java.util.HashMap;
|
|
|
9 |
import java.util.List;
|
|
|
10 |
import java.util.Map;
|
|
|
11 |
import java.util.Properties;
|
|
|
12 |
|
|
|
13 |
|
|
|
14 |
public class LogInterceptor {
|
|
|
15 |
|
|
|
16 |
private static Map<String, FilteredLogEvent> eventsToBeDiscardedForCalc;
|
|
|
17 |
private static List<String> logRegexList;
|
|
|
18 |
static {
|
|
|
19 |
eventsToBeDiscardedForCalc = new HashMap<String, FilteredLogEvent>();
|
|
|
20 |
logRegexList = new ArrayList<String>();
|
|
|
21 |
processConfigParams();
|
|
|
22 |
|
|
|
23 |
}
|
|
|
24 |
|
|
|
25 |
public static boolean shouldProcessLog(String logMessage) {
|
|
|
26 |
Date date = new Date();
|
|
|
27 |
for(String regex : logRegexList) {
|
|
|
28 |
if(logMessage.substring(Math.min(150, logMessage.length())).indexOf(regex)!=-1) {
|
|
|
29 |
FilteredLogEvent filteredLogEvent = eventsToBeDiscardedForCalc.get(regex);
|
|
|
30 |
if(filteredLogEvent!=null) {
|
|
|
31 |
if(date.getHours() >= filteredLogEvent.getStartThresholdHour() &&
|
|
|
32 |
date.getHours() <= filteredLogEvent.getEndThresholdHour() &&
|
|
|
33 |
date.getMinutes() >=filteredLogEvent.getStartThresholdMinute() &&
|
|
|
34 |
date.getMinutes() <=filteredLogEvent.getEndThresholdMinute()) {
|
|
|
35 |
System.out.println("\n Should not Process log " + new Date());
|
|
|
36 |
return false;
|
|
|
37 |
}
|
|
|
38 |
}
|
|
|
39 |
}
|
|
|
40 |
}
|
|
|
41 |
System.out.println("\n Should Process log \n" + new Date());
|
|
|
42 |
return true;
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
private static void processConfigParams() {
|
|
|
46 |
try {
|
|
|
47 |
Properties filterProp = new Properties();
|
|
|
48 |
FileInputStream in = new FileInputStream("logFilters.properties");
|
|
|
49 |
filterProp.load(in);
|
|
|
50 |
Enumeration em = filterProp.keys();
|
|
|
51 |
while(em.hasMoreElements()){
|
|
|
52 |
String logRegex = (String)em.nextElement();
|
|
|
53 |
String startTime = ((String)filterProp.get(logRegex)).split("/")[0];
|
|
|
54 |
String endTime = ((String)filterProp.get(logRegex)).split("/")[1];
|
|
|
55 |
String startHour = startTime.split(":")[0];
|
|
|
56 |
String startMin = startTime.split(":")[1];
|
|
|
57 |
String endHour = endTime.split(":")[0];
|
|
|
58 |
String endMin = endTime.split(":")[1];
|
|
|
59 |
|
|
|
60 |
FilteredLogEvent filteredLogEvent = new FilteredLogEvent();
|
|
|
61 |
filteredLogEvent.setLogRegex(logRegex);
|
|
|
62 |
filteredLogEvent.setStartThresholdHour(Integer.parseInt(startHour));
|
|
|
63 |
filteredLogEvent.setStartThresholdMinute(Integer.parseInt(startMin));
|
|
|
64 |
filteredLogEvent.setEndThresholdHour(Integer.parseInt(endHour));
|
|
|
65 |
filteredLogEvent.setEndThresholdMinute(Integer.parseInt(endMin));
|
|
|
66 |
eventsToBeDiscardedForCalc.put(logRegex, filteredLogEvent);
|
|
|
67 |
logRegexList.add(logRegex);
|
|
|
68 |
|
|
|
69 |
System.out.println("Interval is \n StartThresholdHour : " + startHour);
|
|
|
70 |
System.out.println("Interval is \n StartThresholdMin : " + startMin);
|
|
|
71 |
System.out.println("Interval is \n EndThresholdHour : " + endHour);
|
|
|
72 |
System.out.println("Interval is \n EndThresholdMin : " + endMin);
|
|
|
73 |
|
|
|
74 |
}
|
|
|
75 |
} catch(IOException ex) {
|
|
|
76 |
System.out.println("Exception while initializing logFilter properties");
|
|
|
77 |
ex.printStackTrace();
|
|
|
78 |
}
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
public static void main(String[] args) {
|
|
|
82 |
System.out.println("");
|
|
|
83 |
processConfigParams();
|
|
|
84 |
}
|
|
|
85 |
}
|