From 9b0a711117be8cd2743a1e209d18a11e993a3f3d Mon Sep 17 00:00:00 2001 From: Jisha Abubaker Date: Thu, 24 Aug 2017 12:54:37 -0700 Subject: [PATCH 1/6] wip samples --- logging/cloud-client/pom.xml | 4 +- .../com/example/logging/QuickstartSample.java | 5 +- logging/pom.xml | 31 +---- logging/src/main/java/ListLogs.java | 117 ------------------ logging/src/test/java/ListLogsTest.java | 62 ---------- pom.xml | 22 ++++ 6 files changed, 33 insertions(+), 208 deletions(-) delete mode 100644 logging/src/main/java/ListLogs.java delete mode 100644 logging/src/test/java/ListLogsTest.java diff --git a/logging/cloud-client/pom.xml b/logging/cloud-client/pom.xml index 853cebb1e1b..1a8b73024cf 100644 --- a/logging/cloud-client/pom.xml +++ b/logging/cloud-client/pom.xml @@ -16,7 +16,7 @@ 4.0.0 com.example.logging - logging-google-cloud-samples + logging-google-cloud-samples-api jar @@ -37,7 +37,7 @@ com.google.cloud google-cloud-logging - 1.3.1 + 1.4.0 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 58d5e92a97b..93183fa61fe 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 @@ -24,12 +24,13 @@ 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.getDefaultInstance().getService(); @@ -44,7 +45,7 @@ public static void main(String... args) throws Exception { .setResource(MonitoredResource.newBuilder("global").build()) .build(); - // Writes the log entry + // Writes the log entry asynchronously logging.write(Collections.singleton(entry)); System.out.printf("Logged: %s%n", text); diff --git a/logging/pom.xml b/logging/pom.xml index b2cc05516cf..9e6e45bce91 100644 --- a/logging/pom.xml +++ b/logging/pom.xml @@ -32,35 +32,16 @@ limitations under the License. 1.8 + + logback + jul + cloud-client + + - - com.google.apis - google-api-services-logging - v1beta3-rev10-1.21.0 - - - com.google.oauth-client - google-oauth-client - ${project.oauth.version} - - - com.google.http-client - google-http-client-jackson2 - ${project.http.version} - - - com.google.oauth-client - google-oauth-client-jetty - ${project.oauth.version} - junit junit - - com.jcabi - jcabi-matchers - - diff --git a/logging/src/main/java/ListLogs.java b/logging/src/main/java/ListLogs.java deleted file mode 100644 index db4252bf4e6..00000000000 --- a/logging/src/main/java/ListLogs.java +++ /dev/null @@ -1,117 +0,0 @@ -/** - * Copyright (c) 2015 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. - */ -// [START imports] -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.http.HttpTransport; -import com.google.api.client.http.javanet.NetHttpTransport; -import com.google.api.client.json.JsonFactory; -import com.google.api.client.json.jackson2.JacksonFactory; -import com.google.api.client.util.Strings; -import com.google.api.services.logging.Logging; -import com.google.api.services.logging.LoggingScopes; -import com.google.api.services.logging.model.ListLogsResponse; -import com.google.api.services.logging.model.Log; - -import java.io.IOException; -import java.net.URLDecoder; -import java.util.Collections; -import java.util.List; -// [END imports] - -/** - * Cloud Logging Java API sample that lists the logs available to a project. - * Uses the v1beta3 Cloud Logging API, version 1.20.0 or later. - * See https://cloud.google.com/logging/docs/api/libraries/. - */ -public class ListLogs { - - private static final List LOGGING_SCOPES = Collections.singletonList( - LoggingScopes.LOGGING_READ); - - private static final String APPLICATION_NAME = "ListLogs sample"; - - /** - * Returns an authorized Cloud Logging API service client that is usable - * on Google App Engine, Google Compute Engine, workstations with the Google Cloud SDK, - * and other computers if you install service account private credentials. - * See https://cloud.google.com/logging/docs/api/tasks. - */ - // [START auth] - public static Logging getLoggingService() throws IOException { - HttpTransport transport = new NetHttpTransport(); - JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); - GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory); - if (credential.createScopedRequired()) { - credential = credential.createScoped(LOGGING_SCOPES); - } - Logging service = new Logging.Builder(transport, jsonFactory, credential) - .setApplicationName(APPLICATION_NAME).build(); - return service; - } - // [END auth] - - /** - * Lists the names of the logs visible to a project, which may require fetching multiple - * pages of results from the Cloud Logging API. This method converts log resource names - * ("/projects/PROJECTID/logs/SERVICENAME%2FLOGNAME") to simple log names ("SERVICENAME/LOGNAME"). - * - * @param service The logging service client returned by getLoggingService. - * @param projectId The project whose logs are to be listed. - * @throws IOException If the Cloud Logging API fails because, for example, the project ID - * doesn't exist or authorization fails. - * See https://cloud.google.com//logging/docs/api/tasks/#java_sample_code. - */ - // [START listlogs] - private static void listLogs(Logging service, String projectId) throws IOException { - final int pageSize = 3; - final int resourcePrefixLength = ("/projects/" + projectId + "/logs/").length(); - String nextPageToken = ""; - - do { - ListLogsResponse response = service.projects().logs().list(projectId) - .setPageToken(nextPageToken).setPageSize(pageSize).execute(); - if (response.isEmpty()) { - break; - } - for (Log log: response.getLogs()) { - System.out.println(URLDecoder.decode( - log.getName().substring(resourcePrefixLength), "utf-8")); - } - nextPageToken = response.getNextPageToken(); - } while (!Strings.isNullOrEmpty(nextPageToken)); - System.out.println("Done."); - } - // [END listlogs] - - /** - * Demonstrates the Cloud Logging API by listing the logs in a project. - * @param args The project ID. - * @throws IOException if a Cloud Logging API call fails because, say, the project ID is wrong - * or authorization fails. - */ - public static void main(String[] args) throws IOException { - if (args.length != 1) { - System.err.println(String.format("Usage: %s ", - ListLogs.class.getSimpleName())); - return; - } - - String projectId = args[0]; - Logging service = getLoggingService(); - listLogs(service, projectId); - } -} -// [END all] diff --git a/logging/src/test/java/ListLogsTest.java b/logging/src/test/java/ListLogsTest.java deleted file mode 100644 index 45efec69444..00000000000 --- a/logging/src/test/java/ListLogsTest.java +++ /dev/null @@ -1,62 +0,0 @@ -/** - * Copyright 2015 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. - */ -import static com.jcabi.matchers.RegexMatchers.containsPattern; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThat; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; - -/** - * Tests the Cloud Logging sample. - */ -public class ListLogsTest { - private final ByteArrayOutputStream stdout = new ByteArrayOutputStream(); - private final ByteArrayOutputStream stderr = new ByteArrayOutputStream(); - private static final PrintStream REAL_OUT = System.out; - private static final PrintStream REAL_ERR = System.err; - private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); - - @Before - public void setUp() { - System.setOut(new PrintStream(stdout)); - System.setErr(new PrintStream(stderr)); - } - - @After - public void tearDown() { - System.setOut(ListLogsTest.REAL_OUT); - System.setErr(ListLogsTest.REAL_ERR); - } - - @Test - public void testUsage() throws Exception { - ListLogs.main(new String[] {}); - assertEquals("Usage: ListLogs \n", stderr.toString()); - } - - @Test - public void testListLogs() throws Exception { - ListLogs.main(new String[] {PROJECT_ID}); - String out = stdout.toString(); - // Don't know what logs the test project will have. - assertThat(out, containsPattern("Done\\.")); - } -} diff --git a/pom.xml b/pom.xml index 8cec39abbd4..a949f5469d3 100644 --- a/pom.xml +++ b/pom.xml @@ -16,6 +16,28 @@ 4.0.0 1.0.0 + + + com.google.cloud + google-cloud-logging + 1.3.2-SNAPSHOT + + + com.google.cloud + google-cloud-logging + 1.3.2-SNAPSHOT + + + com.google.cloud + google-cloud-logging + 1.3.2-SNAPSHOT + + + com.google.cloud + google-cloud-logging + 1.3.2-SNAPSHOT + + com.google.cloud doc-samples From 97c250d881792c4e19b7413d54f4ddfd23bde221 Mon Sep 17 00:00:00 2001 From: Jisha Abubaker Date: Thu, 24 Aug 2017 13:35:10 -0700 Subject: [PATCH 2/6] wip --- logging/jul/pom.xml | 57 +++++++++++++++++++ .../com/example/logging/jul/Quickstart.java | 31 ++++++++++ .../jul/enhancers/ExampleEnhancer.java | 31 ++++++++++ .../jul/src/main/resources/logging.properties | 29 ++++++++++ logging/logback/pom.xml | 57 +++++++++++++++++++ .../example/logging/logback/Quickstart.java | 31 ++++++++++ .../logback/enhancers/ExampleEnhancer.java | 32 +++++++++++ .../logback/src/main/resources/logback.xml | 32 +++++++++++ 8 files changed, 300 insertions(+) create mode 100644 logging/jul/pom.xml create mode 100644 logging/jul/src/main/java/com/example/logging/jul/Quickstart.java create mode 100644 logging/jul/src/main/java/com/example/logging/jul/enhancers/ExampleEnhancer.java create mode 100644 logging/jul/src/main/resources/logging.properties create mode 100644 logging/logback/pom.xml create mode 100644 logging/logback/src/main/java/com/example/logging/logback/Quickstart.java create mode 100644 logging/logback/src/main/java/com/example/logging/logback/enhancers/ExampleEnhancer.java create mode 100644 logging/logback/src/main/resources/logback.xml diff --git a/logging/jul/pom.xml b/logging/jul/pom.xml new file mode 100644 index 00000000000..add6406710b --- /dev/null +++ b/logging/jul/pom.xml @@ -0,0 +1,57 @@ + + + 4.0.0 + com.example.logging + logging-google-cloud-samples-jul + jar + + + + doc-samples + com.google.cloud + 1.0.0 + ../.. + + + + 1.8 + 1.8 + UTF-8 + + + + + com.google.cloud + google-cloud-logging + 1.4.0 + + + + + junit + junit + 4.12 + test + + + com.google.truth + truth + 0.34 + test + + + diff --git a/logging/jul/src/main/java/com/example/logging/jul/Quickstart.java b/logging/jul/src/main/java/com/example/logging/jul/Quickstart.java new file mode 100644 index 00000000000..7e16e5c83e0 --- /dev/null +++ b/logging/jul/src/main/java/com/example/logging/jul/Quickstart.java @@ -0,0 +1,31 @@ +/* + Copyright 2017, 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.jul; + +// [START jul_quickstart] + +import java.util.logging.Logger; + +public class Quickstart { + private static final Logger logger = Logger.getLogger(Quickstart.class.getName()); + + public static void main(String[] args) { + logger.info("Logging INFO with java.util.logging"); + logger.severe("Logging ERROR with java.util.logging"); + } +} +// [END jul_quickstart] \ No newline at end of file diff --git a/logging/jul/src/main/java/com/example/logging/jul/enhancers/ExampleEnhancer.java b/logging/jul/src/main/java/com/example/logging/jul/enhancers/ExampleEnhancer.java new file mode 100644 index 00000000000..728ddf09297 --- /dev/null +++ b/logging/jul/src/main/java/com/example/logging/jul/enhancers/ExampleEnhancer.java @@ -0,0 +1,31 @@ + +/* + * Copyright 2017 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.jul.enhancers; + +import com.google.cloud.logging.LogEntry; +import com.google.cloud.logging.LoggingEnhancer; + +// Add / update additional fields to the log entry +public class ExampleEnhancer implements LoggingEnhancer { + + @Override + public void enhanceLogEntry(LogEntry.Builder logEntry) { + // add additional labels + logEntry.addLabel("test-label-1", "test-value-1"); + } +} diff --git a/logging/jul/src/main/resources/logging.properties b/logging/jul/src/main/resources/logging.properties new file mode 100644 index 00000000000..04d1832bf21 --- /dev/null +++ b/logging/jul/src/main/resources/logging.properties @@ -0,0 +1,29 @@ +# A default java.util.logging configuration. +# +# [START jul_config] +# To use this configuration, add to system properties : -Djava.util.logging.config.file="/path/to/file" +# +.level = INFO + +# it is recommended that io.grpc and sun.net logging level is kept at INFO level, +# as both these packages are used by Stackdriver internals and can result in verbose / initialization problems. +io.grpc.netty.level=INFO +sun.net.level=INFO + +com.example.logging.jul.Quickstart.handlers=com.google.cloud.logging.LoggingHandler +# default : java.log +com.google.cloud.logging.LoggingHandler.log=custom_log + +# default : INFO +com.google.cloud.logging.LoggingHandler.level=FINE + +# default : ERROR +com.google.cloud.logging.LoggingHandler.flushLevel=ERROR + +# custom formatter +com.google.cloud.logging.LoggingHandler.formatter=java.util.logging.SimpleFormatter +java.util.logging.SimpleFormatter.format=%3$s: %5$s%6$s + +#optional enhancers (to add additional fields, labels) +com.google.cloud.logging.LoggingHandler.enhancers=com.example.logging.jul.enhancers.ExampleEnhancer +# [END jul_config] \ No newline at end of file diff --git a/logging/logback/pom.xml b/logging/logback/pom.xml new file mode 100644 index 00000000000..fc19f3041fe --- /dev/null +++ b/logging/logback/pom.xml @@ -0,0 +1,57 @@ + + + 4.0.0 + com.example.logging + logging-google-cloud-samples-logback + jar + + + + doc-samples + com.google.cloud + 1.0.0 + ../.. + + + + 1.8 + 1.8 + UTF-8 + + + + + com.google.cloud + google-cloud-logging-logback + 0.22.0-alpha + + + + + junit + junit + 4.12 + test + + + com.google.truth + truth + 0.34 + test + + + diff --git a/logging/logback/src/main/java/com/example/logging/logback/Quickstart.java b/logging/logback/src/main/java/com/example/logging/logback/Quickstart.java new file mode 100644 index 00000000000..f085b9a569d --- /dev/null +++ b/logging/logback/src/main/java/com/example/logging/logback/Quickstart.java @@ -0,0 +1,31 @@ +/* + Copyright 2017, 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.logback; + +// [START logback_quickstart] +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class Quickstart { + private static final Logger logger = LoggerFactory.getLogger(Quickstart.class); + + public static void main(String[] args) { + logger.info("Logging INFO with Logback"); + logger.error("Logging ERROR with Logback"); + } +} +// [END logback_quickstart] \ No newline at end of file diff --git a/logging/logback/src/main/java/com/example/logging/logback/enhancers/ExampleEnhancer.java b/logging/logback/src/main/java/com/example/logging/logback/enhancers/ExampleEnhancer.java new file mode 100644 index 00000000000..1c9bde9ffbc --- /dev/null +++ b/logging/logback/src/main/java/com/example/logging/logback/enhancers/ExampleEnhancer.java @@ -0,0 +1,32 @@ + +/* + * Copyright 2017 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.logback.enhancers; +// [START logging_enhancer] +import com.google.cloud.logging.LogEntry; +import com.google.cloud.logging.LoggingEnhancer; + +// Add / update additional fields to the log entry +public class ExampleEnhancer implements LoggingEnhancer { + + @Override + public void enhanceLogEntry(LogEntry.Builder logEntry) { + // add additional labels + logEntry.addLabel("test-label-1", "test-value-1"); + } +} +// [END logging_enhancer] \ No newline at end of file diff --git a/logging/logback/src/main/resources/logback.xml b/logging/logback/src/main/resources/logback.xml new file mode 100644 index 00000000000..3cd36e776ca --- /dev/null +++ b/logging/logback/src/main/resources/logback.xml @@ -0,0 +1,32 @@ + + + + + + + INFO + + application.log + com.example.logging.logback.enhancers.ExampleEnhancer + WARN + + + + + + + From 31396c4de14afff079e970cd1be87febdce56327 Mon Sep 17 00:00:00 2001 From: Jisha Abubaker Date: Thu, 24 Aug 2017 16:15:52 -0700 Subject: [PATCH 3/6] adding README, tests, formatting fixes --- logging/cloud-client/README.md | 17 ++++--- logging/cloud-client/pom.xml | 6 +-- .../com/example/logging/QuickstartSample.java | 13 +++-- ...QuickstartSampleIT.java => LoggingIT.java} | 47 ++++++++++++++----- logging/jul/pom.xml | 6 +-- logging/logback/pom.xml | 6 +-- logging/pom.xml | 2 +- pom.xml | 23 --------- 8 files changed, 65 insertions(+), 55 deletions(-) rename logging/cloud-client/src/test/java/com/example/logging/{QuickstartSampleIT.java => LoggingIT.java} (56%) diff --git a/logging/cloud-client/README.md b/logging/cloud-client/README.md index f98475a9aff..0fab8f40ece 100644 --- a/logging/cloud-client/README.md +++ b/logging/cloud-client/README.md @@ -3,7 +3,7 @@ [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 +These sample Java applications demonstrate how to access the Stackdriver Logging API using the [Google Cloud Client Library for Java][google-cloud-java]. [logging]: https://cloud.google.com/logging/ @@ -17,13 +17,16 @@ 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" + + +### List log entries + + mvn exec:java -Dexec.mainClass=com.example.logging.ListLogs \ + -Dexec.args="my-log" + + +Logs can also viewed using the [Logs Viewer Console](https://pantheon.corp.google.com/logs/viewer). diff --git a/logging/cloud-client/pom.xml b/logging/cloud-client/pom.xml index 1a8b73024cf..c99554d74a6 100644 --- a/logging/cloud-client/pom.xml +++ b/logging/cloud-client/pom.xml @@ -21,10 +21,10 @@ - doc-samples - com.google.cloud + com.google.cloud.logging.samples + cloud-logging-samples 1.0.0 - ../.. + .. 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 93183fa61fe..3022f1bce6d 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 @@ -17,20 +17,26 @@ 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 com.google.cloud.logging.Severity; import java.util.Collections; +/** + * This sample demonstrates writing logs using the Stackdriver Logging API. + * The library also offers a java.util.logging Handler `com.google.cloud.logging.LoggingHandler` + * Logback integration is also available : + * https://github.com/GoogleCloudPlatform/google-cloud-java/tree/master/google-cloud-contrib/google-cloud-logging-logback + * Using the java.util.logging handler / Logback appender should be preferred to programmatically using the API. + */ public class QuickstartSample { + /** Expects a new or existing Stackdriver log name as the first argument.*/ public static void main(String... args) throws Exception { - // Instantiates a client Logging logging = LoggingOptions.getDefaultInstance().getService(); @@ -41,6 +47,7 @@ public static void main(String... args) throws Exception { String text = "Hello, world!"; LogEntry entry = LogEntry.newBuilder(StringPayload.of(text)) + .setSeverity(Severity.ERROR) .setLogName(logName) .setResource(MonitoredResource.newBuilder("global").build()) .build(); diff --git a/logging/cloud-client/src/test/java/com/example/logging/QuickstartSampleIT.java b/logging/cloud-client/src/test/java/com/example/logging/LoggingIT.java similarity index 56% rename from logging/cloud-client/src/test/java/com/example/logging/QuickstartSampleIT.java rename to logging/cloud-client/src/test/java/com/example/logging/LoggingIT.java index 66aa5685a65..1915d60667f 100644 --- a/logging/cloud-client/src/test/java/com/example/logging/QuickstartSampleIT.java +++ b/logging/cloud-client/src/test/java/com/example/logging/LoggingIT.java @@ -18,37 +18,37 @@ import static com.google.common.truth.Truth.assertThat; +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.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.Collections; 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 { +public class LoggingIT { private ByteArrayOutputStream bout; private PrintStream out; + private Logging logging = LoggingOptions.getDefaultInstance().getService(); - private static final void deleteMyLog() { - Logging logging = LoggingOptions.getDefaultInstance().getService(); - - logging.deleteLog("my-log"); + private void deleteLog(String logName) { + logging.deleteLog(logName); } @Before public void setUp() { - deleteMyLog(); - bout = new ByteArrayOutputStream(); out = new PrintStream(bout); System.setOut(out); @@ -57,13 +57,36 @@ public void setUp() { @After public void tearDown() { System.setOut(null); - deleteMyLog(); } @Test public void testQuickstart() throws Exception { - QuickstartSample.main("my-log"); + String logName = "my-log"; + deleteLog(logName); + QuickstartSample.main(logName); String got = bout.toString(); assertThat(got).contains("Logged: Hello, world!"); + deleteLog(logName); + } + + @Test(timeout = 10000) + public void testWriteAndListLogs() throws Exception { + String logName = "test-log"; + deleteLog(logName); + // write a log entry + LogEntry entry = LogEntry.newBuilder(StringPayload.of("Hello world again")) + .setLogName(logName) + .setResource(MonitoredResource.newBuilder("global").build()) + .build(); + logging.write(Collections.singleton(entry)); + // flush out log immediately + logging.flush(); + bout.reset(); + while (bout.toString().isEmpty()) { + ListLogs.main(logName); + Thread.sleep(1000); + } + assertThat(bout.toString().contains("Hello world again")).isTrue(); + deleteLog(logName); } } diff --git a/logging/jul/pom.xml b/logging/jul/pom.xml index add6406710b..8ac8c19d7f7 100644 --- a/logging/jul/pom.xml +++ b/logging/jul/pom.xml @@ -21,10 +21,10 @@ - doc-samples - com.google.cloud + com.google.cloud.logging.samples + cloud-logging-samples 1.0.0 - ../.. + .. diff --git a/logging/logback/pom.xml b/logging/logback/pom.xml index fc19f3041fe..8c69a0b9368 100644 --- a/logging/logback/pom.xml +++ b/logging/logback/pom.xml @@ -21,10 +21,10 @@ - doc-samples - com.google.cloud + com.google.cloud.logging.samples + cloud-logging-samples 1.0.0 - ../.. + .. diff --git a/logging/pom.xml b/logging/pom.xml index 9e6e45bce91..1d678a90d2e 100644 --- a/logging/pom.xml +++ b/logging/pom.xml @@ -18,7 +18,7 @@ limitations under the License. 4.0.0 com.google.cloud.logging.samples cloud-logging-samples - jar + pom doc-samples diff --git a/pom.xml b/pom.xml index a949f5469d3..fe7f9e86aa0 100644 --- a/pom.xml +++ b/pom.xml @@ -16,29 +16,6 @@ 4.0.0 1.0.0 - - - com.google.cloud - google-cloud-logging - 1.3.2-SNAPSHOT - - - com.google.cloud - google-cloud-logging - 1.3.2-SNAPSHOT - - - com.google.cloud - google-cloud-logging - 1.3.2-SNAPSHOT - - - com.google.cloud - google-cloud-logging - 1.3.2-SNAPSHOT - - - com.google.cloud doc-samples pom From bc2b92d43c35b13ab4cb4d1bcd7ad5b8d5cfb726 Mon Sep 17 00:00:00 2001 From: Jisha Abubaker Date: Thu, 24 Aug 2017 16:18:48 -0700 Subject: [PATCH 4/6] updating README --- logging/cloud-client/README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/logging/cloud-client/README.md b/logging/cloud-client/README.md index 0fab8f40ece..2b0c8630204 100644 --- a/logging/cloud-client/README.md +++ b/logging/cloud-client/README.md @@ -9,13 +9,15 @@ 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 +## Setup Install [Maven](http://maven.apache.org/). Build your project with: mvn clean package -DskipTests + +[Setup authentication](https://cloud.google.com/docs/authentication) using a service account. ### Writing a log entry (using the quickstart sample) From 6803adcf3679c9d847f7ec46ae1c019ad279172f Mon Sep 17 00:00:00 2001 From: Jisha Abubaker Date: Thu, 24 Aug 2017 16:19:08 -0700 Subject: [PATCH 5/6] adding READMEs --- logging/jul/README.md | 37 +++++++++++++++++++++++++++++++++++++ logging/logback/README.md | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 logging/jul/README.md create mode 100644 logging/logback/README.md diff --git a/logging/jul/README.md b/logging/jul/README.md new file mode 100644 index 00000000000..90f91b14c2d --- /dev/null +++ b/logging/jul/README.md @@ -0,0 +1,37 @@ +# Getting Started with Stackdriver Logging using `java.util.logging` + +[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 write logs to Stackdriver using +the default Java Logging API (`java.util.logging`) handler for +[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 + +## Setup + +Install [Maven](http://maven.apache.org/). + +Build your project with: + + mvn clean package -DskipTests + +[Setup authentication](https://cloud.google.com/docs/authentication) using a service account. + +## Configuration + +Update [logging.properties](src/main/resources/logging.properties) to configure the handler. + +## Enhancers + +[ExampleEnhancer.java](src/main/java/com/example/logging/jul/enhancers/ExampleEnhancer.java) +provides an example of enhancing log entries with additional labels. + + +## Writing log entries + mvn exec:java -Dexec.mainClass=com.example.logging.jul.Quickstart \ + -Dexec.args="-Djava.util.logging.file=src/main/resources/logging.properties" + +Logs can be viewed using the [Logs Viewer Console](https://pantheon.corp.google.com/logs/viewer). diff --git a/logging/logback/README.md b/logging/logback/README.md new file mode 100644 index 00000000000..b12c0c9890b --- /dev/null +++ b/logging/logback/README.md @@ -0,0 +1,38 @@ +# Getting Started with Stackdriver Logging using Logback + +[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 write logs to Stackdriver using +[Logback](https://logback.qos.ch/) appender for [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 + +## Setup + +Install [Maven](http://maven.apache.org/). + +Build your project with: + + mvn clean package -DskipTests + +[Setup authentication](https://cloud.google.com/docs/authentication) using a service account. + +## Configuration + +Update [logback.xml](src/main/resources/logback.xml) to configure the appender. + +More information on Logback configuration can be found +[here](https://logback.qos.ch/manual/configuration.html). + +## Enhancers +[ExampleEnhancer.java](src/main/java/com/example/logging/logback/enhancers/ExampleEnhancer.java) +provides an example of enhancing log entries with additional labels. + + +## Writing log entries + mvn exec:java -Dexec.mainClass=com.example.logging.logback.Quickstart + +Logs can be viewed using the [Logs Viewer Console](https://pantheon.corp.google.com/logs/viewer). From 5b41a82d9b019d17f81572412f8d5a22b54b5166 Mon Sep 17 00:00:00 2001 From: Jisha Abubaker Date: Fri, 25 Aug 2017 09:56:25 -0700 Subject: [PATCH 6/6] fixing license issues --- .../main/java/com/example/logging/QuickstartSample.java | 7 +++---- .../src/test/java/com/example/logging/LoggingIT.java | 2 +- .../src/main/java/com/example/logging/jul/Quickstart.java | 2 +- .../com/example/logging/jul/enhancers/ExampleEnhancer.java | 1 - .../main/java/com/example/logging/logback/Quickstart.java | 2 +- .../example/logging/logback/enhancers/ExampleEnhancer.java | 1 - 6 files changed, 6 insertions(+), 9 deletions(-) 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 3022f1bce6d..444642e0f9e 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 @@ -1,5 +1,5 @@ /* - Copyright 2016, Google, Inc. + 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. @@ -28,9 +28,8 @@ /** * This sample demonstrates writing logs using the Stackdriver Logging API. * The library also offers a java.util.logging Handler `com.google.cloud.logging.LoggingHandler` - * Logback integration is also available : - * https://github.com/GoogleCloudPlatform/google-cloud-java/tree/master/google-cloud-contrib/google-cloud-logging-logback - * Using the java.util.logging handler / Logback appender should be preferred to programmatically using the API. + * Logback integration is also available : https://goo.gl/DNMoRh + * Using the java.util.logging handler / Logback appender should be preferred to using the API directly. */ public class QuickstartSample { diff --git a/logging/cloud-client/src/test/java/com/example/logging/LoggingIT.java b/logging/cloud-client/src/test/java/com/example/logging/LoggingIT.java index 1915d60667f..1abbca0906b 100644 --- a/logging/cloud-client/src/test/java/com/example/logging/LoggingIT.java +++ b/logging/cloud-client/src/test/java/com/example/logging/LoggingIT.java @@ -1,5 +1,5 @@ /* - Copyright 2016, Google, Inc. + Copyright 2017 Google Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/logging/jul/src/main/java/com/example/logging/jul/Quickstart.java b/logging/jul/src/main/java/com/example/logging/jul/Quickstart.java index 7e16e5c83e0..f75801f7f9a 100644 --- a/logging/jul/src/main/java/com/example/logging/jul/Quickstart.java +++ b/logging/jul/src/main/java/com/example/logging/jul/Quickstart.java @@ -1,5 +1,5 @@ /* - Copyright 2017, Google, Inc. + Copyright 2017 Google Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/logging/jul/src/main/java/com/example/logging/jul/enhancers/ExampleEnhancer.java b/logging/jul/src/main/java/com/example/logging/jul/enhancers/ExampleEnhancer.java index 728ddf09297..20be8259139 100644 --- a/logging/jul/src/main/java/com/example/logging/jul/enhancers/ExampleEnhancer.java +++ b/logging/jul/src/main/java/com/example/logging/jul/enhancers/ExampleEnhancer.java @@ -1,4 +1,3 @@ - /* * Copyright 2017 Google Inc. * diff --git a/logging/logback/src/main/java/com/example/logging/logback/Quickstart.java b/logging/logback/src/main/java/com/example/logging/logback/Quickstart.java index f085b9a569d..d646cadcad0 100644 --- a/logging/logback/src/main/java/com/example/logging/logback/Quickstart.java +++ b/logging/logback/src/main/java/com/example/logging/logback/Quickstart.java @@ -1,5 +1,5 @@ /* - Copyright 2017, Google, Inc. + Copyright 2017 Google Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/logging/logback/src/main/java/com/example/logging/logback/enhancers/ExampleEnhancer.java b/logging/logback/src/main/java/com/example/logging/logback/enhancers/ExampleEnhancer.java index 1c9bde9ffbc..b99c82efe1f 100644 --- a/logging/logback/src/main/java/com/example/logging/logback/enhancers/ExampleEnhancer.java +++ b/logging/logback/src/main/java/com/example/logging/logback/enhancers/ExampleEnhancer.java @@ -1,4 +1,3 @@ - /* * Copyright 2017 Google Inc. *