1 23 24 30 31 package com.sun.enterprise.server.logging.stats; 32 33 import java.util.Map ; 34 import java.util.List ; 35 import java.util.HashMap ; 36 import java.util.ArrayList ; 37 import java.util.logging.Level ; 38 import java.util.logging.Logger ; 39 import java.util.logging.LogRecord ; 40 41 import com.sun.logging.LogDomains; 42 import com.sun.appserv.management.ext.logging.LogAnalyzer; 43 44 49 public class ErrorStatistics { 50 51 53 private static final int MIN_INTERVALS = 5; 54 private static final int MAX_INTERVALS = 500; 55 private static final long DEFAULT_INTERVAL = 3600*1000; 57 private static final ErrorStatistics singleton = new ErrorStatistics(); 58 59 public static ErrorStatistics singleton() { 60 return singleton; 61 } 62 63 public static void registerStartupTime() { 64 singleton(); } 66 67 private static class ErrorCount { 68 69 private int severeCount; 70 private int warningCount; 71 72 void setSevereCount(int count) { 73 this.severeCount = count; 74 } 75 76 int getSevereCount() { 77 return this.severeCount; 78 } 79 80 void setWarningCount(int count) { 81 this.warningCount = count; 82 } 83 84 int getWarningCount() { 85 return this.warningCount; 86 } 87 } 88 89 91 private long interval; 92 private int numOfIntervals; 93 private long startTimeStamp; 94 private HashMap <Long ,HashMap <String ,ErrorCount>> intervalMap; 95 96 ErrorStatistics() { 97 interval = DEFAULT_INTERVAL; 98 numOfIntervals = MIN_INTERVALS; 99 intervalMap = new HashMap <Long ,HashMap <String ,ErrorCount>>(); 100 startTimeStamp = System.currentTimeMillis(); 101 } 102 103 108 public int getNumOfIntervals() { 109 return numOfIntervals; 110 } 111 112 118 public void setNumOfIntervals(int intervals) { 119 if ((intervals < MIN_INTERVALS) || (intervals > MAX_INTERVALS)) { 120 throw new IllegalArgumentException ( 121 "Number of intervals must be between " + MIN_INTERVALS + 122 " and " + MAX_INTERVALS); 123 } 124 numOfIntervals = intervals; 125 } 126 127 132 public void setIntervalDuration(long interval) { 133 this.interval = interval; 134 } 135 136 141 public long getIntervalDuration() { 142 return this.interval; 143 } 144 145 150 public void updateStatistics(LogRecord record) { 151 152 154 long logTimeStamp = record.getMillis(); 155 String logModuleId = record.getLoggerName(); 156 if (logModuleId == null) { 157 Logger logger = LogDomains.getLogger(LogDomains.CORE_LOGGER); 158 logger.log( 159 Level.WARNING, "Update error statistics failed", 160 (new Throwable ("Logger name is null.")).fillInStackTrace()); 161 return; 162 } 163 Level logLevel = record.getLevel(); 164 165 173 175 long hoursElapsed = (logTimeStamp-startTimeStamp)/interval; 176 long timeStamp = startTimeStamp + hoursElapsed*interval; 177 181 182 184 HashMap <String ,ErrorCount> moduleMap = intervalMap.get(timeStamp); 185 if (moduleMap == null) { 186 moduleMap = new HashMap <String ,ErrorCount>(); 187 trimHourMap(timeStamp); intervalMap.put(Long.valueOf(timeStamp), moduleMap); 189 } 190 191 ErrorCount errorCount = moduleMap.get(logModuleId); 192 if (errorCount == null) { 193 errorCount = new ErrorCount(); 194 moduleMap.put(logModuleId, errorCount); 195 } 196 197 if (logLevel.equals(Level.SEVERE)) { 198 errorCount.setSevereCount(errorCount.getSevereCount()+1); 199 } else { errorCount.setWarningCount(errorCount.getWarningCount()+1); 201 } 202 } 203 204 private void trimHourMap(long refTimeStamp) { 205 Long [] timeStamps = intervalMap.keySet().toArray(new Long [0]); 206 for (int i = 0; i < timeStamps.length; i++) { 207 long timeStamp = timeStamps[i]; 208 if ((refTimeStamp-timeStamp) >= interval*numOfIntervals) { 209 intervalMap.remove(timeStamp); 210 } 211 } 212 } 213 214 216 220 public List <Map <String ,Object >> getErrorInformation() { 221 222 224 long intervalsElapsed = 225 (System.currentTimeMillis()-startTimeStamp)/interval; 226 long recentTimeStamp = startTimeStamp + (intervalsElapsed*interval); 227 228 230 List <Map <String ,Object >> results = 231 new ArrayList <Map <String ,Object >>(); 232 for (int i = 0; i < numOfIntervals; i++) { 233 long timeStamp = recentTimeStamp - interval*i; 234 HashMap <String ,ErrorCount> moduleMap = intervalMap.get(timeStamp); 235 int severeCount = 0, warningCount = 0; 236 if (timeStamp<startTimeStamp) { 237 severeCount = -1; 238 warningCount = -1; 239 } 240 if (moduleMap != null) { 241 for (ErrorCount error : moduleMap.values()) { 242 severeCount += error.getSevereCount(); 243 warningCount += error.getWarningCount(); 244 } 245 } 246 HashMap <String ,Object > entry = new HashMap <String ,Object >(); 247 entry.put(LogAnalyzer.TIMESTAMP_KEY, timeStamp); 248 entry.put(LogAnalyzer.SEVERE_COUNT_KEY, severeCount); 249 entry.put(LogAnalyzer.WARNING_COUNT_KEY, warningCount); 250 results.add(entry); 251 } 252 253 return results; 254 } 255 256 260 public Map <String ,Integer > getErrorDistribution( 261 long timeStamp, Level level) { 262 263 265 if (!(level.equals(Level.SEVERE) || level.equals(Level.WARNING))) { 266 throw new IllegalArgumentException ("Log level: " + level); 267 } 268 269 271 HashMap <String ,ErrorCount> moduleMap = intervalMap.get(timeStamp); 272 if (moduleMap == null) { 273 return null; 274 } 275 276 Map <String ,Integer > results = new HashMap <String ,Integer >(); 277 for (String moduleId : moduleMap.keySet()) { 278 ErrorCount errorCount = moduleMap.get(moduleId); 279 if (level.equals(Level.SEVERE)) { 280 results.put(moduleId, errorCount.getSevereCount()); 281 } else { results.put(moduleId, errorCount.getWarningCount()); 283 } 284 } 285 286 return results; 287 } 288 289 291 public static void main(String [] args) { 292 293 295 LogRecord srecord = new LogRecord (Level.SEVERE, "severe record"); 296 srecord.setMillis(System.currentTimeMillis()); 297 srecord.setLoggerName("com.wombat.smodule"); 298 299 LogRecord wrecord = new LogRecord (Level.WARNING, "warning record"); 300 wrecord.setMillis(System.currentTimeMillis()); 301 wrecord.setLoggerName("com.wombat.wmodule"); 302 303 305 java.text.SimpleDateFormat sdf = 306 new java.text.SimpleDateFormat ("HH:mm:ss"); 307 308 ErrorStatistics stats = new ErrorStatistics(); 309 long interval = 1000; stats.setIntervalDuration(interval); 311 for (int i = 0; i < 10; i++) { 312 try { 313 Thread.sleep(interval); 314 } catch (InterruptedException e) {} 315 for (int j = 0; j < 2; j++) { 316 srecord.setMillis(System.currentTimeMillis()); 317 stats.updateStatistics(srecord); 318 wrecord.setMillis(System.currentTimeMillis()); 319 stats.updateStatistics(wrecord); 320 } 321 System.out.printf("Interval(%1$s): %2$s\n", i, 322 sdf.format(new java.util.Date (System.currentTimeMillis()))); 323 } 324 325 327 System.out.println("\nTimeStamp\tSevere\tWarning"); 328 System.out.println("--------------------------------"); 329 List <Map <String ,Object >> list = stats.getErrorInformation(); 330 for (Map <String ,Object > item : list) { 331 long timeStamp = (Long ) item.get(LogAnalyzer.TIMESTAMP_KEY); 332 int severeCount = (Integer ) item.get(LogAnalyzer.SEVERE_COUNT_KEY); 333 int warningCount = 334 (Integer ) item.get(LogAnalyzer.WARNING_COUNT_KEY); 335 System.out.printf("%1$s\t%2$s\t%3$s\n", 336 sdf.format(new java.util.Date (timeStamp)), 337 severeCount, warningCount); 338 } 339 340 for (Map <String ,Object > item : list) { 341 long timeStamp = (Long ) item.get(LogAnalyzer.TIMESTAMP_KEY); 342 Map <String ,Integer > map = 343 stats.getErrorDistribution(timeStamp, Level.SEVERE); 344 System.out.printf("\nModuleId\tLevel.SEVERE\t(%1$s)\n", 345 sdf.format(new java.util.Date (timeStamp))); 346 System.out.println("------------------------------------------"); 347 for (String moduleId : map.keySet()) { 348 System.out.printf("%1$s\t%2$s\n", moduleId, map.get(moduleId)); 349 } 350 map = stats.getErrorDistribution(timeStamp, Level.WARNING); 351 System.out.printf("\nModuleId\tLevel.WARNING\t(%1$s)\n", 352 sdf.format(new java.util.Date (timeStamp))); 353 System.out.println("------------------------------------------"); 354 for (String moduleId : map.keySet()) { 355 System.out.printf("%1$s\t%2$s\n", moduleId, map.get(moduleId)); 356 } 357 } 358 } 359 } 360 | Popular Tags |