LogCollectorpublic class LogCollector extends Object implements CollectorVisits one or more server.log files to collect log entries
satisfying the log filters - startDate, endDate, minLogLevel and
max number of log entries collected. Collects log entries
pertaingin to ULF only. |
Fields Summary |
---|
private static final SimpleDateFormat | dateFormat | private com.sun.enterprise.diagnostics.ServiceConfig | config | private Date | startDate | private Date | endDate | private String | destFolder | private boolean | partialPrevEntry | private boolean | prevEntryCopied | private String | logFileName | private static Logger | logger |
Constructors Summary |
---|
public LogCollector(Date startDate, Date endDate, String destFolder, com.sun.enterprise.diagnostics.ServiceConfig config)
this.startDate = startDate;
this.endDate = endDate;
this.destFolder = destFolder;
this.config = config;
| public LogCollector(String destFolder, String logFile)
this.destFolder = destFolder;
this.logFileName = logFile;
|
Methods Summary |
---|
public com.sun.enterprise.diagnostics.Data | capture()Captures contents for server.log
int noOfCapturedEntries = 0;
int maxNoOfEntries = Defaults.MAX_NO_OF_ENTRIES;
int minLogLevel = Defaults.MIN_LOG_LEVEL;
if(config != null) {
maxNoOfEntries = config.getMaxNoOfEntries();
minLogLevel = config.getMinLogLevel();
logFileName = config.getLogFile();
if(logFileName.indexOf(config.getRepositoryDir()) == -1)
logFileName = config.getRepositoryDir() + logFileName;
}
String destLogFile = destFolder + Defaults.DEST_LOG_FILE;
// Start reading server.log*, may have to traverse mulitple
// log files when start date and/or end date is specified.
//If startDate != null
// if startDate is not specified, read from server.log only.
List logFiles;
try {
if (startDate != null) {
File logFile = new File(logFileName);
File logDir = new File(logFile.getParent());
String fileNamePrefix = logFile.getName();
logFiles = FileUtils.getFileListing
(logDir, false,
new LogNameFilter
(fileNamePrefix, startDate, endDate),
new LogNameComparator());
if(logFiles != null && logFiles.size() == 0)
logFiles = Arrays.asList(new File[] {new File(logFileName)});
} else
logFiles = Arrays.asList(new File[] {new File(logFileName)});
logger.log(Level.FINE, "diagnostic-service.dest_log_file",
new Object[] {destLogFile} );
Iterator filesIterator = logFiles.iterator();
PrintWriter out ;
String logEntry;
BufferedReader inFile = null;
try {
out = new PrintWriter
(new BufferedWriter(new FileWriter(destLogFile)));
}catch(IOException ioe1) {
File parent = (new File(destLogFile)).getParentFile();
parent.mkdirs();
out = new PrintWriter(new BufferedWriter(new FileWriter(destLogFile)));
}
while (filesIterator.hasNext()) {
if (endDate != null || noOfCapturedEntries < maxNoOfEntries) {
try {
inFile = new BufferedReader
(new FileReader((File) filesIterator.next()));
while ((logEntry = inFile.readLine()) != null) {
try {
if (isValid
(logEntry, startDate, endDate, minLogLevel)) {
out.println(logEntry);
if (endDate == null) {
if (!partialPrevEntry)
noOfCapturedEntries++;
if (noOfCapturedEntries >= maxNoOfEntries) {
break;
}
}
}//if(isValid)
} catch (Exception pe) {
}//catch
}//while
out.flush();
} catch (IOException ioe) {
throw new DiagnosticException(ioe.getMessage());
}
finally {
try {
if (inFile != null) {
inFile.close();
}
} catch (IOException ioe) {
//ignore
}
if(out != null){
out.flush();
out.close();
}
}
}//if (noOfCapturedEntries < maxNoOfEntris)
else
break;
}
return new FileData(destLogFile, DataType.LOG_INFO);
}catch (FileNotFoundException fnfe) {
throw new DiagnosticException(fnfe.getMessage());
}
catch (IOException ioe) {
throw new DiagnosticException(ioe.getMessage());
}
| private boolean | hasEndOfEntry(java.lang.String entry)
return (entry.indexOf(Constants.ENTRY_END_INDICATOR) > -1);
| private boolean | isValid(java.lang.String entry, java.util.Date startDate, java.util.Date endDate, int minLogLevel)Entries are in the following format
[#|yyyy-mm-ddThh:mm:ss.SSS-Z|Log Level|ProductName_Version|LoggerName|
Key Value Pairs|Message|#]
Entries may span multiple lines
//entryDate >= startDate, entryDate <= endDate ,
//entryLogLevel >= minLogLevel
// Blank line
if (entry.length() <= 0)
return false;
//Previous Partial Line was copied, copy blindly
if (partialPrevEntry) {
if (hasEndOfEntry(entry))
partialPrevEntry = false;
if (prevEntryCopied)
return true;
else
return false;
}
// New log entry
int logLevelSepBeginIndex = entry.indexOf
(Constants.FIELD_SEPARATOR, Constants.ENTRY_DATE_BEGIN_INDEX) + 1;
String entryLogLevelStr = entry.substring
(logLevelSepBeginIndex, entry.indexOf(Constants.FIELD_SEPARATOR,
logLevelSepBeginIndex));
int entryLogLevel = Level.parse(entryLogLevelStr).intValue();
// Entry log level > min Log Level
if (entryLogLevel >= minLogLevel) {
Date entryDate = dateFormat.parse
(entry.substring(Constants.ENTRY_DATE_BEGIN_INDEX,
Constants.ENTRY_DATE_BEGIN_INDEX + Constants.ENTRY_DATE_LENGTH));
if (startDate != null && endDate != null) {
if (entryDate.compareTo(startDate) >= 0 &&
entryDate.compareTo(endDate) <= 0)
prevEntryCopied = true;
else
prevEntryCopied = false; // Date comparison fails
} else if (startDate != null && entryDate.compareTo(startDate) < 0) {
prevEntryCopied = false;
} else {
prevEntryCopied = true; //entry is not restricted to date
}
} else
prevEntryCopied = false;
//Determine whether the entry has end indicator
if (!hasEndOfEntry(entry))
partialPrevEntry = true;
return prevEntryCopied;
|
|