Search in sources :

Example 1 with LogQuery

use of com.google.appengine.api.log.LogQuery in project teammates by TEAMMATES.

the class CompileLogsAction method getErrorLogs.

private List<AppLogLine> getErrorLogs() {
    LogService logService = LogServiceFactory.getLogService();
    long endTime = new Date().getTime();
    // Sets the range to 6 minutes to slightly overlap the 5 minute email timer
    long queryRange = 1000 * 60 * 6;
    long startTime = endTime - queryRange;
    LogQuery q = LogQuery.Builder.withDefaults().includeAppLogs(true).startTimeMillis(startTime).endTimeMillis(endTime).minLogLevel(LogLevel.ERROR);
    Iterable<RequestLogs> logs = logService.fetch(q);
    List<AppLogLine> errorLogs = new ArrayList<>();
    for (RequestLogs requestLogs : logs) {
        List<AppLogLine> logList = requestLogs.getAppLogLines();
        for (AppLogLine currentLog : logList) {
            LogLevel logLevel = currentLog.getLogLevel();
            if (LogLevel.FATAL == logLevel || LogLevel.ERROR == logLevel) {
                errorLogs.add(currentLog);
            }
        }
    }
    return errorLogs;
}
Also used : LogQuery(com.google.appengine.api.log.LogQuery) ArrayList(java.util.ArrayList) AppLogLine(com.google.appengine.api.log.AppLogLine) RequestLogs(com.google.appengine.api.log.RequestLogs) LogService(com.google.appengine.api.log.LogService) Date(java.util.Date) LogLevel(com.google.appengine.api.log.LogService.LogLevel)

Example 2 with LogQuery

use of com.google.appengine.api.log.LogQuery in project java-docs-samples by GoogleCloudPlatform.

the class LogsServlet method doGet.

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    resp.setContentType("text/html");
    PrintWriter writer = resp.getWriter();
    writer.println("<!DOCTYPE html>");
    writer.println("<meta charset=\"utf-8\">");
    writer.println("<title>App Engine Logs Sample</title>");
    // We use this to break out of our iteration loop, limiting record
    // display to 5 request logs at a time.
    int limit = 5;
    // This retrieves the offset from the Next link upon user click.
    String offset = req.getParameter("offset");
    // We want the App logs for each request log
    LogQuery query = LogQuery.Builder.withDefaults();
    query.includeAppLogs(true);
    // Set the offset value retrieved from the Next link click.
    if (offset != null) {
        query.offset(offset);
    }
    // This gets filled from the last request log in the iteration
    String lastOffset = null;
    int count = 0;
    // Display a few properties of each request log.
    for (RequestLogs record : LogServiceFactory.getLogService().fetch(query)) {
        writer.println("<br>REQUEST LOG <br>");
        DateTime reqTime = new DateTime(record.getStartTimeUsec() / 1000);
        writer.println("IP: " + record.getIp() + "<br>");
        writer.println("Method: " + record.getMethod() + "<br>");
        writer.println("Resource " + record.getResource() + "<br>");
        writer.println(String.format("<br>Date: %s", reqTime.toString()));
        lastOffset = record.getOffset();
        // Display all the app logs for each request log.
        for (AppLogLine appLog : record.getAppLogLines()) {
            writer.println("<br>" + "APPLICATION LOG" + "<br>");
            DateTime appTime = new DateTime(appLog.getTimeUsec() / 1000);
            writer.println(String.format("<br>Date: %s", appTime.toString()));
            writer.println("<br>Level: " + appLog.getLogLevel() + "<br>");
            writer.println("Message: " + appLog.getLogMessage() + "<br> <br>");
        }
        if (++count >= limit) {
            break;
        }
    }
    // When the user clicks this link, the offset is processed in the
    // GET handler and used to cycle through to the next 5 request logs.
    writer.println(String.format("<br><a href=\"/?offset=%s\">Next</a>", lastOffset));
}
Also used : LogQuery(com.google.appengine.api.log.LogQuery) AppLogLine(com.google.appengine.api.log.AppLogLine) RequestLogs(com.google.appengine.api.log.RequestLogs) DateTime(org.joda.time.DateTime) PrintWriter(java.io.PrintWriter)

Aggregations

AppLogLine (com.google.appengine.api.log.AppLogLine)2 LogQuery (com.google.appengine.api.log.LogQuery)2 RequestLogs (com.google.appengine.api.log.RequestLogs)2 LogService (com.google.appengine.api.log.LogService)1 LogLevel (com.google.appengine.api.log.LogService.LogLevel)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 DateTime (org.joda.time.DateTime)1