|
| 1 | +/* Copyright 2016 Google Inc. All Rights Reserved. |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | +// [START logs_API_example] |
| 16 | +package com.example.appengine.logs; |
| 17 | + |
| 18 | +//import com.google.appengine.api.log.*; // Fix these to remove * import |
| 19 | +import com.google.appengine.api.log.AppLogLine; |
| 20 | +import com.google.appengine.api.log.LogQuery; |
| 21 | +import com.google.appengine.api.log.LogServiceFactory; |
| 22 | +import com.google.appengine.api.log.RequestLogs; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.io.PrintWriter; |
| 26 | +import java.util.Calendar; |
| 27 | + |
| 28 | +//import javax.servlet.http.*; // Fix these to remove * import |
| 29 | +import javax.servlet.http.HttpServlet; |
| 30 | +import javax.servlet.http.HttpServletRequest; |
| 31 | +import javax.servlet.http.HttpServletResponse; |
| 32 | + |
| 33 | + |
| 34 | +// Get request logs along with their app log lines and display them 5 at |
| 35 | +// a time, using a Next link to cycle through to the next 5. |
| 36 | +public class LogsServlet extends HttpServlet { |
| 37 | + @Override |
| 38 | + public void doGet(HttpServletRequest req, HttpServletResponse resp) |
| 39 | + throws IOException { |
| 40 | + |
| 41 | + resp.setContentType("text/html"); |
| 42 | + PrintWriter writer = resp.getWriter(); |
| 43 | + // We use this to break out of our iteration loop, limiting record |
| 44 | + // display to 5 request logs at a time. |
| 45 | + int limit = 5; |
| 46 | + |
| 47 | + // This retrieves the offset from the Next link upon user click. |
| 48 | + String offset = req.getParameter("offset"); |
| 49 | + |
| 50 | + // We want the App logs for each request log |
| 51 | + LogQuery query = LogQuery.Builder.withDefaults(); |
| 52 | + query.includeAppLogs(true); |
| 53 | + |
| 54 | + // Set the offset value retrieved from the Next link click. |
| 55 | + if (offset != null) { |
| 56 | + query.offset(offset); |
| 57 | + } |
| 58 | + |
| 59 | + // This gets filled from the last request log in the iteration |
| 60 | + String lastOffset = null; |
| 61 | + int count = 0; |
| 62 | + |
| 63 | + // Display a few properties of each request log. |
| 64 | + for (RequestLogs record : LogServiceFactory.getLogService().fetch(query)) { |
| 65 | + writer.println("<br />REQUEST LOG <br />"); |
| 66 | + Calendar cal = Calendar.getInstance(); |
| 67 | + cal.setTimeInMillis(record.getStartTimeUsec() / 1000); |
| 68 | + |
| 69 | + writer.println("IP: " + record.getIp() + "<br />"); |
| 70 | + writer.println("Method: " + record.getMethod() + "<br />"); |
| 71 | + writer.println("Resource " + record.getResource() + "<br />"); |
| 72 | + writer.println(String.format("<br />Date: %s", cal.getTime().toString())); |
| 73 | + |
| 74 | + lastOffset = record.getOffset(); |
| 75 | + |
| 76 | + // Display all the app logs for each request log. |
| 77 | + for (AppLogLine appLog : record.getAppLogLines()) { |
| 78 | + writer.println("<br />" + "APPLICATION LOG" + "<br />"); |
| 79 | + Calendar appCal = Calendar.getInstance(); |
| 80 | + appCal.setTimeInMillis(appLog.getTimeUsec() / 1000); |
| 81 | + writer.println(String.format("<br />Date: %s", |
| 82 | + appCal.getTime().toString())); |
| 83 | + writer.println("<br />Level: " + appLog.getLogLevel() + "<br />"); |
| 84 | + writer.println("Message: " + appLog.getLogMessage() + "<br /> <br />"); |
| 85 | + } //for each log line |
| 86 | + |
| 87 | + if (++count >= limit) { |
| 88 | + break; |
| 89 | + } |
| 90 | + } // for each record |
| 91 | + |
| 92 | + // When the user clicks this link, the offset is processed in the |
| 93 | + // GET handler and used to cycle through to the next 5 request logs. |
| 94 | + writer.println(String.format("<br><a href=\"/?offset=%s\">Next</a>", |
| 95 | + lastOffset)); |
| 96 | + } // end doGet |
| 97 | +} //end class |
| 98 | +// [END logs_API_example] |
| 99 | + |
0 commit comments