ErrorStatisticspublic class ErrorStatistics extends Object This keeps track of the error statistics (log levels SEVERE and WARNING). |
Fields Summary |
---|
private static final int | MIN_INTERVALS | private static final int | MAX_INTERVALS | private static final long | DEFAULT_INTERVAL | private static final ErrorStatistics | singleton | private long | interval | private int | numOfIntervals | private long | startTimeStamp | private HashMap | intervalMap |
Constructors Summary |
---|
ErrorStatistics()
interval = DEFAULT_INTERVAL;
numOfIntervals = MIN_INTERVALS;
intervalMap = new HashMap<Long,HashMap<String,ErrorCount>>();
startTimeStamp = System.currentTimeMillis();
|
Methods Summary |
---|
public java.util.Map | getErrorDistribution(long timeStamp, java.util.logging.Level level)
// Sanity check.
if (!(level.equals(Level.SEVERE) || level.equals(Level.WARNING))) {
throw new IllegalArgumentException("Log level: " + level);
}
// Gather the information.
HashMap<String,ErrorCount> moduleMap = intervalMap.get(timeStamp);
if (moduleMap == null) {
return null;
}
Map<String,Integer> results = new HashMap<String,Integer>();
for (String moduleId : moduleMap.keySet()) {
ErrorCount errorCount = moduleMap.get(moduleId);
if (level.equals(Level.SEVERE)) {
results.put(moduleId, errorCount.getSevereCount());
} else { // Level == WARNING
results.put(moduleId, errorCount.getWarningCount());
}
}
return results;
| public java.util.List | getErrorInformation()
// Calculate the most recent timestamp bucket.
long intervalsElapsed =
(System.currentTimeMillis()-startTimeStamp)/interval;
long recentTimeStamp = startTimeStamp + (intervalsElapsed*interval);
// Gather the information for the past numOfIntervals.
List<Map<String,Object>> results =
new ArrayList<Map<String,Object>>();
for (int i = 0; i < getNumOfIntervals(); i++) {
long timeStamp = recentTimeStamp - interval*i;
HashMap<String,ErrorCount> moduleMap = intervalMap.get(timeStamp);
int severeCount = 0, warningCount = 0;
if (timeStamp<startTimeStamp) {
severeCount = -1;
warningCount = -1;
}
if (moduleMap != null) {
for (ErrorCount error : moduleMap.values()) {
severeCount += error.getSevereCount();
warningCount += error.getWarningCount();
}
}
HashMap<String,Object> entry = new HashMap<String,Object>();
entry.put(LogAnalyzer.TIMESTAMP_KEY, timeStamp);
entry.put(LogAnalyzer.SEVERE_COUNT_KEY, severeCount);
entry.put(LogAnalyzer.WARNING_COUNT_KEY, warningCount);
results.add(entry);
}
return results;
| public long | getIntervalDuration()Get the interval duration.
return this.interval;
| public int | getNumOfIntervals()Get the number of intevals, for which error statistics is maintained.
try {
ServerContext sc = ApplicationServer.getServerContext();
if (sc != null) {
LogService ls = ServerBeansFactory.getConfigBean(sc.getConfigContext()).getLogService();
numOfIntervals = Integer.parseInt(ls.getRetainErrorStatisticsForHours());
}
}catch(Exception n) {
numOfIntervals = MIN_INTERVALS;
}
return numOfIntervals;
| public static void | main(java.lang.String[] args)
// Create log records
LogRecord srecord = new LogRecord(Level.SEVERE, "severe record");
srecord.setMillis(System.currentTimeMillis());
srecord.setLoggerName("com.wombat.smodule");
LogRecord wrecord = new LogRecord(Level.WARNING, "warning record");
wrecord.setMillis(System.currentTimeMillis());
wrecord.setLoggerName("com.wombat.wmodule");
// Update error statistics
java.text.SimpleDateFormat sdf =
new java.text.SimpleDateFormat("HH:mm:ss");
ErrorStatistics stats = new ErrorStatistics();
long interval = 1000; // 1 second.
stats.setIntervalDuration(interval);
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(interval);
} catch (InterruptedException e) {}
for (int j = 0; j < 2; j++) {
srecord.setMillis(System.currentTimeMillis());
stats.updateStatistics(srecord);
wrecord.setMillis(System.currentTimeMillis());
stats.updateStatistics(wrecord);
}
System.out.printf("Interval(%1$s): %2$s\n", i,
sdf.format(new java.util.Date(System.currentTimeMillis())));
}
// Query error statistics
System.out.println("\nTimeStamp\tSevere\tWarning");
System.out.println("--------------------------------");
List<Map<String,Object>> list = stats.getErrorInformation();
for (Map<String,Object> item : list) {
long timeStamp = (Long) item.get(LogAnalyzer.TIMESTAMP_KEY);
int severeCount = (Integer) item.get(LogAnalyzer.SEVERE_COUNT_KEY);
int warningCount =
(Integer) item.get(LogAnalyzer.WARNING_COUNT_KEY);
System.out.printf("%1$s\t%2$s\t%3$s\n",
sdf.format(new java.util.Date(timeStamp)),
severeCount, warningCount);
}
for (Map<String,Object> item : list) {
long timeStamp = (Long) item.get(LogAnalyzer.TIMESTAMP_KEY);
Map<String,Integer> map =
stats.getErrorDistribution(timeStamp, Level.SEVERE);
System.out.printf("\nModuleId\tLevel.SEVERE\t(%1$s)\n",
sdf.format(new java.util.Date(timeStamp)));
System.out.println("------------------------------------------");
for (String moduleId : map.keySet()) {
System.out.printf("%1$s\t%2$s\n", moduleId, map.get(moduleId));
}
map = stats.getErrorDistribution(timeStamp, Level.WARNING);
System.out.printf("\nModuleId\tLevel.WARNING\t(%1$s)\n",
sdf.format(new java.util.Date(timeStamp)));
System.out.println("------------------------------------------");
for (String moduleId : map.keySet()) {
System.out.printf("%1$s\t%2$s\n", moduleId, map.get(moduleId));
}
}
| public static void | registerStartupTime()
singleton(); // Loads the class which sets up the startup time.
| public void | setIntervalDuration(long interval)Set interval duration. The interval is the duration, in milli seconds,
between consecutive time samples. For example, if the interval is 1 hour,
statistical data collected over time, will be organized by the hour.
this.interval = interval;
| public void | setNumOfIntervals(int intervals)Set the number of time intervals, for which statistics will be
maintained in memory. For example, if the interval is 1 hour, and
number of intervals is 10, statistics will be maintained for the most
recent 10 hours.
if ((intervals < MIN_INTERVALS) || (intervals > MAX_INTERVALS)) {
throw new IllegalArgumentException(
"Number of intervals must be between " + MIN_INTERVALS +
" and " + MAX_INTERVALS);
}
numOfIntervals = intervals;
| public static com.sun.enterprise.server.logging.stats.ErrorStatistics | singleton()
return singleton;
| private void | trimHourMap(long refTimeStamp)
Long[] timeStamps = intervalMap.keySet().toArray(new Long[0]);
for (int i = 0; i < timeStamps.length; i++) {
long timeStamp = timeStamps[i];
if ((refTimeStamp-timeStamp) >= interval*getNumOfIntervals()) {
intervalMap.remove(timeStamp);
}
}
| public void | updateStatistics(java.util.logging.LogRecord record)This method is not synchronized. The caller must ensure that
this method is not used in a concurrent fashion. Currently,
the only caller is publish() method of FileandSyslogHandler.
// Get information from log record.
long logTimeStamp = record.getMillis();
String logModuleId = record.getLoggerName();
if (logModuleId == null) {
Logger logger = LogDomains.getLogger(LogDomains.CORE_LOGGER);
logger.log(
Level.WARNING, "Update error statistics failed",
(new Throwable("Logger name is null.")).fillInStackTrace());
return;
}
Level logLevel = record.getLevel();
// Sanity check.
/*
boolean isAnErrorRecord =
logLevel.equals(Level.SEVERE) || logLevel.equals(Level.WARNING);
if (!isAnErrorRecord) {
return; // no op.
}
*/
// Calculate the timestamp bucket for the log record.
long hoursElapsed = (logTimeStamp-startTimeStamp)/interval;
long timeStamp = startTimeStamp + hoursElapsed*interval;
/*
System.out.println((new java.text.SimpleDateFormat("HH:mm:ss")).
format(new java.util.Date(timeStamp)));
*/
// Update the error statistics.
HashMap<String,ErrorCount> moduleMap = intervalMap.get(timeStamp);
if (moduleMap == null) {
moduleMap = new HashMap<String,ErrorCount>();
trimHourMap(timeStamp); // removes stale entries
intervalMap.put(Long.valueOf(timeStamp), moduleMap);
}
ErrorCount errorCount = moduleMap.get(logModuleId);
if (errorCount == null) {
errorCount = new ErrorCount();
moduleMap.put(logModuleId, errorCount);
}
if (logLevel.equals(Level.SEVERE)) {
errorCount.setSevereCount(errorCount.getSevereCount()+1);
} else { // Level == WARNING
errorCount.setWarningCount(errorCount.getWarningCount()+1);
}
|
|