-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
samples: migrate logging samples from java-docs-samples (#98)
* samples: migrate logging samples from java-docs-samples * chore: run code formatter
- Loading branch information
Showing
8 changed files
with
459 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?xml version='1.0' encoding='UTF-8'?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>logging-install-with-bom</artifactId> | ||
<packaging>jar</packaging> | ||
<name>Google Cloud Logging Install With BOM Sample</name> | ||
<url>https://github.com/googleapis/java-logging</url> | ||
|
||
<parent> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>google-cloud-logging-samples</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
</parent> | ||
|
||
<properties> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<!-- [START logging_install_with_bom] --> | ||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>libraries-bom</artifactId> | ||
<version>4.1.0</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>google-cloud-logging</artifactId> | ||
</dependency> | ||
</dependencies> | ||
<!-- [END logging_install_with_bom] --> | ||
|
||
<!-- compile and run all snippet tests --> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<artifactId>build-helper-maven-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<id>add-snippets-source</id> | ||
<goals> | ||
<goal>add-source</goal> | ||
</goals> | ||
<configuration> | ||
<sources> | ||
<source>../snippets/src/main/java</source> | ||
</sources> | ||
</configuration> | ||
</execution> | ||
<execution> | ||
<id>add-snippets-tests</id> | ||
<goals> | ||
<goal>add-test-source</goal> | ||
</goals> | ||
<configuration> | ||
<sources> | ||
<source>../snippets/src/test/java</source> | ||
</sources> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?xml version='1.0' encoding='UTF-8'?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>logging-install-without-bom</artifactId> | ||
<packaging>jar</packaging> | ||
<name>Google Cloud Logging Install Without BOM Sample</name> | ||
<url>https://github.com/googleapis/java-logging</url> | ||
|
||
<parent> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>google-cloud-logging-samples</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
</parent> | ||
|
||
<properties> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<!-- {x-version-update-start:google-cloud-logging:released} --> | ||
<dependencies> | ||
<!-- [START logging_install_without_bom] --> | ||
<dependency> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>google-cloud-logging</artifactId> | ||
<version>1.100.0</version> | ||
</dependency> | ||
<!-- [END logging_install_without_bom] --> | ||
</dependencies> | ||
<!-- {x-version-update-end} --> | ||
|
||
<!-- compile and run all snippet tests --> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<artifactId>build-helper-maven-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<id>add-snippets-source</id> | ||
<goals> | ||
<goal>add-source</goal> | ||
</goals> | ||
<configuration> | ||
<sources> | ||
<source>../snippets/src/main/java</source> | ||
</sources> | ||
</configuration> | ||
</execution> | ||
<execution> | ||
<id>add-snippets-tests</id> | ||
<goals> | ||
<goal>add-test-source</goal> | ||
</goals> | ||
<configuration> | ||
<sources> | ||
<source>../snippets/src/test/java</source> | ||
</sources> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?xml version='1.0' encoding='UTF-8'?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>google-cloud-logging-samples</artifactId> | ||
<version>0.0.1-SNAPSHOT</version><!-- This artifact should not be released --> | ||
<packaging>pom</packaging> | ||
<name>Google Cloud Logging Samples Parent</name> | ||
<url>https://github.com/googleapis/java-logging</url> | ||
<description> | ||
Java idiomatic client for Google Cloud Platform services. | ||
</description> | ||
|
||
<!-- | ||
The parent pom defines common style checks and testing strategies for our samples. | ||
Removing or replacing it should not affect the execution of the samples in any way. | ||
--> | ||
<parent> | ||
<groupId>com.google.cloud.samples</groupId> | ||
<artifactId>shared-configuration</artifactId> | ||
<version>1.0.11</version> | ||
</parent> | ||
|
||
<properties> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<modules> | ||
<module>install-with-bom</module> | ||
<module>install-without-bom</module> | ||
<module>snippets</module> | ||
</modules> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-deploy-plugin</artifactId> | ||
<version>2.8.2</version> | ||
<configuration> | ||
<skip>true</skip> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.sonatype.plugins</groupId> | ||
<artifactId>nexus-staging-maven-plugin</artifactId> | ||
<version>1.6.8</version> | ||
<configuration> | ||
<skipNexusStagingDeployMojo>true</skipNexusStagingDeployMojo> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.13</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.truth</groupId> | ||
<artifactId>truth</artifactId> | ||
<version>1.0.1</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?xml version='1.0' encoding='UTF-8'?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>logging-snippets</artifactId> | ||
<packaging>jar</packaging> | ||
<name>Google Cloud Logging Snippets</name> | ||
<url>https://github.com/googleapis/java-logging</url> | ||
|
||
<parent> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>google-cloud-logging-samples</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
</parent> | ||
|
||
<properties> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>google-cloud-logging</artifactId> | ||
<version>1.100.1-SNAPSHOT</version><!-- {x-version-update:google-cloud-logging:current} --> | ||
</dependency> | ||
</dependencies> | ||
</project> |
51 changes: 51 additions & 0 deletions
51
samples/snippets/src/main/java/com/example/logging/ListLogs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* 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; | ||
|
||
import com.google.api.gax.paging.Page; | ||
import com.google.cloud.logging.LogEntry; | ||
import com.google.cloud.logging.Logging; | ||
import com.google.cloud.logging.Logging.EntryListOption; | ||
import com.google.cloud.logging.LoggingOptions; | ||
|
||
/** List logs programmatically using the StackDriver Logging API. */ | ||
public class ListLogs { | ||
|
||
/** Expects an existing Stackdriver log name as an argument. */ | ||
public static void main(String... args) throws Exception { | ||
// [START logging_list_log_entries] | ||
// Instantiates a client | ||
LoggingOptions options = LoggingOptions.getDefaultInstance(); | ||
|
||
String logName = args[0]; | ||
|
||
try (Logging logging = options.getService()) { | ||
|
||
String logFilter = "logName=projects/" + options.getProjectId() + "/logs/" + logName; | ||
|
||
// List all log entries | ||
Page<LogEntry> entries = logging.listLogEntries(EntryListOption.filter(logFilter)); | ||
do { | ||
for (LogEntry logEntry : entries.iterateAll()) { | ||
System.out.println(logEntry); | ||
} | ||
entries = entries.getNextPage(); | ||
} while (entries != null); | ||
} | ||
// [END logging_list_log_entries] | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
samples/snippets/src/main/java/com/example/logging/QuickstartSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* 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] | ||
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/googleapis/google-cloud-java/tree/master/google-cloud-clients/google-cloud-contrib/google-cloud-logging-logback | ||
* Using the java.util.logging handler / Logback appender should be preferred to using the API | ||
* directly. | ||
*/ | ||
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(); | ||
|
||
// 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)) | ||
.setSeverity(Severity.ERROR) | ||
.setLogName(logName) | ||
.setResource(MonitoredResource.newBuilder("global").build()) | ||
.build(); | ||
|
||
// Writes the log entry asynchronously | ||
logging.write(Collections.singleton(entry)); | ||
|
||
System.out.printf("Logged: %s%n", text); | ||
} | ||
} | ||
// [END logging_quickstart] |
Oops, something went wrong.