-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Moved Logs sample to Github #166
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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][appid] on [Google App | ||
Engine][ae-docs]. | ||
|
||
[appid]: 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://developers.google.com/console/help/new/#projectnumber) and SOME-VERSION with the desired version number. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Link seems broken to me when I try it. :-( Looks like the content moved to https://support.google.com/cloud/answer/6158840 |
||
|
||
$ 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. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Copyright 2015 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 xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The xmlns and xsi attributes are optional now. It works without them. |
||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<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> | ||
|
||
</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> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* 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.*; // Fix these to remove * import | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please strike. |
||
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 java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.util.Calendar; | ||
|
||
//import javax.servlet.http.*; // Fix these to remove * import | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably should remove this line. |
||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nit, optional] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It takes the line to 91 columns and looks much wider than the rest of the page. Since I'm expecting the line width to change to 80 as soon as I move it, I'm going to leave it that way for now. |
||
|
||
resp.setContentType("text/html"); | ||
PrintWriter writer = resp.getWriter(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should output the required tags here:
See: http://google.github.io/styleguide/htmlcssguide.xml?showone=HTML_Validity#HTML_Validity There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like that, but google html style guide says don't use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I got this directly from the same style guide. |
||
// 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 />"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We use HTML5, not XHTML, no self-closing tags, please. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. HTML style guide: http://google.github.io/styleguide/htmlcssguide.xml |
||
Calendar cal = Calendar.getInstance(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Google strongly discourages the the use of |
||
cal.setTimeInMillis(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", cal.getTime().toString())); | ||
|
||
lastOffset = record.getOffset(); | ||
|
||
// Display all the app logs for each request log. | ||
for (AppLogLine appLog : record.getAppLogLines()) { | ||
writer.println("<br />" + "APPLICATION LOG" + "<br />"); | ||
Calendar appCal = Calendar.getInstance(); | ||
appCal.setTimeInMillis(appLog.getTimeUsec() / 1000); | ||
writer.println(String.format("<br />Date: %s", | ||
appCal.getTime().toString())); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This indentation seems off. |
||
writer.println("<br />Level: " + appLog.getLogLevel() + "<br />"); | ||
writer.println("Message: " + appLog.getLogMessage() + "<br /> <br />"); | ||
} //for each log line | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nit, optional] missing space before |
||
|
||
if (++count >= limit) { | ||
break; | ||
} | ||
} // for each record | ||
|
||
// 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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This indendentation seems off, too. Per the "rectangle rule" we'd prefer to break before |
||
} // end doGet | ||
} //end class | ||
// [END logs_API_example] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. License header? |
||
<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> |
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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. License header? (I think not strictly necessary for config files, but since comment syntax is supported, we have tended to throw them in) |
||
<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> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd recommend something like
[Logs API][log-docs]
instead of[appid]
.