Skip to content

Commit 219f9ad

Browse files
authored
Stackdriver logging samples (JUL, Logback, Client library) (#826)
1 parent 3e5c3d1 commit 219f9ad

File tree

18 files changed

+446
-237
lines changed

18 files changed

+446
-237
lines changed

logging/cloud-client/README.md

+13-8
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,32 @@
33
[Stackdriver Logging][logging] allows you to store, search, analyze, monitor,
44
and alert on log data and events from Google Cloud Platform and Amazon Web
55
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
77
the [Google Cloud Client Library for Java][google-cloud-java].
88

99
[logging]: https://cloud.google.com/logging/
1010
[google-cloud-java]: https://github.com/GoogleCloudPlatform/google-cloud-java
1111

12-
## Quickstart
12+
## Setup
1313

1414
Install [Maven](http://maven.apache.org/).
1515

1616
Build your project with:
1717

1818
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.
2521

2622
### Writing a log entry (using the quickstart sample)
2723

2824
mvn exec:java -Dexec.mainClass=com.example.logging.QuickstartSample \
2925
-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).

logging/cloud-client/pom.xml

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616
<project>
1717
<modelVersion>4.0.0</modelVersion>
1818
<groupId>com.example.logging</groupId>
19-
<artifactId>logging-google-cloud-samples</artifactId>
19+
<artifactId>logging-google-cloud-samples-api</artifactId>
2020
<packaging>jar</packaging>
2121

2222
<!-- Parent defines config for testing & linting. -->
2323
<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>
2626
<version>1.0.0</version>
27-
<relativePath>../..</relativePath>
27+
<relativePath>..</relativePath>
2828
</parent>
2929

3030
<properties>
@@ -37,7 +37,7 @@
3737
<dependency>
3838
<groupId>com.google.cloud</groupId>
3939
<artifactId>google-cloud-logging</artifactId>
40-
<version>1.3.1</version>
40+
<version>1.4.0</version>
4141
</dependency>
4242

4343
<!-- Test dependencies -->

logging/cloud-client/src/main/java/com/example/logging/QuickstartSample.java

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2016, Google, Inc.
2+
Copyright 2016 Google Inc.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -17,19 +17,25 @@
1717
package com.example.logging;
1818

1919
// [START logging_quickstart]
20-
// Imports the Google Cloud client library
21-
2220
import com.google.cloud.MonitoredResource;
2321
import com.google.cloud.logging.LogEntry;
2422
import com.google.cloud.logging.Logging;
2523
import com.google.cloud.logging.LoggingOptions;
2624
import com.google.cloud.logging.Payload.StringPayload;
27-
25+
import com.google.cloud.logging.Severity;
2826
import java.util.Collections;
2927

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+
*/
3034
public class QuickstartSample {
3135

36+
/** Expects a new or existing Stackdriver log name as the first argument.*/
3237
public static void main(String... args) throws Exception {
38+
3339
// Instantiates a client
3440
Logging logging = LoggingOptions.getDefaultInstance().getService();
3541

@@ -40,11 +46,12 @@ public static void main(String... args) throws Exception {
4046
String text = "Hello, world!";
4147

4248
LogEntry entry = LogEntry.newBuilder(StringPayload.of(text))
49+
.setSeverity(Severity.ERROR)
4350
.setLogName(logName)
4451
.setResource(MonitoredResource.newBuilder("global").build())
4552
.build();
4653

47-
// Writes the log entry
54+
// Writes the log entry asynchronously
4855
logging.write(Collections.singleton(entry));
4956

5057
System.out.printf("Logged: %s%n", text);
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2016, Google, Inc.
2+
Copyright 2017 Google Inc.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -18,37 +18,37 @@
1818

1919
import static com.google.common.truth.Truth.assertThat;
2020

21+
import com.google.cloud.MonitoredResource;
22+
import com.google.cloud.logging.LogEntry;
2123
import com.google.cloud.logging.Logging;
2224
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;
2329
import org.junit.After;
2430
import org.junit.Before;
2531
import org.junit.Test;
2632
import org.junit.runner.RunWith;
2733
import org.junit.runners.JUnit4;
2834

29-
import java.io.ByteArrayOutputStream;
30-
import java.io.PrintStream;
31-
3235
/**
3336
* Tests for quickstart sample.
3437
*/
3538
@RunWith(JUnit4.class)
3639
@SuppressWarnings("checkstyle:abbreviationaswordinname")
37-
public class QuickstartSampleIT {
40+
public class LoggingIT {
3841

3942
private ByteArrayOutputStream bout;
4043
private PrintStream out;
44+
private Logging logging = LoggingOptions.getDefaultInstance().getService();
4145

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);
4648
}
4749

4850
@Before
4951
public void setUp() {
50-
deleteMyLog();
51-
5252
bout = new ByteArrayOutputStream();
5353
out = new PrintStream(bout);
5454
System.setOut(out);
@@ -57,13 +57,36 @@ public void setUp() {
5757
@After
5858
public void tearDown() {
5959
System.setOut(null);
60-
deleteMyLog();
6160
}
6261

6362
@Test
6463
public void testQuickstart() throws Exception {
65-
QuickstartSample.main("my-log");
64+
String logName = "my-log";
65+
deleteLog(logName);
66+
QuickstartSample.main(logName);
6667
String got = bout.toString();
6768
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);
6891
}
6992
}

logging/jul/README.md

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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).

logging/jul/pom.xml

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
}

0 commit comments

Comments
 (0)