diff --git a/appengine/logs/README.md b/appengine/logs/README.md new file mode 100644 index 00000000000..0d07745e5e8 --- /dev/null +++ b/appengine/logs/README.md @@ -0,0 +1,39 @@ +# Users Authentication sample for Google App Engine + +This sample demonstrates how to use the [Logs API][log-docs] on [Google App +Engine][ae-docs]. + +[log-docs]: https://cloud.google.com/appengine/docs/java/logs/ +[ae-docs]: https://cloud.google.com/appengine/docs/java/ + +## Running locally + +The Logs API only generates output for deployed apps, so this program should not be run locally. + +## Deploying + +This example uses the +[Maven gcloud plugin](https://cloud.google.com/appengine/docs/java/managed-vms/maven). + +In the following command, replace YOUR-PROJECT-ID with your +[Google Cloud Project ID](https://support.google.com/cloud/answer/6158840) and SOME-VERSION with the desired version number. + + $ mvn appengine:update -Dappengine.appId=YOUR-PROJECT-ID -Dappengine.version=SOME-VERSION + +## Setup +To save your project settings so that you don't need to enter the + parameters, you can: + +1. Update the tag in src/main/webapp/WEB-INF/appengine-web.xml + with your project name. + +2. Update the tag in src/main/webapp/WEB-INF/appengine-web.xml + with a valid version number. + + +You will now be able to run + + $ mvn appengine:update + +without the need for any additional parameters. + diff --git a/appengine/logs/pom.xml b/appengine/logs/pom.xml new file mode 100644 index 00000000000..75feb324db1 --- /dev/null +++ b/appengine/logs/pom.xml @@ -0,0 +1,79 @@ + + + + 4.0.0 + war + 1.0-SNAPSHOT + com.example.appengine + appengine-logs + + + com.google.cloud + doc-samples + 1.0.0 + ../.. + + + + com.google.appengine + appengine-api-1.0-sdk + ${appengine.sdk.version} + + + com.google.guava + guava + 19.0 + + + javax.servlet + servlet-api + 2.5 + jar + provided + + + org.json + json + 20151123 + + + joda-time + joda-time + 2.9.3 + + + + + ${project.build.directory}/${project.build.finalName}/WEB-INF/classes + + + org.apache.maven.plugins + 3.3 + maven-compiler-plugin + + 1.7 + 1.7 + + + + com.google.appengine + appengine-maven-plugin + ${appengine.sdk.version} + + + + diff --git a/appengine/logs/src/main/java/com/example/appengine/logs/LogsServlet.java b/appengine/logs/src/main/java/com/example/appengine/logs/LogsServlet.java new file mode 100644 index 00000000000..6ab30b504bd --- /dev/null +++ b/appengine/logs/src/main/java/com/example/appengine/logs/LogsServlet.java @@ -0,0 +1,97 @@ +/* Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +// [START logs_API_example] +package com.example.appengine.logs; + +import com.google.appengine.api.log.AppLogLine; +import com.google.appengine.api.log.LogQuery; +import com.google.appengine.api.log.LogServiceFactory; +import com.google.appengine.api.log.RequestLogs; + +import org.joda.time.DateTime; + +import java.io.IOException; +import java.io.PrintWriter; + +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + + +// Get request logs along with their app log lines and display them 5 at +// a time, using a Next link to cycle through to the next 5. +public class LogsServlet extends HttpServlet { + @Override + public void doGet(HttpServletRequest req, HttpServletResponse resp) + throws IOException { + + resp.setContentType("text/html"); + PrintWriter writer = resp.getWriter(); + writer.println(""); + writer.println(""); + writer.println("App Engine Logs Sample"); + + // 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("
REQUEST LOG
"); + DateTime reqTime = new DateTime(record.getStartTimeUsec() / 1000); + writer.println("IP: " + record.getIp() + "
"); + writer.println("Method: " + record.getMethod() + "
"); + writer.println("Resource " + record.getResource() + "
"); + writer.println(String.format("
Date: %s", reqTime.toString())); + + lastOffset = record.getOffset(); + + // Display all the app logs for each request log. + for (AppLogLine appLog : record.getAppLogLines()) { + writer.println("
" + "APPLICATION LOG" + "
"); + DateTime appTime = new DateTime(appLog.getTimeUsec() / 1000); + writer.println(String.format("
Date: %s", appTime.toString())); + writer.println("
Level: " + appLog.getLogLevel() + "
"); + writer.println("Message: " + appLog.getLogMessage() + "

"); + } + + 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("
Next", lastOffset)); + } +} +// [END logs_API_example] + diff --git a/appengine/logs/src/main/webapp/WEB-INF/appengine-web.xml b/appengine/logs/src/main/webapp/WEB-INF/appengine-web.xml new file mode 100644 index 00000000000..73eefaee076 --- /dev/null +++ b/appengine/logs/src/main/webapp/WEB-INF/appengine-web.xml @@ -0,0 +1,23 @@ + + + + + + YOUR-PROJECT-ID + YOUR-VERSION-NUMBER + true + + + + diff --git a/appengine/logs/src/main/webapp/WEB-INF/logging.properties b/appengine/logs/src/main/webapp/WEB-INF/logging.properties new file mode 100644 index 00000000000..3e7f85b9dc1 --- /dev/null +++ b/appengine/logs/src/main/webapp/WEB-INF/logging.properties @@ -0,0 +1,14 @@ +# A default java.util.logging configuration. +# (All App Engine logging is through java.util.logging by default). +# +# To use this configuration, copy it into your application's WEB-INF +# folder and add the following to your appengine-web.xml: +# +# +# +# +# + +# Set the default logging level for all loggers to WARNING +.level = WARNING + diff --git a/appengine/logs/src/main/webapp/WEB-INF/web.xml b/appengine/logs/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 00000000000..fa2cb4457c4 --- /dev/null +++ b/appengine/logs/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,28 @@ + + + + + + + logs + com.example.appengine.logs.LogsServlet + + + logs + / + + diff --git a/pom.xml b/pom.xml index cb178bd6ae5..b1d2d6ee761 100644 --- a/pom.xml +++ b/pom.xml @@ -46,6 +46,7 @@ appengine/analytics appengine/appidentity appengine/helloworld + appengine/logs appengine/mailgun appengine/mailjet appengine/memcache