Skip to content

Commit 0b55164

Browse files
authored
samples: migrate logging samples from java-docs-samples (#98)
* samples: migrate logging samples from java-docs-samples * chore: run code formatter
1 parent 55f6936 commit 0b55164

8 files changed

Lines changed: 459 additions & 0 deletions

File tree

pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,4 +308,13 @@
308308
</plugin>
309309
</plugins>
310310
</reporting>
311+
312+
<profiles>
313+
<profile>
314+
<id>enable-samples</id>
315+
<modules>
316+
<module>samples</module>
317+
</modules>
318+
</profile>
319+
</profiles>
311320
</project>

samples/install-with-bom/pom.xml

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<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">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.google.cloud</groupId>
5+
<artifactId>logging-install-with-bom</artifactId>
6+
<packaging>jar</packaging>
7+
<name>Google Cloud Logging Install With BOM Sample</name>
8+
<url>https://github.com/googleapis/java-logging</url>
9+
10+
<parent>
11+
<groupId>com.google.cloud</groupId>
12+
<artifactId>google-cloud-logging-samples</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
</parent>
15+
16+
<properties>
17+
<maven.compiler.target>1.8</maven.compiler.target>
18+
<maven.compiler.source>1.8</maven.compiler.source>
19+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20+
</properties>
21+
22+
<!-- [START logging_install_with_bom] -->
23+
<dependencyManagement>
24+
<dependencies>
25+
<dependency>
26+
<groupId>com.google.cloud</groupId>
27+
<artifactId>libraries-bom</artifactId>
28+
<version>4.1.0</version>
29+
<type>pom</type>
30+
<scope>import</scope>
31+
</dependency>
32+
</dependencies>
33+
</dependencyManagement>
34+
35+
<dependencies>
36+
<dependency>
37+
<groupId>com.google.cloud</groupId>
38+
<artifactId>google-cloud-logging</artifactId>
39+
</dependency>
40+
</dependencies>
41+
<!-- [END logging_install_with_bom] -->
42+
43+
<!-- compile and run all snippet tests -->
44+
<build>
45+
<plugins>
46+
<plugin>
47+
<groupId>org.codehaus.mojo</groupId>
48+
<artifactId>build-helper-maven-plugin</artifactId>
49+
<executions>
50+
<execution>
51+
<id>add-snippets-source</id>
52+
<goals>
53+
<goal>add-source</goal>
54+
</goals>
55+
<configuration>
56+
<sources>
57+
<source>../snippets/src/main/java</source>
58+
</sources>
59+
</configuration>
60+
</execution>
61+
<execution>
62+
<id>add-snippets-tests</id>
63+
<goals>
64+
<goal>add-test-source</goal>
65+
</goals>
66+
<configuration>
67+
<sources>
68+
<source>../snippets/src/test/java</source>
69+
</sources>
70+
</configuration>
71+
</execution>
72+
</executions>
73+
</plugin>
74+
</plugins>
75+
</build>
76+
</project>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<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">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.google.cloud</groupId>
5+
<artifactId>logging-install-without-bom</artifactId>
6+
<packaging>jar</packaging>
7+
<name>Google Cloud Logging Install Without BOM Sample</name>
8+
<url>https://github.com/googleapis/java-logging</url>
9+
10+
<parent>
11+
<groupId>com.google.cloud</groupId>
12+
<artifactId>google-cloud-logging-samples</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
</parent>
15+
16+
<properties>
17+
<maven.compiler.target>1.8</maven.compiler.target>
18+
<maven.compiler.source>1.8</maven.compiler.source>
19+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20+
</properties>
21+
22+
<!-- {x-version-update-start:google-cloud-logging:released} -->
23+
<dependencies>
24+
<!-- [START logging_install_without_bom] -->
25+
<dependency>
26+
<groupId>com.google.cloud</groupId>
27+
<artifactId>google-cloud-logging</artifactId>
28+
<version>1.100.0</version>
29+
</dependency>
30+
<!-- [END logging_install_without_bom] -->
31+
</dependencies>
32+
<!-- {x-version-update-end} -->
33+
34+
<!-- compile and run all snippet tests -->
35+
<build>
36+
<plugins>
37+
<plugin>
38+
<groupId>org.codehaus.mojo</groupId>
39+
<artifactId>build-helper-maven-plugin</artifactId>
40+
<executions>
41+
<execution>
42+
<id>add-snippets-source</id>
43+
<goals>
44+
<goal>add-source</goal>
45+
</goals>
46+
<configuration>
47+
<sources>
48+
<source>../snippets/src/main/java</source>
49+
</sources>
50+
</configuration>
51+
</execution>
52+
<execution>
53+
<id>add-snippets-tests</id>
54+
<goals>
55+
<goal>add-test-source</goal>
56+
</goals>
57+
<configuration>
58+
<sources>
59+
<source>../snippets/src/test/java</source>
60+
</sources>
61+
</configuration>
62+
</execution>
63+
</executions>
64+
</plugin>
65+
</plugins>
66+
</build>
67+
</project>

samples/pom.xml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<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">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.google.cloud</groupId>
5+
<artifactId>google-cloud-logging-samples</artifactId>
6+
<version>0.0.1-SNAPSHOT</version><!-- This artifact should not be released -->
7+
<packaging>pom</packaging>
8+
<name>Google Cloud Logging Samples Parent</name>
9+
<url>https://github.com/googleapis/java-logging</url>
10+
<description>
11+
Java idiomatic client for Google Cloud Platform services.
12+
</description>
13+
14+
<!--
15+
The parent pom defines common style checks and testing strategies for our samples.
16+
Removing or replacing it should not affect the execution of the samples in any way.
17+
-->
18+
<parent>
19+
<groupId>com.google.cloud.samples</groupId>
20+
<artifactId>shared-configuration</artifactId>
21+
<version>1.0.11</version>
22+
</parent>
23+
24+
<properties>
25+
<maven.compiler.target>1.8</maven.compiler.target>
26+
<maven.compiler.source>1.8</maven.compiler.source>
27+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
28+
</properties>
29+
30+
<modules>
31+
<module>install-with-bom</module>
32+
<module>install-without-bom</module>
33+
<module>snippets</module>
34+
</modules>
35+
36+
<build>
37+
<plugins>
38+
<plugin>
39+
<groupId>org.apache.maven.plugins</groupId>
40+
<artifactId>maven-deploy-plugin</artifactId>
41+
<version>2.8.2</version>
42+
<configuration>
43+
<skip>true</skip>
44+
</configuration>
45+
</plugin>
46+
<plugin>
47+
<groupId>org.sonatype.plugins</groupId>
48+
<artifactId>nexus-staging-maven-plugin</artifactId>
49+
<version>1.6.8</version>
50+
<configuration>
51+
<skipNexusStagingDeployMojo>true</skipNexusStagingDeployMojo>
52+
</configuration>
53+
</plugin>
54+
</plugins>
55+
</build>
56+
57+
<dependencies>
58+
<dependency>
59+
<groupId>junit</groupId>
60+
<artifactId>junit</artifactId>
61+
<version>4.13</version>
62+
<scope>test</scope>
63+
</dependency>
64+
<dependency>
65+
<groupId>com.google.truth</groupId>
66+
<artifactId>truth</artifactId>
67+
<version>1.0.1</version>
68+
<scope>test</scope>
69+
</dependency>
70+
</dependencies>
71+
</project>

samples/snippets/pom.xml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<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">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.google.cloud</groupId>
5+
<artifactId>logging-snippets</artifactId>
6+
<packaging>jar</packaging>
7+
<name>Google Cloud Logging Snippets</name>
8+
<url>https://github.com/googleapis/java-logging</url>
9+
10+
<parent>
11+
<groupId>com.google.cloud</groupId>
12+
<artifactId>google-cloud-logging-samples</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
</parent>
15+
16+
<properties>
17+
<maven.compiler.target>1.8</maven.compiler.target>
18+
<maven.compiler.source>1.8</maven.compiler.source>
19+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>com.google.cloud</groupId>
25+
<artifactId>google-cloud-logging</artifactId>
26+
<version>1.100.1-SNAPSHOT</version><!-- {x-version-update:google-cloud-logging:current} -->
27+
</dependency>
28+
</dependencies>
29+
</project>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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;
18+
19+
import com.google.api.gax.paging.Page;
20+
import com.google.cloud.logging.LogEntry;
21+
import com.google.cloud.logging.Logging;
22+
import com.google.cloud.logging.Logging.EntryListOption;
23+
import com.google.cloud.logging.LoggingOptions;
24+
25+
/** List logs programmatically using the StackDriver Logging API. */
26+
public class ListLogs {
27+
28+
/** Expects an existing Stackdriver log name as an argument. */
29+
public static void main(String... args) throws Exception {
30+
// [START logging_list_log_entries]
31+
// Instantiates a client
32+
LoggingOptions options = LoggingOptions.getDefaultInstance();
33+
34+
String logName = args[0];
35+
36+
try (Logging logging = options.getService()) {
37+
38+
String logFilter = "logName=projects/" + options.getProjectId() + "/logs/" + logName;
39+
40+
// List all log entries
41+
Page<LogEntry> entries = logging.listLogEntries(EntryListOption.filter(logFilter));
42+
do {
43+
for (LogEntry logEntry : entries.iterateAll()) {
44+
System.out.println(logEntry);
45+
}
46+
entries = entries.getNextPage();
47+
} while (entries != null);
48+
}
49+
// [END logging_list_log_entries]
50+
}
51+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
import com.google.cloud.MonitoredResource;
21+
import com.google.cloud.logging.LogEntry;
22+
import com.google.cloud.logging.Logging;
23+
import com.google.cloud.logging.LoggingOptions;
24+
import com.google.cloud.logging.Payload.StringPayload;
25+
import com.google.cloud.logging.Severity;
26+
import java.util.Collections;
27+
28+
/**
29+
* This sample demonstrates writing logs using the Stackdriver Logging API. The library also offers
30+
* a java.util.logging Handler `com.google.cloud.logging.LoggingHandler` Logback integration is also
31+
* available :
32+
* https://github.com/googleapis/google-cloud-java/tree/master/google-cloud-clients/google-cloud-contrib/google-cloud-logging-logback
33+
* Using the java.util.logging handler / Logback appender should be preferred to using the API
34+
* directly.
35+
*/
36+
public class QuickstartSample {
37+
38+
/** Expects a new or existing Stackdriver log name as the first argument. */
39+
public static void main(String... args) throws Exception {
40+
41+
// Instantiates a client
42+
Logging logging = LoggingOptions.getDefaultInstance().getService();
43+
44+
// The name of the log to write to
45+
String logName = args[0]; // "my-log";
46+
47+
// The data to write to the log
48+
String text = "Hello, world!";
49+
50+
LogEntry entry =
51+
LogEntry.newBuilder(StringPayload.of(text))
52+
.setSeverity(Severity.ERROR)
53+
.setLogName(logName)
54+
.setResource(MonitoredResource.newBuilder("global").build())
55+
.build();
56+
57+
// Writes the log entry asynchronously
58+
logging.write(Collections.singleton(entry));
59+
60+
System.out.printf("Logged: %s%n", text);
61+
}
62+
}
63+
// [END logging_quickstart]

0 commit comments

Comments
 (0)