Search in sources :

Example 21 with LogMessage

use of com.emc.storageos.systemservices.impl.logsvc.LogMessage in project coprhd-controller by CoprHD.

the class LogReader method readNextLogMessage.

/**
 * Read one log message from log file
 *
 * @return
 * @throws IOException
 */
@Override
public LogMessage readNextLogMessage() {
    // logger.info("readMessage()");
    try {
        if (reader == null) {
            return null;
        }
        while (true) {
            String line = reader.readLine();
            if (line == null) {
                close();
                if (currentLog != null && matchRegex(currentLog)) {
                    incrementLogCount(currentLog);
                    return currentLog;
                }
                return null;
            }
            fileLineNumber++;
            LogMessage nextLog = null;
            if (parser != null) {
                // already find the match parser
                nextLog = parser.parseLine(line, request);
            } else {
                // match parser
                for (LogParser parser : parserTable) {
                    nextLog = parser.parseLine(line, request);
                    if (!nextLog.isContinuation()) {
                        this.parser = parser;
                        break;
                    }
                }
                // this line does not match all parsers, skip it and write it into status
                if (nextLog == null || nextLog.isContinuation()) {
                    status.appendInfo(this.filePath, fileLineNumber);
                    continue;
                }
            }
            if (nextLog.isContinuation()) {
                if (currentLog != null) {
                    currentLog.appendMessage(LogUtil.stringToBytes(line));
                }
            } else if (nextLog.isRejected()) {
                // logs are only rejected in the first line
                if (currentLog != null) {
                    LogMessage returnedLog = currentLog;
                    currentLog = null;
                    if (matchRegex(returnedLog)) {
                        incrementLogCount(returnedLog);
                        return returnedLog;
                    }
                // else read the next line
                }
            } else if (nextLog.isRejectedLast()) {
                // log is too late
                if (currentLog != null) {
                    LogMessage returnedLog = currentLog;
                    currentLog = null;
                    if (matchRegex(returnedLog)) {
                        incrementLogCount(returnedLog);
                        return returnedLog;
                    }
                // else break from the loop
                }
                break;
            } else {
                // accepted as the first line of a new log message
                if (currentLog != null) {
                    LogMessage returnedLog = currentLog;
                    currentLog = nextLog;
                    // skip all but the last one
                    if (returnedLog.isHeader() && currentLog.isHeader()) {
                        continue;
                    }
                    if (matchRegex(returnedLog)) {
                        incrementLogCount(returnedLog);
                        return returnedLog;
                    }
                // else read the next line
                } else {
                    currentLog = nextLog;
                }
            }
        }
    } catch (IOException e) {
        status.appendErrFileName(filePath);
    }
    return null;
}
Also used : LogMessage(com.emc.storageos.systemservices.impl.logsvc.LogMessage) LogParser(com.emc.storageos.systemservices.impl.logsvc.parse.LogParser) IOException(java.io.IOException)

Example 22 with LogMessage

use of com.emc.storageos.systemservices.impl.logsvc.LogMessage in project coprhd-controller by CoprHD.

the class ParserTest method testNginxAccessLog.

@Test
public void testNginxAccessLog() throws Exception {
    LogReader reader = null;
    String regexStr = ".*login HTTP.*";
    Calendar calendar = Calendar.getInstance();
    calendar.set(2014, 1, 20, 16, 38, 16);
    Date startTimeFilter = calendar.getTime();
    calendar.set(2014, 6, 16, 16, 38, 0);
    Date endTimeFilter = calendar.getTime();
    LogRequest request = new LogRequest.Builder().regex(regexStr).startTime(startTimeFilter).endTime(endTimeFilter).build();
    LogStatusInfo status = new LogStatusInfo();
    // "/opt/storageos/logs/nginx_access.log";
    String filePath = nginxAccessLogPath;
    String baseName = "nginx";
    reader = new LogReader(filePath, request, status, baseName);
    final LogMessage log = reader.readNextLogMessage();
    assertTrue("log is null", log != null);
    assertTrue("log message is null", !new String(log.getLogContent()).equals("null"));
    assertTrue("log message does not match regex", Pattern.compile(regexStr, Pattern.DOTALL | Pattern.MULTILINE).matcher(LogUtil.bytesToString(log.getLogContent())).matches());
    assertTrue("log time does not fit time filter", log.getTime() >= startTimeFilter.getTime() && log.getTime() <= endTimeFilter.getTime());
    assertTrue("log file name is not null", new String(log.getFileName()).equals("null"));
    assertTrue("log thread name not is null", new String(log.getThreadName()).equals("null"));
    assertTrue("log level is not null", new String(log.getLevel()).equals("null"));
    assertTrue("log line is not -1", new String(log.getLineNumber()).equals("-1"));
}
Also used : LogRequest(com.emc.vipr.model.sys.logging.LogRequest) LogStatusInfo(com.emc.storageos.systemservices.impl.logsvc.LogStatusInfo) LogMessage(com.emc.storageos.systemservices.impl.logsvc.LogMessage) Calendar(java.util.Calendar) LogReader(com.emc.storageos.systemservices.impl.logsvc.stream.LogReader) Date(java.util.Date) Test(org.junit.Test)

Example 23 with LogMessage

use of com.emc.storageos.systemservices.impl.logsvc.LogMessage in project coprhd-controller by CoprHD.

the class ParserTest method testParticularLog.

@Test
public void testParticularLog() throws Exception {
    LogReader reader = null;
    LogRequest request = new LogRequest.Builder().build();
    LogStatusInfo status = new LogStatusInfo();
    String filePath = particularLogPath;
    String baseName = "log";
    reader = new LogReader(filePath, request, status, baseName);
    final LogMessage log = reader.readNextLogMessage();
    assertTrue("log is null", log != null);
    // assertTrue("log message is null", log.getLogContent() != null);
    assertTrue("log message is null", log.getLogContent() != null);
    assertTrue("log file name is null", log.getFileName() != null);
    assertTrue("log thread name is null", log.getThreadName() != null);
    assertTrue("log level is not Error", new String(log.getLevel()).equals("ERROR"));
    assertTrue("log line is null", log.getLineNumber() != null);
}
Also used : LogRequest(com.emc.vipr.model.sys.logging.LogRequest) LogStatusInfo(com.emc.storageos.systemservices.impl.logsvc.LogStatusInfo) LogMessage(com.emc.storageos.systemservices.impl.logsvc.LogMessage) LogReader(com.emc.storageos.systemservices.impl.logsvc.stream.LogReader) Test(org.junit.Test)

Example 24 with LogMessage

use of com.emc.storageos.systemservices.impl.logsvc.LogMessage in project coprhd-controller by CoprHD.

the class ParserTest method testNginxErrorLog.

@Test
public void testNginxErrorLog() throws Exception {
    LogReader reader = null;
    String regexStr = ".*server: localhost, request:.*";
    Calendar calendar = Calendar.getInstance();
    calendar.set(2014, 1, 20, 16, 38, 16);
    Date startTimeFilter = calendar.getTime();
    calendar.set(2014, 6, 16, 16, 38, 0);
    Date endTimeFilter = calendar.getTime();
    LogRequest request = new LogRequest.Builder().regex(regexStr).startTime(startTimeFilter).endTime(endTimeFilter).build();
    LogStatusInfo status = new LogStatusInfo();
    // "/opt/storageos/logs/nginx_error.log";
    String filePath = nginxErrorLogPath;
    String baseName = "nginx";
    reader = new LogReader(filePath, request, status, baseName);
    final LogMessage log = reader.readNextLogMessage();
    assertTrue("log is null", log != null);
    assertTrue("log message is null", !new String(log.getLogContent()).equals("null"));
    assertTrue("log message does not match regex", Pattern.compile(regexStr, Pattern.DOTALL | Pattern.MULTILINE).matcher(LogUtil.bytesToString(log.getLogContent())).matches());
    assertTrue("log time does not fit time filter", log.getTime() >= startTimeFilter.getTime() && log.getTime() <= endTimeFilter.getTime());
    assertTrue("log file name is not null", new String(log.getFileName()).equals("null"));
    assertTrue("log thread name not is null", new String(log.getThreadName()).equals("null"));
    assertTrue("log level is not Error", new String(log.getLevel()).equalsIgnoreCase("ERROR"));
    assertTrue("log line is not -1", new String(log.getLineNumber()).equals("-1"));
}
Also used : LogRequest(com.emc.vipr.model.sys.logging.LogRequest) LogStatusInfo(com.emc.storageos.systemservices.impl.logsvc.LogStatusInfo) LogMessage(com.emc.storageos.systemservices.impl.logsvc.LogMessage) Calendar(java.util.Calendar) LogReader(com.emc.storageos.systemservices.impl.logsvc.stream.LogReader) Date(java.util.Date) Test(org.junit.Test)

Example 25 with LogMessage

use of com.emc.storageos.systemservices.impl.logsvc.LogMessage in project coprhd-controller by CoprHD.

the class SysLogReaderTest method testAllLogFormatSmallFileNoFilterSysParser.

/**
 * Test if readMessage() can read and parse syssvc file which has different
 * format logs correctly
 */
@Test
public void testAllLogFormatSmallFileNoFilterSysParser() throws Exception {
    System.out.println("starting testAllLogFormatSmallFileNoFilterSysParser");
    LogStatusInfo status = new LogStatusInfo();
    String filePath = "src/main/data/testReaderData/testSyslogParser.log";
    LogReader reader = null;
    LogRequest req = new LogRequest.Builder().build();
    reader = new LogReader(filePath, req, status, null);
    BufferedReader br = new BufferedReader(new FileReader("src/main/data/testReaderData/testSyslogParser.log"));
    while (true) {
        LogMessage log = reader.readNextLogMessage();
        String line = br.readLine();
        if (line == null) {
            assertNull("Log should be null", log);
            break;
        }
        assertEquals("whole log message mismatch", log.toStringOriginalFormatSysLog(), line);
    }
    br.close();
    System.out.println("done testAllLogFormatSmallFileNoFilterSysParser");
}
Also used : LogStatusInfo(com.emc.storageos.systemservices.impl.logsvc.LogStatusInfo) LogRequest(com.emc.vipr.model.sys.logging.LogRequest) LogMessage(com.emc.storageos.systemservices.impl.logsvc.LogMessage) LogReader(com.emc.storageos.systemservices.impl.logsvc.stream.LogReader) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Test(org.junit.Test)

Aggregations

LogMessage (com.emc.storageos.systemservices.impl.logsvc.LogMessage)25 LogRequest (com.emc.vipr.model.sys.logging.LogRequest)15 Test (org.junit.Test)15 LogStatusInfo (com.emc.storageos.systemservices.impl.logsvc.LogStatusInfo)12 Date (java.util.Date)9 LogReader (com.emc.storageos.systemservices.impl.logsvc.stream.LogReader)7 ArrayList (java.util.ArrayList)7 Calendar (java.util.Calendar)6 File (java.io.File)5 Ignore (org.junit.Ignore)5 LogFileStream (com.emc.storageos.systemservices.impl.logsvc.stream.LogFileStream)4 LogStreamMerger (com.emc.storageos.systemservices.impl.logsvc.merger.LogStreamMerger)3 IOException (java.io.IOException)3 BufferedOutputStream (java.io.BufferedOutputStream)2 BufferedReader (java.io.BufferedReader)2 FileReader (java.io.FileReader)2 DateFormat (java.text.DateFormat)2 SimpleDateFormat (java.text.SimpleDateFormat)2 APIException (com.emc.storageos.svcs.errorhandling.resources.APIException)1 InternalServerErrorException (com.emc.storageos.svcs.errorhandling.resources.InternalServerErrorException)1