1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.status;
15
16 import java.util.Iterator;
17 import java.util.List;
18 import java.util.regex.Matcher;
19 import java.util.regex.Pattern;
20
21 import ch.qos.logback.core.Context;
22 import ch.qos.logback.core.CoreConstants;
23
24 import static ch.qos.logback.core.status.StatusUtil.filterStatusListByTimeThreshold;
25
26 public class StatusChecker {
27
28 StatusManager sm;
29
30 public StatusChecker(StatusManager sm) {
31 this.sm = sm;
32 }
33
34 public StatusChecker(Context context) {
35 this.sm = context.getStatusManager();
36 }
37
38 public boolean hasXMLParsingErrors(long threshold) {
39 return containsMatch(threshold, Status.ERROR, CoreConstants.XML_PARSING);
40 }
41
42 public boolean noXMLParsingErrorsOccurred(long threshold) {
43 return !hasXMLParsingErrors(threshold);
44 }
45
46 public int getHighestLevel(long threshold) {
47 List<Status> filteredList = filterStatusListByTimeThreshold(sm.getCopyOfStatusList(), threshold);
48 int maxLevel = Status.INFO;
49 for (Status s : filteredList) {
50 if (s.getLevel() > maxLevel)
51 maxLevel = s.getLevel();
52 }
53 return maxLevel;
54 }
55
56 public boolean isErrorFree(long threshold) {
57 return Status.ERROR > getHighestLevel(threshold);
58 }
59
60 public boolean containsMatch(long threshold, int level, String regex) {
61 List<Status> filteredList = filterStatusListByTimeThreshold(sm.getCopyOfStatusList(), threshold);
62 Pattern p = Pattern.compile(regex);
63
64 for (Status status : filteredList) {
65 if (level != status.getLevel()) {
66 continue;
67 }
68 String msg = status.getMessage();
69 Matcher matcher = p.matcher(msg);
70 if (matcher.lookingAt()) {
71 return true;
72 }
73 }
74 return false;
75
76 }
77
78 public boolean containsMatch(int level, String regex) {
79 return containsMatch(0, level, regex);
80 }
81
82 public boolean containsMatch(String regex) {
83 Pattern p = Pattern.compile(regex);
84 for (Status status : sm.getCopyOfStatusList()) {
85 String msg = status.getMessage();
86 Matcher matcher = p.matcher(msg);
87 if (matcher.lookingAt()) {
88 return true;
89 }
90 }
91 return false;
92 }
93
94 public int matchCount(String regex) {
95 int count = 0;
96 Pattern p = Pattern.compile(regex);
97 for (Status status : sm.getCopyOfStatusList()) {
98 String msg = status.getMessage();
99 Matcher matcher = p.matcher(msg);
100 if (matcher.lookingAt()) {
101 count++;
102 }
103 }
104 return count;
105 }
106
107 public boolean containsException(Class exceptionType) {
108 Iterator stati = sm.getCopyOfStatusList().iterator();
109 while (stati.hasNext()) {
110 Status status = (Status) stati.next();
111 Throwable t = status.getThrowable();
112 if (t != null && t.getClass().getName().equals(exceptionType.getName())) {
113 return true;
114 }
115 }
116 return false;
117 }
118
119
120
121
122
123 public long timeOfLastReset() {
124 List<Status> statusList = sm.getCopyOfStatusList();
125 if(statusList == null)
126 return -1;
127
128 int len = statusList.size();
129 for(int i = len-1; i >= 0; i--) {
130 Status s = statusList.get(i);
131 if(CoreConstants.RESET_MSG_PREFIX.equals(s.getMessage())) {
132 return s.getDate();
133 }
134 }
135 return -1;
136 }
137 }