@Test public void testGenerateCompiledLogsEmail() throws IOException { AppLogLine typicalLogLine = new AppLogLine(); typicalLogLine.setLogLevel(LogLevel.ERROR); typicalLogLine.setLogMessage("Typical log message"); AppLogLine logLineWithLineBreak = new AppLogLine(); logLineWithLineBreak.setLogLevel(LogLevel.ERROR); logLineWithLineBreak.setLogMessage("Log line \n with line break <br> and also HTML br tag"); EmailWrapper email = new EmailGenerator().generateCompiledLogsEmail( Arrays.asList(typicalLogLine, logLineWithLineBreak)); String subject = String.format(EmailType.SEVERE_LOGS_COMPILATION.getSubject(), Config.APP_VERSION); verifyEmail(email, Config.SUPPORT_EMAIL, subject, "/severeLogsCompilationEmail.html"); }
@Test public void logEntry_withMalformationAppLogLine_constructionFail() { ______TS("Fail: log message not in correct format"); AppLogLine appLog = new AppLogLine(); appLog.setLogMessage("TEAMMATESLOG||RANDOM"); ActivityLogEntry entry = ActivityLogEntry.buildFromAppLog(appLog); assertTrue(entry.generateLogMessage().contains(Const.ActivityLog.MESSAGE_ERROR_LOG_MESSAGE_FORMAT)); String logMessageMalformation = "TEAMMATESLOG|||instructorHome|||Pageload|||true|||Instructor" + "|||UserName|||UserId|||UserEmail|||Message|||URL"; appLog.setLogMessage(logMessageMalformation); entry = ActivityLogEntry.buildFromAppLog(appLog); assertTrue(entry.generateLogMessage().contains(Const.ActivityLog.MESSAGE_ERROR_LOG_MESSAGE_FORMAT)); }
protected AppLogLine findLogLine(String text, LogQuery logQuery) { Iterable<RequestLogs> iterable = LogServiceFactory.getLogService().fetch(logQuery); for (RequestLogs logs : iterable) { for (AppLogLine logLine : logs.getAppLogLines()) { if (logLine.getLogMessage().contains(text)) { return logLine; } } } return null; }
@Test public void testLogLineTimeUsec() { String logMark = getTimeStampRandom(); long beforeTimeUsec = System.currentTimeMillis() * 1000; log.log(Level.INFO, "testLogLineTimeUsec " + logMark); long afterTimeUsec = (System.currentTimeMillis() + 1) * 1000; flush(log); sync(7000); int retryMax = 1; AppLogLine logLine = findLogLineContaining(logMark, retryMax); assertNotNull("log with " + logMark + " should exist.", logLine); assertTrue("Expected logLine.getTimeUsec (" + logLine.getTimeUsec() + ") >= beforeTimeUsec (" + beforeTimeUsec + "), but it was not", logLine.getTimeUsec() >= beforeTimeUsec); assertTrue("Expected logLine.getTimeUsec (" + logLine.getTimeUsec() + ") <= afterTimeUsec (" + afterTimeUsec + "), but it was not", logLine.getTimeUsec() <= beforeTimeUsec); }
protected void assertLogContains(String text, LogService.LogLevel logLevel) { int retryMax = 4; AppLogLine logLine = findLogLineContaining(text, retryMax); assertNotNull("log should contain '" + text + "', but it does not", logLine); if (logLevel != null) { assertEquals("incorrect logLevel for text '" + text + "'", logLevel, logLine.getLogLevel()); } }
@Test public void testLogMessageIsFormatted() { // GAE dev server doesn't handle this properly (see http://code.google.com/p/googleappengine/issues/detail?id=8666) String logMark = getTimeStampRandom(); String logMsg = "Parameterized message with params {0} and {1}"; String logExpect = "Parameterized message with params param1 and " + logMark; log.log(Level.INFO, logMsg, new Object[] {"param1", logMark}); flush(log); sync(7000); int retryMax = 1; AppLogLine logLine = findLogLineContaining(logMark, retryMax); assertNotNull("log with " + logMark + " should exist.", logLine); assertTrue("logLine should contain: " + logExpect + "but is: " + logLine, logLine.toString().contains(logExpect)); }
@Test public void logEntry_withAppLogLine_constructSuccessfully() { ______TS("Success: Generate activityLog from appLogLine (with TimeTaken)"); String logMessageWithoutTimeTaken = "TEAMMATESLOG|||instructorHome|||Pageload|||true|||Instructor" + "|||UserName|||UserId|||UserEmail|||Message|||URL|||UserId20151019143729608"; AppLogLine appLog = new AppLogLine(); appLog.setLogMessage(logMessageWithoutTimeTaken + Const.ActivityLog.FIELD_SEPARATOR + "20"); ActivityLogEntry entry = ActivityLogEntry.buildFromAppLog(appLog); assertEquals(logMessageWithoutTimeTaken, entry.generateLogMessage()); assertEquals(20, entry.getActionTimeTaken()); ______TS("Success: Generate activityLog from appLogLine (without TimeTaken)"); appLog.setLogMessage(logMessageWithoutTimeTaken); entry = ActivityLogEntry.buildFromAppLog(appLog); assertEquals(logMessageWithoutTimeTaken, entry.generateLogMessage()); assertEquals(0, entry.getActionTimeTaken()); ______TS("Success with severe log: timeTaken not in correct format"); appLog.setLogMessage(logMessageWithoutTimeTaken + Const.ActivityLog.FIELD_SEPARATOR + "random"); entry = ActivityLogEntry.buildFromAppLog(appLog); assertEquals(logMessageWithoutTimeTaken, entry.generateLogMessage()); assertEquals(0, entry.getActionTimeTaken()); }
@Test public void testLogMessageStartsWithClassAndMethodAndEndsWithNewLine() { String methodName = "testLogMessageStartsWithClassAndMethodAndEndsWithNewLine"; String logMark = getTimeStampRandom(); String msg = "logged message " + logMark; Logger fooLogger = Logger.getLogger("fooLogger"); // logger name is ignored completely (!?) fooLogger.info(msg); flush(fooLogger); sync(7000); int retryMax = 1; AppLogLine appLogLine = findLogLineContaining(msg, retryMax); String expectedMessage = getClass().getName() + " " + methodName + ": " + msg + "\n"; assertEquals(expectedMessage, appLogLine.getLogMessage()); }
@Test public void testLogLinesAreReturnedInSameOrderAsTheyAreLogged() { String logMark = getTimeStampRandom(); String msg1 = "msg 1 " + logMark; String msg2 = "msg 2 " + logMark; log.log(Level.INFO, msg1); log.log(Level.INFO, msg2); flush(log); sync(15000); RequestLogs requestLogs = getCurrentRequestLogs(); Integer msg1Index = null; Integer msg2Index = null; int i = 0; for (AppLogLine appLogLine : requestLogs.getAppLogLines()) { if (appLogLine.getLogMessage().contains(msg1)) { msg1Index = i; } else if (appLogLine.getLogMessage().contains(msg2)) { msg2Index = i; } i++; } assertNotNull("1st log message not found in appLogLines", msg1Index); assertNotNull("2nd log message not found in appLogLines", msg2Index); assertTrue("Expected first logged message to come before second logged message", msg1Index < msg2Index); }