Skip to content

Commit ced4fa6

Browse files
committed
Merge pull request GoogleCloudPlatform#166 from laurieaw/master
Moved Logs sample to Github
2 parents 2d0f513 + a864a97 commit ced4fa6

File tree

7 files changed

+281
-0
lines changed

7 files changed

+281
-0
lines changed

appengine/logs/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Users Authentication sample for Google App Engine
2+
3+
This sample demonstrates how to use the [Logs API][log-docs] on [Google App
4+
Engine][ae-docs].
5+
6+
[log-docs]: https://cloud.google.com/appengine/docs/java/logs/
7+
[ae-docs]: https://cloud.google.com/appengine/docs/java/
8+
9+
## Running locally
10+
11+
The Logs API only generates output for deployed apps, so this program should not be run locally.
12+
13+
## Deploying
14+
15+
This example uses the
16+
[Maven gcloud plugin](https://cloud.google.com/appengine/docs/java/managed-vms/maven).
17+
18+
In the following command, replace YOUR-PROJECT-ID with your
19+
[Google Cloud Project ID](https://support.google.com/cloud/answer/6158840) and SOME-VERSION with the desired version number.
20+
21+
$ mvn appengine:update -Dappengine.appId=YOUR-PROJECT-ID -Dappengine.version=SOME-VERSION
22+
23+
## Setup
24+
To save your project settings so that you don't need to enter the
25+
parameters, you can:
26+
27+
1. Update the <application> tag in src/main/webapp/WEB-INF/appengine-web.xml
28+
with your project name.
29+
30+
2. Update the <version> tag in src/main/webapp/WEB-INF/appengine-web.xml
31+
with a valid version number.
32+
33+
34+
You will now be able to run
35+
36+
$ mvn appengine:update
37+
38+
without the need for any additional parameters.
39+

appengine/logs/pom.xml

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2016 Google Inc. All Rights Reserved.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<project>
18+
<modelVersion>4.0.0</modelVersion>
19+
<packaging>war</packaging>
20+
<version>1.0-SNAPSHOT</version>
21+
<groupId>com.example.appengine</groupId>
22+
<artifactId>appengine-logs</artifactId>
23+
<!-- Parent POM defines ${appengine.sdk.version} (updates frequently). -->
24+
<parent>
25+
<groupId>com.google.cloud</groupId>
26+
<artifactId>doc-samples</artifactId>
27+
<version>1.0.0</version>
28+
<relativePath>../..</relativePath>
29+
</parent>
30+
<dependencies>
31+
<dependency>
32+
<groupId>com.google.appengine</groupId>
33+
<artifactId>appengine-api-1.0-sdk</artifactId>
34+
<version>${appengine.sdk.version}</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>com.google.guava</groupId>
38+
<artifactId>guava</artifactId>
39+
<version>19.0</version>
40+
</dependency>
41+
<dependency>
42+
<groupId>javax.servlet</groupId>
43+
<artifactId>servlet-api</artifactId>
44+
<version>2.5</version>
45+
<type>jar</type>
46+
<scope>provided</scope>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.json</groupId>
50+
<artifactId>json</artifactId>
51+
<version>20151123</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>joda-time</groupId>
55+
<artifactId>joda-time</artifactId>
56+
<version>2.9.3</version>
57+
</dependency>
58+
</dependencies>
59+
<build>
60+
<!-- for hot reload of the web application -->
61+
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
62+
<plugins>
63+
<plugin>
64+
<groupId>org.apache.maven.plugins</groupId>
65+
<version>3.3</version>
66+
<artifactId>maven-compiler-plugin</artifactId>
67+
<configuration>
68+
<source>1.7</source>
69+
<target>1.7</target>
70+
</configuration>
71+
</plugin>
72+
<plugin>
73+
<groupId>com.google.appengine</groupId>
74+
<artifactId>appengine-maven-plugin</artifactId>
75+
<version>${appengine.sdk.version}</version>
76+
</plugin>
77+
</plugins>
78+
</build>
79+
</project>
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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.AppLogLine;
19+
import com.google.appengine.api.log.LogQuery;
20+
import com.google.appengine.api.log.LogServiceFactory;
21+
import com.google.appengine.api.log.RequestLogs;
22+
23+
import org.joda.time.DateTime;
24+
25+
import java.io.IOException;
26+
import java.io.PrintWriter;
27+
28+
import javax.servlet.http.HttpServlet;
29+
import javax.servlet.http.HttpServletRequest;
30+
import javax.servlet.http.HttpServletResponse;
31+
32+
33+
// Get request logs along with their app log lines and display them 5 at
34+
// a time, using a Next link to cycle through to the next 5.
35+
public class LogsServlet extends HttpServlet {
36+
@Override
37+
public void doGet(HttpServletRequest req, HttpServletResponse resp)
38+
throws IOException {
39+
40+
resp.setContentType("text/html");
41+
PrintWriter writer = resp.getWriter();
42+
writer.println("<!DOCTYPE html>");
43+
writer.println("<meta charset=\"utf-8\">");
44+
writer.println("<title>App Engine Logs Sample</title>");
45+
46+
// We use this to break out of our iteration loop, limiting record
47+
// display to 5 request logs at a time.
48+
int limit = 5;
49+
50+
// This retrieves the offset from the Next link upon user click.
51+
String offset = req.getParameter("offset");
52+
53+
// We want the App logs for each request log
54+
LogQuery query = LogQuery.Builder.withDefaults();
55+
query.includeAppLogs(true);
56+
57+
// Set the offset value retrieved from the Next link click.
58+
if (offset != null) {
59+
query.offset(offset);
60+
}
61+
62+
// This gets filled from the last request log in the iteration
63+
String lastOffset = null;
64+
int count = 0;
65+
66+
// Display a few properties of each request log.
67+
for (RequestLogs record : LogServiceFactory.getLogService().fetch(query)) {
68+
writer.println("<br>REQUEST LOG <br>");
69+
DateTime reqTime = new DateTime(record.getStartTimeUsec() / 1000);
70+
writer.println("IP: " + record.getIp() + "<br>");
71+
writer.println("Method: " + record.getMethod() + "<br>");
72+
writer.println("Resource " + record.getResource() + "<br>");
73+
writer.println(String.format("<br>Date: %s", reqTime.toString()));
74+
75+
lastOffset = record.getOffset();
76+
77+
// Display all the app logs for each request log.
78+
for (AppLogLine appLog : record.getAppLogLines()) {
79+
writer.println("<br>" + "APPLICATION LOG" + "<br>");
80+
DateTime appTime = new DateTime(appLog.getTimeUsec() / 1000);
81+
writer.println(String.format("<br>Date: %s", appTime.toString()));
82+
writer.println("<br>Level: " + appLog.getLogLevel() + "<br>");
83+
writer.println("Message: " + appLog.getLogMessage() + "<br> <br>");
84+
}
85+
86+
if (++count >= limit) {
87+
break;
88+
}
89+
}
90+
91+
// When the user clicks this link, the offset is processed in the
92+
// GET handler and used to cycle through to the next 5 request logs.
93+
writer.println(String.format("<br><a href=\"/?offset=%s\">Next</a>", lastOffset));
94+
}
95+
}
96+
// [END logs_API_example]
97+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- [START_EXCLUDE] -->
3+
<!--
4+
Copyright 2016 Google Inc. All Rights Reserved.
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
http://www.apache.org/licenses/LICENSE-2.0
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+
<!-- [END_EXCLUDE] -->
16+
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
17+
<application>YOUR-PROJECT-ID</application>
18+
<version>YOUR-VERSION-NUMBER</version>
19+
<threadsafe>true</threadsafe>
20+
<system-properties>
21+
<property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
22+
</system-properties>
23+
</appengine-web-app>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# A default java.util.logging configuration.
2+
# (All App Engine logging is through java.util.logging by default).
3+
#
4+
# To use this configuration, copy it into your application's WEB-INF
5+
# folder and add the following to your appengine-web.xml:
6+
#
7+
# <system-properties>
8+
# <property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
9+
# </system-properties>
10+
#
11+
12+
# Set the default logging level for all loggers to WARNING
13+
.level = WARNING
14+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- [START_EXCLUDE] -->
3+
<!--
4+
Copyright 2016 Google Inc. All Rights Reserved.
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
http://www.apache.org/licenses/LICENSE-2.0
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+
<!-- [END_EXCLUDE] -->
16+
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
17+
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
18+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
19+
version="2.5">
20+
<servlet>
21+
<servlet-name>logs</servlet-name>
22+
<servlet-class>com.example.appengine.logs.LogsServlet</servlet-class>
23+
</servlet>
24+
<servlet-mapping>
25+
<servlet-name>logs</servlet-name>
26+
<url-pattern>/</url-pattern>
27+
</servlet-mapping>
28+
</web-app>

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
<module>appengine/analytics</module>
4747
<module>appengine/appidentity</module>
4848
<module>appengine/helloworld</module>
49+
<module>appengine/logs</module>
4950
<module>appengine/mailgun</module>
5051
<module>appengine/mailjet</module>
5152
<module>appengine/memcache</module>

0 commit comments

Comments
 (0)