end = maxLogs;
}
try {
logStream = getInputStream(logFile, tenantId, serviceName);
} catch (Exception e) {
throw new LogViewerException("Cannot find the specified file location to the log file",
e);
}
DataInputStream dataInput = new DataInputStream(logStream);
int index = 1;
String line;
boolean isSyslogFile;
try {
isSyslogFile = isSysLogAppender(tenantId);
} catch (Exception e1) {
throw new LogViewerException("Cannot validate syslog appender", e1);
}
try {
while ((line = dataInput.readLine()) != null) {
if (isSyslogFile) {
line = removeSyslogHeader(line);
}
line = cleanLogHeader(line, tenantId);
if ((index <= end && index > start)) {
// When if a log entry has multiple lines (ie exception ect)
// it waits for valid log header,
// and add the multiple log lines to the specific log header
// (since we are reading from bottom up
// those multiple lines belongs to the next valid log
// header.
LogMessage logMessage = null;
if (isErrorHeader(line)) {
if (!errorLine.equals("")) {
errorLine = (String) errorLine.subSequence(0, (errorLine.length() - 1));
logMessage = getLogMessageForType(errorLine);
if (logMessage != null) {
logsList.add(logMessage);
}
errorLine = "";
}
// when there are log messages with multiple lines one
// after the other
// next line is also considered as a error line
errorLine = line;
} else if (isFatalHeader(line)) {
if (!errorLine.equals("")) {
errorLine = (String) errorLine.subSequence(0, (errorLine.length() - 1));
logMessage = getLogMessageForType(errorLine);
if (logMessage != null) {
logsList.add(logMessage);
}
errorLine = "";
}
errorLine = line;
} else if (isTraceHeader(line)) {
if (!errorLine.equals("")) {
errorLine = (String) errorLine.subSequence(0, (errorLine.length() - 1));
logMessage = getLogMessageForType(errorLine);
if (logMessage != null) {
logsList.add(logMessage);
}
errorLine = "";
}
errorLine = line;
} else if (isInfoHeader(line)) {
if (!errorLine.equals("")) {
errorLine = (String) errorLine.subSequence(0, (errorLine.length() - 1));
logMessage = getLogMessageForType(errorLine);
if (logMessage != null) {
logsList.add(logMessage);
}
errorLine = "";
}
errorLine = line;
} else if (isWarnHeader(line)) {
if (!errorLine.equals("")) {
errorLine = (String) errorLine.subSequence(0, (errorLine.length() - 1));
logMessage = getLogMessageForType(errorLine);
if (logMessage != null) {
logsList.add(logMessage);
}
errorLine = "";
}
errorLine = line;
} else if (isDebugHeader(line)) {
if (!errorLine.equals("")) {
errorLine = (String) errorLine.subSequence(0, (errorLine.length() - 1));
logMessage = getLogMessageForType(errorLine);
if (logMessage != null) {
logsList.add(logMessage);
}
errorLine = "";
}
errorLine = line;
} else if (!isLogHeader(line)) {
// if a log line has no valid log header that log line
// is considered as a error line.
errorLine = errorLine + line + LoggingConstants.RegexPatterns.NEW_LINE;
} else if (isLogHeader(line)) {
if (!errorLine.equals("")) {
errorLine = (String) errorLine.subSequence(0, (errorLine.length() - 1));
logMessage = getLogMessageForType(errorLine);
if (logMessage != null) {
logsList.add(logMessage);
}
errorLine = "";
}
logMessage = getLogMessageForType(line);
if (logMessage != null) {
logsList.add(logMessage);
}
} else {
log.warn("The log message " + line + " is ignored.");
}
}
index++;
}
if (!errorLine.equals("")) {
LogMessage logMessage = getLogMessageForType(errorLine);
if (logMessage != null) {
logsList.add(logMessage);
} else {
log.warn("The log message " + errorLine + " is ignored.");
}
}
dataInput.close();
} catch (IOException e) {
throw new LogViewerException("Cannot read the log file", e);
}
return logsList.toArray(new LogMessage[logsList.size()]);
}