001/* 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2026, QOS.ch. All rights reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under 006 * either the terms of the Eclipse Public License v2.0 as published by 007 * the Eclipse Foundation 008 * 009 * or (per the licensee's choosing) 010 * 011 * under the terms of the GNU Lesser General Public License version 2.1 012 * as published by the Free Software Foundation. 013 */ 014package ch.qos.logback.core.status; 015 016import ch.qos.logback.core.Context; 017import ch.qos.logback.core.CoreConstants; 018 019import java.util.ArrayList; 020import java.util.List; 021import java.util.Objects; 022import java.util.regex.Matcher; 023import java.util.regex.Pattern; 024 025public class StatusUtil { 026 027 StatusManager sm; 028 029 public StatusUtil(StatusManager sm) { 030 this.sm = sm; 031 } 032 033 public StatusUtil(Context context) { 034 this.sm = context.getStatusManager(); 035 } 036 037 /** 038 * Returns true if the StatusManager associated with the context passed as 039 * parameter has one or more StatusListener instances registered. Returns false 040 * otherwise. 041 * 042 * @param context 043 * @return true if one or more StatusListeners registered, false otherwise 044 * @since 1.0.8 045 */ 046 static public boolean contextHasStatusListener(Context context) { 047 StatusManager sm = context.getStatusManager(); 048 if (sm == null) 049 return false; 050 List<StatusListener> listeners = sm.getCopyOfStatusListenerList(); 051 if (listeners == null || listeners.size() == 0) 052 return false; 053 else 054 return true; 055 } 056 057 static public List<Status> filterStatusListByTimeThreshold(List<Status> rawList, long threshold) { 058 List<Status> filteredList = new ArrayList<Status>(); 059 for (Status s : rawList) { 060 if (s.getTimestamp() >= threshold) 061 filteredList.add(s); 062 } 063 return filteredList; 064 } 065 066 public void addStatus(Status status) { 067 if (sm != null) { 068 sm.add(status); 069 } 070 } 071 072 public void addInfo(Object caller, String msg) { 073 addStatus(new InfoStatus(msg, caller)); 074 } 075 076 public void addWarn(Object caller, String msg) { 077 addStatus(new WarnStatus(msg, caller)); 078 } 079 080 public void addError(Object caller, String msg, Throwable t) { 081 addStatus(new ErrorStatus(msg, caller, t)); 082 } 083 084 public boolean hasXMLParsingErrors(long threshold) { 085 return containsMatch(threshold, Status.ERROR, CoreConstants.XML_PARSING); 086 } 087 088 public boolean noXMLParsingErrorsOccurred(long threshold) { 089 return !hasXMLParsingErrors(threshold); 090 } 091 092 public int getHighestLevel(long threshold) { 093 List<Status> filteredList = filterStatusListByTimeThreshold(sm.getCopyOfStatusList(), threshold); 094 int maxLevel = Status.INFO; 095 for (Status s : filteredList) { 096 if (s.getLevel() > maxLevel) 097 maxLevel = s.getLevel(); 098 } 099 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 144 public int levelCount(int level, long threshold) { 145 List<Status> filteredList = filterStatusListByTimeThreshold(sm.getCopyOfStatusList(), threshold); 146 147 int count = 0; 148 for (Status status : filteredList) { 149 if (status.getLevel() == level) 150 count++; 151 } 152 return count; 153 } 154 155 public int matchCount(String regex) { 156 int count = 0; 157 Pattern p = Pattern.compile(regex); 158 for (Status status : sm.getCopyOfStatusList()) { 159 String msg = status.getMessage(); 160 Matcher matcher = p.matcher(msg); 161 if (matcher.lookingAt()) { 162 count++; 163 } 164 } 165 return count; 166 } 167 168 public boolean containsException(Class<?> exceptionType) { 169 return containsException(exceptionType, null); 170 } 171 172 public boolean containsException(Class<?> exceptionType, String msgRegex) { 173 for (Status status : sm.getCopyOfStatusList()) { 174 Throwable t = status.getThrowable(); 175 while (t != null) { 176 if (t.getClass().getName().equals(exceptionType.getName())) { 177 if (msgRegex == null) { 178 return true; 179 } else if (checkRegexMatch(t.getMessage(), msgRegex)) { 180 return true; 181 } 182 } 183 t = t.getCause(); 184 } 185 } 186 return false; 187 } 188 189 private boolean checkRegexMatch(String message, String msgRegex) { 190 Pattern p = Pattern.compile(msgRegex); 191 Matcher matcher = p.matcher(message); 192 return matcher.lookingAt(); 193 } 194 195 196 /** 197 * Return the time of last reset. -1 if last reset time could not be found 198 * 199 * @return time of last reset or -1 200 */ 201 public long timeOfLastReset() { 202 List<Status> statusList = sm.getCopyOfStatusList(); 203 if (statusList == null) 204 return -1; 205 206 int len = statusList.size(); 207 for (int i = len - 1; i >= 0; i--) { 208 Status s = statusList.get(i); 209 if (CoreConstants.RESET_MSG_PREFIX.equals(s.getMessage())) { 210 return s.getTimestamp(); 211 } 212 } 213 return -1; 214 } 215 216 public static String diff(Status left, Status right) { 217 StringBuilder sb = new StringBuilder(); 218 if( left.getLevel() != right.getLevel()) { 219 sb.append(" left.level ").append(left.getLevel()).append(" != right.level ").append(right.getLevel()); 220 } 221 if( left.getTimestamp() != right.getTimestamp()) { 222 sb.append(" left.timestamp ").append(left.getTimestamp()).append(" != right.timestamp ").append(right.getTimestamp()); 223 } 224 if( !Objects.equals(left.getMessage(), right.getMessage())) { 225 sb.append(" left.message ").append(left.getMessage()).append(" != right.message ").append(right.getMessage()); 226 } 227 228 return sb.toString(); 229 } 230}