Skip to content

Commit 520f5a2

Browse files
authored
Merge pull request #416 from GoogleCloudPlatform/quickstarts
Add Logging quickstart sample
2 parents a577680 + 7472602 commit 520f5a2

File tree

6 files changed

+210
-1
lines changed

6 files changed

+210
-1
lines changed

datastore/cloud-client/src/main/java/com/example/datastore/QuickstartSample.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
public class QuickstartSample {
2727
public static void main(String... args) throws Exception {
2828
// Instantiates a client
29-
Datastore datastore = DatastoreOptions.defaultInstance().service();
29+
Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
3030

3131
// The kind for the new entity
3232
String kind = "Task";

logging/cloud-client/README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Getting Started with Stackdriver Logging and the Google Cloud Client libraries
2+
3+
[Stackdriver Logging][logging] allows you to store, search, analyze, monitor,
4+
and alert on log data and events from Google Cloud Platform and Amazon Web
5+
Services.
6+
These sample Java applications demonstrate how to access the Cloud Storage API using
7+
the [Google Cloud Client Library for Java][google-cloud-java].
8+
9+
[logging]: https://cloud.google.com/logging/
10+
[google-cloud-java]: https://github.com/GoogleCloudPlatform/google-cloud-java
11+
12+
## Quickstart
13+
14+
Install [Maven](http://maven.apache.org/).
15+
16+
Build your project with:
17+
18+
mvn clean package -DskipTests
19+
20+
You can then run a given `ClassName` via:
21+
22+
mvn exec:java -Dexec.mainClass=com.example.logging.ClassName \
23+
-DpropertyName=propertyValue \
24+
-Dexec.args="any arguments to the app"
25+
26+
### Writing a log entry (using the quickstart sample)
27+
28+
mvn exec:java -Dexec.mainClass=com.example.logging.QuickstartSample \
29+
-Dexec.args="my-log"

logging/cloud-client/pom.xml

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<!--
2+
Copyright 2016 Google Inc. All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
<project>
17+
<modelVersion>4.0.0</modelVersion>
18+
<groupId>come.example.logging</groupId>
19+
<artifactId>logging-google-cloud-samples</artifactId>
20+
<packaging>jar</packaging>
21+
22+
<!-- Parent defines config for testing & linting. -->
23+
<parent>
24+
<artifactId>doc-samples</artifactId>
25+
<groupId>com.google.cloud</groupId>
26+
<version>1.0.0</version>
27+
<relativePath>../..</relativePath>
28+
</parent>
29+
30+
<properties>
31+
<maven.compiler.target>1.8</maven.compiler.target>
32+
<maven.compiler.source>1.8</maven.compiler.source>
33+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
34+
</properties>
35+
36+
<dependencies>
37+
<dependency>
38+
<groupId>com.google.cloud</groupId>
39+
<artifactId>google-cloud-logging</artifactId>
40+
<version>0.6.0</version>
41+
</dependency>
42+
43+
<!-- Test dependencies -->
44+
<dependency>
45+
<groupId>junit</groupId>
46+
<artifactId>junit</artifactId>
47+
<version>4.12</version>
48+
<scope>test</scope>
49+
</dependency>
50+
<dependency>
51+
<groupId>com.google.truth</groupId>
52+
<artifactId>truth</artifactId>
53+
<version>0.30</version>
54+
<scope>test</scope>
55+
</dependency>
56+
</dependencies>
57+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
Copyright 2016, Google, Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package com.example.logging;
18+
19+
// [START logging_quickstart]
20+
// Imports the Google Cloud client library
21+
22+
import com.google.cloud.MonitoredResource;
23+
import com.google.cloud.logging.LogEntry;
24+
import com.google.cloud.logging.Logging;
25+
import com.google.cloud.logging.LoggingOptions;
26+
import com.google.cloud.logging.Payload.StringPayload;
27+
28+
import java.util.Collections;
29+
30+
public class QuickstartSample {
31+
32+
public static void main(String... args) throws Exception {
33+
// Instantiates a client
34+
Logging logging = LoggingOptions.getDefaultInstance().getService();
35+
36+
// The name of the log to write to
37+
String logName = args[0]; // "my-log";
38+
39+
// The data to write to the log
40+
String text = "Hello, world!";
41+
42+
LogEntry entry = LogEntry.newBuilder(StringPayload.of(text))
43+
.setLogName(logName)
44+
.setResource(MonitoredResource.newBuilder("global").build())
45+
.build();
46+
47+
// Writes the log entry
48+
logging.write(Collections.singleton(entry));
49+
50+
System.out.printf("Logged: %s%n", text);
51+
}
52+
}
53+
// [END logging_quickstart]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
Copyright 2016, Google, Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package com.example.logging;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import com.google.cloud.logging.Logging;
22+
import com.google.cloud.logging.LoggingOptions;
23+
import org.junit.After;
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
import org.junit.runner.RunWith;
27+
import org.junit.runners.JUnit4;
28+
29+
import java.io.ByteArrayOutputStream;
30+
import java.io.PrintStream;
31+
32+
/**
33+
* Tests for quickstart sample.
34+
*/
35+
@RunWith(JUnit4.class)
36+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
37+
public class QuickstartSampleIT {
38+
39+
private ByteArrayOutputStream bout;
40+
private PrintStream out;
41+
42+
private static final void deleteMyLog() {
43+
Logging logging = LoggingOptions.getDefaultInstance().getService();
44+
45+
logging.deleteLog("my-log");
46+
}
47+
48+
@Before
49+
public void setUp() {
50+
deleteMyLog();
51+
52+
bout = new ByteArrayOutputStream();
53+
out = new PrintStream(bout);
54+
System.setOut(out);
55+
}
56+
57+
@After
58+
public void tearDown() {
59+
System.setOut(null);
60+
deleteMyLog();
61+
}
62+
63+
@Test
64+
public void testQuickstart() throws Exception {
65+
QuickstartSample.main("my-log");
66+
String got = bout.toString();
67+
assertThat(got).contains("Logged: Hello, world!");
68+
}
69+
}

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
<module>flexible/twilio</module>
106106
<module>language/analysis</module>
107107
<module>logging</module>
108+
<module>logging/cloud-client</module>
108109
<module>monitoring/v2</module>
109110
<module>monitoring/v3</module>
110111
<module>pubsub/cloud-client</module>

0 commit comments

Comments
 (0)