From 9e6f44b1fb2dcc0c6a332a8b2db5bf47ebbf1fbc Mon Sep 17 00:00:00 2001 From: Guillaume Laforge Date: Mon, 7 Nov 2016 20:29:21 +0100 Subject: [PATCH 1/5] Use getters for DatastoreOptions and the Datastore service (#401) `defaultInstance()` and `service()` seem to be deprecated in favor of `getDefaultInstance()` and `getService()` --- .../src/main/java/com/example/datastore/QuickstartSample.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/datastore/cloud-client/src/main/java/com/example/datastore/QuickstartSample.java b/datastore/cloud-client/src/main/java/com/example/datastore/QuickstartSample.java index 1864a1e8f10..f163222afaf 100644 --- a/datastore/cloud-client/src/main/java/com/example/datastore/QuickstartSample.java +++ b/datastore/cloud-client/src/main/java/com/example/datastore/QuickstartSample.java @@ -26,7 +26,7 @@ public class QuickstartSample { public static void main(String... args) throws Exception { // Instantiates a client - Datastore datastore = DatastoreOptions.defaultInstance().service(); + Datastore datastore = DatastoreOptions.getDefaultInstance().getService(); // The kind for the new entity String kind = "Task"; From 47fb0c5dd8c9c829210d8d5d09cea40615acc0bd Mon Sep 17 00:00:00 2001 From: Guillaume Laforge Date: Mon, 7 Nov 2016 20:29:21 +0100 Subject: [PATCH 2/5] Use getters for DatastoreOptions and the Datastore service (#401) `defaultInstance()` and `service()` seem to be deprecated in favor of `getDefaultInstance()` and `getService()` --- .../src/main/java/com/example/datastore/QuickstartSample.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/datastore/cloud-client/src/main/java/com/example/datastore/QuickstartSample.java b/datastore/cloud-client/src/main/java/com/example/datastore/QuickstartSample.java index 1864a1e8f10..f163222afaf 100644 --- a/datastore/cloud-client/src/main/java/com/example/datastore/QuickstartSample.java +++ b/datastore/cloud-client/src/main/java/com/example/datastore/QuickstartSample.java @@ -26,7 +26,7 @@ public class QuickstartSample { public static void main(String... args) throws Exception { // Instantiates a client - Datastore datastore = DatastoreOptions.defaultInstance().service(); + Datastore datastore = DatastoreOptions.getDefaultInstance().getService(); // The kind for the new entity String kind = "Task"; From 993d022cea8603950a0345557ba45c6d5c8f79db Mon Sep 17 00:00:00 2001 From: Jason Dobry Date: Tue, 15 Nov 2016 19:46:46 -0800 Subject: [PATCH 3/5] Add logging sample. --- logging/cloud-client/README.md | 29 ++++++++++ logging/cloud-client/pom.xml | 57 +++++++++++++++++++ .../com/example/logging/QuickstartSample.java | 51 +++++++++++++++++ 3 files changed, 137 insertions(+) create mode 100644 logging/cloud-client/README.md create mode 100644 logging/cloud-client/pom.xml create mode 100644 logging/cloud-client/src/main/java/com/example/logging/QuickstartSample.java diff --git a/logging/cloud-client/README.md b/logging/cloud-client/README.md new file mode 100644 index 00000000000..f98475a9aff --- /dev/null +++ b/logging/cloud-client/README.md @@ -0,0 +1,29 @@ +# Getting Started with Stackdriver Logging and the Google Cloud Client libraries + +[Stackdriver Logging][logging] allows you to store, search, analyze, monitor, +and alert on log data and events from Google Cloud Platform and Amazon Web +Services. +These sample Java applications demonstrate how to access the Cloud Storage API using +the [Google Cloud Client Library for Java][google-cloud-java]. + +[logging]: https://cloud.google.com/logging/ +[google-cloud-java]: https://github.com/GoogleCloudPlatform/google-cloud-java + +## Quickstart + +Install [Maven](http://maven.apache.org/). + +Build your project with: + + mvn clean package -DskipTests + +You can then run a given `ClassName` via: + + mvn exec:java -Dexec.mainClass=com.example.logging.ClassName \ + -DpropertyName=propertyValue \ + -Dexec.args="any arguments to the app" + +### Writing a log entry (using the quickstart sample) + + mvn exec:java -Dexec.mainClass=com.example.logging.QuickstartSample \ + -Dexec.args="my-log" diff --git a/logging/cloud-client/pom.xml b/logging/cloud-client/pom.xml new file mode 100644 index 00000000000..2c38613ed7b --- /dev/null +++ b/logging/cloud-client/pom.xml @@ -0,0 +1,57 @@ + + + 4.0.0 + come.example.logging + logging-google-cloud-samples + jar + + + + doc-samples + com.google.cloud + 1.0.0 + ../.. + + + + 1.8 + 1.8 + UTF-8 + + + + + com.google.cloud + google-cloud-logging + 0.6.0 + + + + + junit + junit + 4.12 + test + + + com.google.truth + truth + 0.30 + test + + + diff --git a/logging/cloud-client/src/main/java/com/example/logging/QuickstartSample.java b/logging/cloud-client/src/main/java/com/example/logging/QuickstartSample.java new file mode 100644 index 00000000000..a2fa91e5074 --- /dev/null +++ b/logging/cloud-client/src/main/java/com/example/logging/QuickstartSample.java @@ -0,0 +1,51 @@ +/* + Copyright 2016, Google, Inc. + + 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. +*/ + +package com.example.logging; + +// [START logging_quickstart] +// Imports the Google Cloud client library +import com.google.cloud.MonitoredResource; +import com.google.cloud.logging.LogEntry; +import com.google.cloud.logging.Logging; +import com.google.cloud.logging.LoggingOptions; +import com.google.cloud.logging.Payload.StringPayload; + +import java.util.Collections; + +public class QuickstartSample { + public static void main(String... args) throws Exception { + // Instantiates a client + Logging logging = LoggingOptions.defaultInstance().service(); + + // The name of the log to write to + String logName = args[0]; // "my-log"; + + // The data to write to the log + String text = "Hello, world!"; + + LogEntry entry = LogEntry.newBuilder(StringPayload.of(text)) + .setLogName(logName) + .setResource(MonitoredResource.builder("global").build()) + .build(); + + // Writes the log entry + logging.write(Collections.singleton(entry)); + + System.out.printf("Logged: %s%n", text); + } +} +// [END logging_quickstart] From 7cb86d1d4f6760420bed8cef9c9d4561d463f984 Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Wed, 16 Nov 2016 14:27:15 -0800 Subject: [PATCH 4/5] Adding Logging Quickstart Test and updated deprecated function --- .../com/example/logging/QuickstartSample.java | 16 +++-- .../example/logging/QuickstartSampleIT.java | 69 +++++++++++++++++++ 2 files changed, 78 insertions(+), 7 deletions(-) create mode 100644 logging/cloud-client/src/test/java/com/example/logging/QuickstartSampleIT.java diff --git a/logging/cloud-client/src/main/java/com/example/logging/QuickstartSample.java b/logging/cloud-client/src/main/java/com/example/logging/QuickstartSample.java index a2fa91e5074..58d5e92a97b 100644 --- a/logging/cloud-client/src/main/java/com/example/logging/QuickstartSample.java +++ b/logging/cloud-client/src/main/java/com/example/logging/QuickstartSample.java @@ -18,6 +18,7 @@ // [START logging_quickstart] // Imports the Google Cloud client library + import com.google.cloud.MonitoredResource; import com.google.cloud.logging.LogEntry; import com.google.cloud.logging.Logging; @@ -27,9 +28,10 @@ import java.util.Collections; public class QuickstartSample { + public static void main(String... args) throws Exception { // Instantiates a client - Logging logging = LoggingOptions.defaultInstance().service(); + Logging logging = LoggingOptions.getDefaultInstance().getService(); // The name of the log to write to String logName = args[0]; // "my-log"; @@ -38,14 +40,14 @@ public static void main(String... args) throws Exception { String text = "Hello, world!"; LogEntry entry = LogEntry.newBuilder(StringPayload.of(text)) - .setLogName(logName) - .setResource(MonitoredResource.builder("global").build()) - .build(); + .setLogName(logName) + .setResource(MonitoredResource.newBuilder("global").build()) + .build(); - // Writes the log entry - logging.write(Collections.singleton(entry)); + // Writes the log entry + logging.write(Collections.singleton(entry)); - System.out.printf("Logged: %s%n", text); + System.out.printf("Logged: %s%n", text); } } // [END logging_quickstart] diff --git a/logging/cloud-client/src/test/java/com/example/logging/QuickstartSampleIT.java b/logging/cloud-client/src/test/java/com/example/logging/QuickstartSampleIT.java new file mode 100644 index 00000000000..66aa5685a65 --- /dev/null +++ b/logging/cloud-client/src/test/java/com/example/logging/QuickstartSampleIT.java @@ -0,0 +1,69 @@ +/* + Copyright 2016, Google, Inc. + + 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. +*/ + +package com.example.logging; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.cloud.logging.Logging; +import com.google.cloud.logging.LoggingOptions; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +/** + * Tests for quickstart sample. + */ +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class QuickstartSampleIT { + + private ByteArrayOutputStream bout; + private PrintStream out; + + private static final void deleteMyLog() { + Logging logging = LoggingOptions.getDefaultInstance().getService(); + + logging.deleteLog("my-log"); + } + + @Before + public void setUp() { + deleteMyLog(); + + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + deleteMyLog(); + } + + @Test + public void testQuickstart() throws Exception { + QuickstartSample.main("my-log"); + String got = bout.toString(); + assertThat(got).contains("Logged: Hello, world!"); + } +} From 62be66b6dd8b54aa1c77f37813743b0b04359247 Mon Sep 17 00:00:00 2001 From: Frank Natividad Date: Wed, 16 Nov 2016 14:37:26 -0800 Subject: [PATCH 5/5] Adding Logging Quickstart to **root** pom.xml --- pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/pom.xml b/pom.xml index 1c00a0f34b6..9ea5af3b395 100644 --- a/pom.xml +++ b/pom.xml @@ -105,6 +105,7 @@ flexible/twilio language/analysis logging + logging/cloud-client monitoring/v2 monitoring/v3 pubsub/cloud-client