View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
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.Iterator;
21  import java.util.List;
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       * Returns true if the StatusManager associated with the context passed
39       * as parameter has one or more StatusListener instances registered. Returns
40       * false otherwise.
41       *
42       * @param context
43       * @return true if one or more StatusListeners registered, false otherwise
44       * @since 1.0.8
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.getDate() >= 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 Status.ERROR > getHighestLevel(threshold);
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 matchCount(String regex) {
144         int count = 0;
145         Pattern p = Pattern.compile(regex);
146         for (Status status : sm.getCopyOfStatusList()) {
147             String msg = status.getMessage();
148             Matcher matcher = p.matcher(msg);
149             if (matcher.lookingAt()) {
150                 count++;
151             }
152         }
153         return count;
154     }
155 
156     public boolean containsException(Class<?> exceptionType) {
157         Iterator<Status> stati = sm.getCopyOfStatusList().iterator();
158         while (stati.hasNext()) {
159             Status status = stati.next();
160             Throwable t = status.getThrowable();
161             while (t != null) {
162                 if (t.getClass().getName().equals(exceptionType.getName())) {
163                     return true;
164                 }
165                 t = t.getCause();
166             }
167         }
168         return false;
169     }
170 
171     /**
172      * Return the time of last reset. -1 if last reset time could not be found
173      *
174      * @return time of last reset or -1
175      */
176     public long timeOfLastReset() {
177         List<Status> statusList = sm.getCopyOfStatusList();
178         if (statusList == null)
179             return -1;
180 
181         int len = statusList.size();
182         for (int i = len - 1; i >= 0; i--) {
183             Status s = statusList.get(i);
184             if (CoreConstants.RESET_MSG_PREFIX.equals(s.getMessage())) {
185                 return s.getDate();
186             }
187         }
188         return -1;
189     }
190 
191 }