Skip to content

Commit 148156d

Browse files
committed
Moved samples for Logs to Github
1 parent 46e05f1 commit 148156d

File tree

7 files changed

+255
-0
lines changed

7 files changed

+255
-0
lines changed

appengine/logs/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Users Authentication sample for Google App Engine
2+
3+
This sample demonstrates how to use the [Logs API][appid] on [Google App
4+
Engine][ae-docs].
5+
6+
[appid]: 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://developers.google.com/console/help/new/#projectnumber).
20+
21+
In the following command, replace YOUR-PROJECT-ID with your
22+
[Google Cloud Project ID](https://developers.google.com/console/help/new/#projectnumber) and SOME-VERSION with the desired version number.
23+
24+
$ mvn appengine:update -Dappengine.appId=YOUR-PROJECT-ID -Dappengine.version=SOME-VERSION
25+
26+
## Setup
27+
To save your project settings so that you don't need to enter the
28+
parameters, you can:
29+
30+
1. Update the <application> tag in src/main/webapp/WEB-INF/appengine-web.xml
31+
with your project name.
32+
33+
2. Update the <version> tag in src/main/webapp/WEB-INF/appengine-web.xml
34+
with a valid version number.
35+
36+
37+
You will now be able to run
38+
39+
$ mvn appengine:update
40+
41+
without the need for any additional parameters.
42+

appengine/logs/pom.xml

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2015 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 xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
18+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
19+
<modelVersion>4.0.0</modelVersion>
20+
<packaging>war</packaging>
21+
<version>1.0-SNAPSHOT</version>
22+
<groupId>com.example.appengine</groupId>
23+
<artifactId>appengine-logs</artifactId>
24+
<!-- Parent POM defines ${appengine.sdk.version} (updates frequently). -->
25+
<parent>
26+
<groupId>com.google.cloud</groupId>
27+
<artifactId>doc-samples</artifactId>
28+
<version>1.0.0</version>
29+
<relativePath>../..</relativePath>
30+
</parent>
31+
<dependencies>
32+
<dependency>
33+
<groupId>com.google.appengine</groupId>
34+
<artifactId>appengine-api-1.0-sdk</artifactId>
35+
<version>${appengine.sdk.version}</version>
36+
</dependency>
37+
<dependency>
38+
<groupId>com.google.guava</groupId>
39+
<artifactId>guava</artifactId>
40+
<version>19.0</version>
41+
</dependency>
42+
<dependency>
43+
<groupId>javax.servlet</groupId>
44+
<artifactId>servlet-api</artifactId>
45+
<version>2.5</version>
46+
<type>jar</type>
47+
<scope>provided</scope>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.json</groupId>
51+
<artifactId>json</artifactId>
52+
<version>20151123</version>
53+
</dependency>
54+
55+
</dependencies>
56+
<build>
57+
<!-- for hot reload of the web application -->
58+
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
59+
<plugins>
60+
<plugin>
61+
<groupId>org.apache.maven.plugins</groupId>
62+
<version>3.3</version>
63+
<artifactId>maven-compiler-plugin</artifactId>
64+
<configuration>
65+
<source>1.7</source>
66+
<target>1.7</target>
67+
</configuration>
68+
</plugin>
69+
<plugin>
70+
<groupId>com.google.appengine</groupId>
71+
<artifactId>appengine-maven-plugin</artifactId>
72+
<version>${appengine.sdk.version}</version>
73+
</plugin>
74+
</plugins>
75+
</build>
76+
</project>
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
3+
<application>YOUR-PROJECT-ID</application>
4+
<version>YOUR-VERSION-NUMBER</version>
5+
<threadsafe>true</threadsafe>
6+
<system-properties>
7+
<property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
8+
</system-properties>
9+
</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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
3+
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
4+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
5+
version="2.5">
6+
<servlet>
7+
<servlet-name>logs</servlet-name>
8+
<servlet-class>com.example.appengine.logs.LogsServlet</servlet-class>
9+
</servlet>
10+
<servlet-mapping>
11+
<servlet-name>logs</servlet-name>
12+
<url-pattern>/</url-pattern>
13+
</servlet-mapping>
14+
</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)