Skip to content
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

Merged
merged 4 commits into from
Apr 20, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions appengine/logs/README.md
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
Copy link
Contributor

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].

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.
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

76 changes: 76 additions & 0 deletions appengine/logs/pom.xml
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"
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit, optional] throws IOException looks like it would fit on the previous line with plenty to spare.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should output the required tags here:

<!DOCTYPE html>
<meta charset="utf-8">
<title>App Engine Logs Sample</title>

See: http://google.github.io/styleguide/htmlcssguide.xml?showone=HTML_Validity#HTML_Validity

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that, but google html style guide says don't use <!DOCTYPE html> - note - I used it last night.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got this directly from the same style guide. <!DOCTYPE html> is required. https://google.github.io/styleguide/htmlcssguide.xml#Document_Type

// 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 />");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use HTML5, not XHTML, no self-closing tags, please.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calendar cal = Calendar.getInstance();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Google strongly discourages the the use of Calendar. The Joda-Time library is preferred. Unfortunately that Java Practices guide is not publicly linkable. I'll send you a link over email.

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()));
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit, optional] missing space before for. Is this comment necessary?


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));
Copy link
Contributor

Choose a reason for hiding this comment

The 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 String.format so that whole function call appears on the next line.

} // end doGet
} //end class
// [END logs_API_example]

9 changes: 9 additions & 0 deletions appengine/logs/src/main/webapp/WEB-INF/appengine-web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
Copy link
Contributor

Choose a reason for hiding this comment

The 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>
14 changes: 14 additions & 0 deletions appengine/logs/src/main/webapp/WEB-INF/logging.properties
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

14 changes: 14 additions & 0 deletions appengine/logs/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
Copy link
Contributor

Choose a reason for hiding this comment

The 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>
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<module>appengine/analytics</module>
<module>appengine/appidentity</module>
<module>appengine/helloworld</module>
<module>appengine/logs</module>
<module>appengine/mailgun</module>
<module>appengine/mailjet</module>
<module>appengine/memcache</module>
Expand Down