GeezerVison is showing github.com — Stackdriver logging samples (JUL, Logback, Client library) (#826) · mdakin/java-docs-samples@219f9ad
File treeExpand file treeCollapse file tree Open diff view settings main/java/com/example/logging test/java/com/example/logging
java/com/example/logging/jul
java/com/example/logging/logback
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: logging/cloud-client/README.md +13-8Lines changed: 13 additions & 8 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 3 | 3 | [Stackdriver Logging][logging] allows you to store, search, analyze, monitor, | 4 | 4 | and alert on log data and events from Google Cloud Platform and Amazon Web | 5 | 5 | Services. | 6 | | -These sample Java applications demonstrate how to access the Cloud Storage API using | | 6 | +These sample Java applications demonstrate how to access the Stackdriver Logging API using | 7 | 7 | the [Google Cloud Client Library for Java][google-cloud-java]. | 8 | 8 |
| 9 | 9 | [logging]: https://cloud.google.com/logging/ | 10 | 10 | [google-cloud-java]: https://github.com/GoogleCloudPlatform/google-cloud-java | 11 | 11 |
| 12 | | -## Quickstart | | 12 | +## Setup | 13 | 13 |
| 14 | 14 | Install [Maven](http://maven.apache.org/). | 15 | 15 |
| 16 | 16 | Build your project with: | 17 | 17 |
| 18 | 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" | | 19 | + | | 20 | +[Setup authentication](https://cloud.google.com/docs/authentication) using a service account. | 25 | 21 |
| 26 | 22 | ### Writing a log entry (using the quickstart sample) | 27 | 23 |
| 28 | 24 | mvn exec:java -Dexec.mainClass=com.example.logging.QuickstartSample \ | 29 | 25 | -Dexec.args="my-log" | | 26 | + | | 27 | + | | 28 | +### List log entries | | 29 | + | | 30 | + mvn exec:java -Dexec.mainClass=com.example.logging.ListLogs \ | | 31 | + -Dexec.args="my-log" | | 32 | + | | 33 | + | | 34 | +Logs can also viewed using the [Logs Viewer Console](https://pantheon.corp.google.com/logs/viewer). |
Collapse file Copy file name to clipboardExpand all lines: logging/cloud-client/pom.xml +5-5Lines changed: 5 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 16 | 16 | <project> | 17 | 17 | <modelVersion>4.0.0</modelVersion> | 18 | 18 | <groupId>com.example.logging</groupId> | 19 | | - <artifactId>logging-google-cloud-samples</artifactId> | | 19 | + <artifactId>logging-google-cloud-samples-api</artifactId> | 20 | 20 | <packaging>jar</packaging> | 21 | 21 |
| 22 | 22 | <!-- Parent defines config for testing & linting. --> | 23 | 23 | <parent> | 24 | | - <artifactId>doc-samples</artifactId> | 25 | | - <groupId>com.google.cloud</groupId> | | 24 | + <groupId>com.google.cloud.logging.samples</groupId> | | 25 | + <artifactId>cloud-logging-samples</artifactId> | 26 | 26 | <version>1.0.0</version> | 27 | | - <relativePath>../..</relativePath> | | 27 | + <relativePath>..</relativePath> | 28 | 28 | </parent> | 29 | 29 |
| 30 | 30 | <properties> | | 37 | 37 | <dependency> | 38 | 38 | <groupId>com.google.cloud</groupId> | 39 | 39 | <artifactId>google-cloud-logging</artifactId> | 40 | | - <version>1.3.1</version> | | 40 | + <version>1.4.0</version> | 41 | 41 | </dependency> | 42 | 42 |
| 43 | 43 | <!-- Test dependencies --> | |
Collapse file Copy file name to clipboardExpand all lines: logging/cloud-client/src/main/java/com/example/logging/QuickstartSample.java +12-5Lines changed: 12 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | /* | 2 | | - Copyright 2016, Google, Inc. | | 2 | + Copyright 2016 Google Inc. | 3 | 3 |
| 4 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); | 5 | 5 | you may not use this file except in compliance with the License. | | 17 | 17 | package com.example.logging; | 18 | 18 |
| 19 | 19 | // [START logging_quickstart] | 20 | | -// Imports the Google Cloud client library | 21 | | - | 22 | 20 | import com.google.cloud.MonitoredResource; | 23 | 21 | import com.google.cloud.logging.LogEntry; | 24 | 22 | import com.google.cloud.logging.Logging; | 25 | 23 | import com.google.cloud.logging.LoggingOptions; | 26 | 24 | import com.google.cloud.logging.Payload.StringPayload; | 27 | | - | | 25 | +import com.google.cloud.logging.Severity; | 28 | 26 | import java.util.Collections; | 29 | 27 |
| | 28 | +/** | | 29 | + * This sample demonstrates writing logs using the Stackdriver Logging API. | | 30 | + * The library also offers a java.util.logging Handler `com.google.cloud.logging.LoggingHandler` | | 31 | + * Logback integration is also available : https://goo.gl/DNMoRh | | 32 | + * Using the java.util.logging handler / Logback appender should be preferred to using the API directly. | | 33 | + */ | 30 | 34 | public class QuickstartSample { | 31 | 35 |
| | 36 | + /** Expects a new or existing Stackdriver log name as the first argument.*/ | 32 | 37 | public static void main(String... args) throws Exception { | | 38 | + | 33 | 39 | // Instantiates a client | 34 | 40 | Logging logging = LoggingOptions.getDefaultInstance().getService(); | 35 | 41 |
| @@ -40,11 +46,12 @@ public static void main(String... args) throws Exception { | 40 | 46 | String text = "Hello, world!"; | 41 | 47 |
| 42 | 48 | LogEntry entry = LogEntry.newBuilder(StringPayload.of(text)) | | 49 | + .setSeverity(Severity.ERROR) | 43 | 50 | .setLogName(logName) | 44 | 51 | .setResource(MonitoredResource.newBuilder("global").build()) | 45 | 52 | .build(); | 46 | 53 |
| 47 | | - // Writes the log entry | | 54 | + // Writes the log entry asynchronously | 48 | 55 | logging.write(Collections.singleton(entry)); | 49 | 56 |
| 50 | 57 | System.out.printf("Logged: %s%n", text); | |
Collapse file Copy file name to clipboard +36-13Lines changed: 36 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | /* | 2 | | - Copyright 2016, Google, Inc. | | 2 | + Copyright 2017 Google Inc. | 3 | 3 |
| 4 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); | 5 | 5 | you may not use this file except in compliance with the License. | | 18 | 18 |
| 19 | 19 | import static com.google.common.truth.Truth.assertThat; | 20 | 20 |
| | 21 | +import com.google.cloud.MonitoredResource; | | 22 | +import com.google.cloud.logging.LogEntry; | 21 | 23 | import com.google.cloud.logging.Logging; | 22 | 24 | import com.google.cloud.logging.LoggingOptions; | | 25 | +import com.google.cloud.logging.Payload.StringPayload; | | 26 | +import java.io.ByteArrayOutputStream; | | 27 | +import java.io.PrintStream; | | 28 | +import java.util.Collections; | 23 | 29 | import org.junit.After; | 24 | 30 | import org.junit.Before; | 25 | 31 | import org.junit.Test; | 26 | 32 | import org.junit.runner.RunWith; | 27 | 33 | import org.junit.runners.JUnit4; | 28 | 34 |
| 29 | | -import java.io.ByteArrayOutputStream; | 30 | | -import java.io.PrintStream; | 31 | | - | 32 | 35 | /** | 33 | 36 | * Tests for quickstart sample. | 34 | 37 | */ | 35 | 38 | @RunWith(JUnit4.class) | 36 | 39 | @SuppressWarnings("checkstyle:abbreviationaswordinname") | 37 | | -public class QuickstartSampleIT { | | 40 | +public class LoggingIT { | 38 | 41 |
| 39 | 42 | private ByteArrayOutputStream bout; | 40 | 43 | private PrintStream out; | | 44 | + private Logging logging = LoggingOptions.getDefaultInstance().getService(); | 41 | 45 |
| 42 | | - private static final void deleteMyLog() { | 43 | | - Logging logging = LoggingOptions.getDefaultInstance().getService(); | 44 | | - | 45 | | - logging.deleteLog("my-log"); | | 46 | + private void deleteLog(String logName) { | | 47 | + logging.deleteLog(logName); | 46 | 48 | } | 47 | 49 |
| 48 | 50 | @Before | 49 | 51 | public void setUp() { | 50 | | - deleteMyLog(); | 51 | | - | 52 | 52 | bout = new ByteArrayOutputStream(); | 53 | 53 | out = new PrintStream(bout); | 54 | 54 | System.setOut(out); | @@ -57,13 +57,36 @@ public void setUp() { | 57 | 57 | @After | 58 | 58 | public void tearDown() { | 59 | 59 | System.setOut(null); | 60 | | - deleteMyLog(); | 61 | 60 | } | 62 | 61 |
| 63 | 62 | @Test | 64 | 63 | public void testQuickstart() throws Exception { | 65 | | - QuickstartSample.main("my-log"); | | 64 | + String logName = "my-log"; | | 65 | + deleteLog(logName); | | 66 | + QuickstartSample.main(logName); | 66 | 67 | String got = bout.toString(); | 67 | 68 | assertThat(got).contains("Logged: Hello, world!"); | | 69 | + deleteLog(logName); | | 70 | + } | | 71 | + | | 72 | + @Test(timeout = 10000) | | 73 | + public void testWriteAndListLogs() throws Exception { | | 74 | + String logName = "test-log"; | | 75 | + deleteLog(logName); | | 76 | + // write a log entry | | 77 | + LogEntry entry = LogEntry.newBuilder(StringPayload.of("Hello world again")) | | 78 | + .setLogName(logName) | | 79 | + .setResource(MonitoredResource.newBuilder("global").build()) | | 80 | + .build(); | | 81 | + logging.write(Collections.singleton(entry)); | | 82 | + // flush out log immediately | | 83 | + logging.flush(); | | 84 | + bout.reset(); | | 85 | + while (bout.toString().isEmpty()) { | | 86 | + ListLogs.main(logName); | | 87 | + Thread.sleep(1000); | | 88 | + } | | 89 | + assertThat(bout.toString().contains("Hello world again")).isTrue(); | | 90 | + deleteLog(logName); | 68 | 91 | } | 69 | 92 | } |
Collapse file Copy file name to clipboard +37Lines changed: 37 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# Getting Started with Stackdriver Logging using `java.util.logging` | | 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 write logs to Stackdriver using | | 7 | +the default Java Logging API (`java.util.logging`) handler for | | 8 | +[Google Cloud Client Library for Java][google-cloud-java]. | | 9 | + | | 10 | +[logging]: https://cloud.google.com/logging/ | | 11 | +[google-cloud-java]: https://github.com/GoogleCloudPlatform/google-cloud-java | | 12 | + | | 13 | +## Setup | | 14 | + | | 15 | +Install [Maven](http://maven.apache.org/). | | 16 | + | | 17 | +Build your project with: | | 18 | + | | 19 | + mvn clean package -DskipTests | | 20 | + | | 21 | +[Setup authentication](https://cloud.google.com/docs/authentication) using a service account. | | 22 | + | | 23 | +## Configuration | | 24 | + | | 25 | +Update [logging.properties](src/main/resources/logging.properties) to configure the handler. | | 26 | + | | 27 | +## Enhancers | | 28 | + | | 29 | +[ExampleEnhancer.java](src/main/java/com/example/logging/jul/enhancers/ExampleEnhancer.java) | | 30 | +provides an example of enhancing log entries with additional labels. | | 31 | + | | 32 | + | | 33 | +## Writing log entries | | 34 | + mvn exec:java -Dexec.mainClass=com.example.logging.jul.Quickstart \ | | 35 | + -Dexec.args="-Djava.util.logging.file=src/main/resources/logging.properties" | | 36 | + | | 37 | +Logs can be viewed using the [Logs Viewer Console](https://pantheon.corp.google.com/logs/viewer). |
Collapse file Copy file name to clipboard +57Lines changed: 57 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 | +<project> | | 17 | + <modelVersion>4.0.0</modelVersion> | | 18 | + <groupId>com.example.logging</groupId> | | 19 | + <artifactId>logging-google-cloud-samples-jul</artifactId> | | 20 | + <packaging>jar</packaging> | | 21 | + | | 22 | + <!-- Parent defines config for testing & linting. --> | | 23 | + <parent> | | 24 | + <groupId>com.google.cloud.logging.samples</groupId> | | 25 | + <artifactId>cloud-logging-samples</artifactId> | | 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>1.4.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.34</version> | | 54 | + <scope>test</scope> | | 55 | + </dependency> | | 56 | + </dependencies> | | 57 | +</project> |
Collapse file Copy file name to clipboard +31Lines changed: 31 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +/* | | 2 | + Copyright 2017 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.jul; | | 18 | + | | 19 | +// [START jul_quickstart] | | 20 | + | | 21 | +import java.util.logging.Logger; | | 22 | + | | 23 | +public class Quickstart { | | 24 | + private static final Logger logger = Logger.getLogger(Quickstart.class.getName()); | | 25 | + | | 26 | + public static void main(String[] args) { | | 27 | + logger.info("Logging INFO with java.util.logging"); | | 28 | + logger.severe("Logging ERROR with java.util.logging"); | | 29 | + } | | 30 | +} | | 31 | +// [END jul_quickstart] |
Collapse file Copy file name to clipboard +30Lines changed: 30 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +/* | | 2 | + * Copyright 2017 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.jul.enhancers; | | 18 | + | | 19 | +import com.google.cloud.logging.LogEntry; | | 20 | +import com.google.cloud.logging.LoggingEnhancer; | | 21 | + | | 22 | +// Add / update additional fields to the log entry | | 23 | +public class ExampleEnhancer implements LoggingEnhancer { | | 24 | + | | 25 | + @Override | | 26 | + public void enhanceLogEntry(LogEntry.Builder logEntry) { | | 27 | + // add additional labels | | 28 | + logEntry.addLabel("test-label-1", "test-value-1"); | | 29 | + } | | 30 | +} |
|