-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #166 from laurieaw/master
Moved Logs sample to Github
- Loading branch information
Showing
7 changed files
with
281 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <application> tag in src/main/webapp/WEB-INF/appengine-web.xml | ||
with your project name. | ||
|
||
2. Update the <version> 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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
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. | ||
--> | ||
<project> | ||
<modelVersion>4.0.0</modelVersion> | ||
<packaging>war</packaging> | ||
<version>1.0-SNAPSHOT</version> | ||
<groupId>com.example.appengine</groupId> | ||
<artifactId>appengine-logs</artifactId> | ||
<!-- Parent POM defines ${appengine.sdk.version} (updates frequently). --> | ||
<parent> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>doc-samples</artifactId> | ||
<version>1.0.0</version> | ||
<relativePath>../..</relativePath> | ||
</parent> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.appengine</groupId> | ||
<artifactId>appengine-api-1.0-sdk</artifactId> | ||
<version>${appengine.sdk.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.guava</groupId> | ||
<artifactId>guava</artifactId> | ||
<version>19.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>servlet-api</artifactId> | ||
<version>2.5</version> | ||
<type>jar</type> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.json</groupId> | ||
<artifactId>json</artifactId> | ||
<version>20151123</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>joda-time</groupId> | ||
<artifactId>joda-time</artifactId> | ||
<version>2.9.3</version> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<!-- for hot reload of the web application --> | ||
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<version>3.3</version> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>1.7</source> | ||
<target>1.7</target> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>com.google.appengine</groupId> | ||
<artifactId>appengine-maven-plugin</artifactId> | ||
<version>${appengine.sdk.version}</version> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
97 changes: 97 additions & 0 deletions
97
appengine/logs/src/main/java/com/example/appengine/logs/LogsServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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("<!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)); | ||
} | ||
} | ||
// [END logs_API_example] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- [START_EXCLUDE] --> | ||
<!-- | ||
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. | ||
--> | ||
<!-- [END_EXCLUDE] --> | ||
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0"> | ||
<application>YOUR-PROJECT-ID</application> | ||
<version>YOUR-VERSION-NUMBER</version> | ||
<threadsafe>true</threadsafe> | ||
<system-properties> | ||
<property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/> | ||
</system-properties> | ||
</appengine-web-app> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: | ||
# | ||
# <system-properties> | ||
# <property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/> | ||
# </system-properties> | ||
# | ||
|
||
# Set the default logging level for all loggers to WARNING | ||
.level = WARNING | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- [START_EXCLUDE] --> | ||
<!-- | ||
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. | ||
--> | ||
<!-- [END_EXCLUDE] --> | ||
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" | ||
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" | ||
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" | ||
version="2.5"> | ||
<servlet> | ||
<servlet-name>logs</servlet-name> | ||
<servlet-class>com.example.appengine.logs.LogsServlet</servlet-class> | ||
</servlet> | ||
<servlet-mapping> | ||
<servlet-name>logs</servlet-name> | ||
<url-pattern>/</url-pattern> | ||
</servlet-mapping> | ||
</web-app> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters