1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.status;
15
16 import ch.qos.logback.core.Context;
17 import ch.qos.logback.core.CoreConstants;
18
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.Objects;
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
24
25 public class StatusUtil {
26
27 StatusManager sm;
28
29 public StatusUtil(StatusManager sm) {
30 this.sm = sm;
31 }
32
33 public StatusUtil(Context context) {
34 this.sm = context.getStatusManager();
35 }
36
37
38
39
40
41
42
43
44
45
46 static public boolean contextHasStatusListener(Context context) {
47 StatusManager sm = context.getStatusManager();
48 if (sm == null)
49 return false;
50 List<StatusListener> listeners = sm.getCopyOfStatusListenerList();
51 if (listeners == null || listeners.size() == 0)
52 return false;
53 else
54 return true;
55 }
56
57 static public List<Status> filterStatusListByTimeThreshold(List<Status> rawList, long threshold) {
58 List<Status> filteredList = new ArrayList<Status>();
59 for (Status s : rawList) {
60 if (s.getTimestamp() >= threshold)
61 filteredList.add(s);
62 }
63 return filteredList;
64 }
65
66 public void addStatus(Status status) {
67 if (sm != null) {
68 sm.add(status);
69 }
70 }
71
72 public void addInfo(Object caller, String msg) {
73 addStatus(new InfoStatus(msg, caller));
74 }
75
76 public void addWarn(Object caller, String msg) {
77 addStatus(new WarnStatus(msg, caller));
78 }
79
80 public void addError(Object caller, String msg, Throwable t) {
81 addStatus(new ErrorStatus(msg, caller, t));
82 }
83
84 public boolean hasXMLParsingErrors(long threshold) {
85 return containsMatch(threshold, Status.ERROR, CoreConstants.XML_PARSING);
86 }
87
88 public boolean noXMLParsingErrorsOccurred(long threshold) {
89 return !hasXMLParsingErrors(threshold);
90 }
91
92 public int getHighestLevel(long threshold) {
93 List<Status> filteredList = filterStatusListByTimeThreshold(sm.getCopyOfStatusList(), threshold);
94 int maxLevel = Status.INFO;
95 for (Status s : filteredList) {
96 if (s.getLevel() > maxLevel)
97 maxLevel = s.getLevel();
98 }
99 return maxLevel;
100 }
101
102 public boolean isErrorFree(long threshold) {
103 return getHighestLevel(threshold) < Status.ERROR;
104 }
105
106 public boolean isWarningOrErrorFree(long threshold) {
107 return Status.WARN > getHighestLevel(threshold);
108 }
109
110 public boolean containsMatch(long threshold, int level, String regex) {
111 List<Status> filteredList = filterStatusListByTimeThreshold(sm.getCopyOfStatusList(), threshold);
112 Pattern p = Pattern.compile(regex);
113
114 for (Status status : filteredList) {
115 if (level != status.getLevel()) {
116 continue;
117 }
118 String msg = status.getMessage();
119 Matcher matcher = p.matcher(msg);
120 if (matcher.lookingAt()) {
121 return true;
122 }
123 }
124 return false;
125 }
126
127 public boolean containsMatch(int level, String regex) {
128 return containsMatch(0, level, regex);
129 }
130
131 public boolean containsMatch(String regex) {
132 Pattern p = Pattern.compile(regex);
133 for (Status status : sm.getCopyOfStatusList()) {
134 String msg = status.getMessage();
135 Matcher matcher = p.matcher(msg);
136 if (matcher.lookingAt()) {
137 return true;
138 }
139 }
140 return false;
141 }
142
143 public int levelCount(int level, long threshold) {
144 List<Status> filteredList = filterStatusListByTimeThreshold(sm.getCopyOfStatusList(), threshold);
145
146 int count = 0;
147 for (Status status : filteredList) {
148 if (status.getLevel() == level)
149 count++;
150 }
151 return count;
152 }
153
154 public int matchCount(String regex) {
155 int count = 0;
156 Pattern p = Pattern.compile(regex);
157 for (Status status : sm.getCopyOfStatusList()) {
158 String msg = status.getMessage();
159 Matcher matcher = p.matcher(msg);
160 if (matcher.lookingAt()) {
161 count++;
162 }
163 }
164 return count;
165 }
166
167 public boolean containsException(Class<?> exceptionType) {
168 return containsException(exceptionType, null);
169 }
170
171 public boolean containsException(Class<?> exceptionType, String msgRegex) {
172 for (Status status : sm.getCopyOfStatusList()) {
173 Throwable t = status.getThrowable();
174 while (t != null) {
175 if (t.getClass().getName().equals(exceptionType.getName())) {
176 if (msgRegex == null) {
177 return true;
178 } else if (checkRegexMatch(t.getMessage(), msgRegex)) {
179 return true;
180 }
181 }
182 t = t.getCause();
183 }
184 }
185 return false;
186 }
187
188 private boolean checkRegexMatch(String message, String msgRegex) {
189 Pattern p = Pattern.compile(msgRegex);
190 Matcher matcher = p.matcher(message);
191 return matcher.lookingAt();
192 }
193
194
195
196
197
198
199
200 public long timeOfLastReset() {
201 List<Status> statusList = sm.getCopyOfStatusList();
202 if (statusList == null)
203 return -1;
204
205 int len = statusList.size();
206 for (int i = len - 1; i >= 0; i--) {
207 Status s = statusList.get(i);
208 if (CoreConstants.RESET_MSG_PREFIX.equals(s.getMessage())) {
209 return s.getTimestamp();
210 }
211 }
212 return -1;
213 }
214
215 public static String diff(Status left, Status right) {
216 StringBuilder sb = new StringBuilder();
217 if( left.getLevel() != right.getLevel()) {
218 sb.append(" left.level ").append(left.getLevel()).append(" != right.level ").append(right.getLevel());
219 }
220 if( left.getTimestamp() != right.getTimestamp()) {
221 sb.append(" left.timestamp ").append(left.getTimestamp()).append(" != right.timestamp ").append(right.getTimestamp());
222 }
223 if( !Objects.equals(left.getMessage(), right.getMessage())) {
224 sb.append(" left.message ").append(left.getMessage()).append(" != right.message ").append(right.getMessage());
225 }
226
227 return sb.toString();
228 }
229 }