Skip to content

Commit 7cb86d1

Browse files
author
Frank Natividad
committed
Adding Logging Quickstart Test and updated deprecated function
1 parent 9fc6eed commit 7cb86d1

File tree

2 files changed

+78
-7
lines changed

2 files changed

+78
-7
lines changed

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
// [START logging_quickstart]
2020
// Imports the Google Cloud client library
21+
2122
import com.google.cloud.MonitoredResource;
2223
import com.google.cloud.logging.LogEntry;
2324
import com.google.cloud.logging.Logging;
@@ -27,9 +28,10 @@
2728
import java.util.Collections;
2829

2930
public class QuickstartSample {
31+
3032
public static void main(String... args) throws Exception {
3133
// Instantiates a client
32-
Logging logging = LoggingOptions.defaultInstance().service();
34+
Logging logging = LoggingOptions.getDefaultInstance().getService();
3335

3436
// The name of the log to write to
3537
String logName = args[0]; // "my-log";
@@ -38,14 +40,14 @@ public static void main(String... args) throws Exception {
3840
String text = "Hello, world!";
3941

4042
LogEntry entry = LogEntry.newBuilder(StringPayload.of(text))
41-
.setLogName(logName)
42-
.setResource(MonitoredResource.builder("global").build())
43-
.build();
43+
.setLogName(logName)
44+
.setResource(MonitoredResource.newBuilder("global").build())
45+
.build();
4446

45-
// Writes the log entry
46-
logging.write(Collections.singleton(entry));
47+
// Writes the log entry
48+
logging.write(Collections.singleton(entry));
4749

48-
System.out.printf("Logged: %s%n", text);
50+
System.out.printf("Logged: %s%n", text);
4951
}
5052
}
5153
// [END logging_quickstart]
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import com.google.cloud.logging.Logging;
22+
import com.google.cloud.logging.LoggingOptions;
23+
import org.junit.After;
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
import org.junit.runner.RunWith;
27+
import org.junit.runners.JUnit4;
28+
29+
import java.io.ByteArrayOutputStream;
30+
import java.io.PrintStream;
31+
32+
/**
33+
* Tests for quickstart sample.
34+
*/
35+
@RunWith(JUnit4.class)
36+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
37+
public class QuickstartSampleIT {
38+
39+
private ByteArrayOutputStream bout;
40+
private PrintStream out;
41+
42+
private static final void deleteMyLog() {
43+
Logging logging = LoggingOptions.getDefaultInstance().getService();
44+
45+
logging.deleteLog("my-log");
46+
}
47+
48+
@Before
49+
public void setUp() {
50+
deleteMyLog();
51+
52+
bout = new ByteArrayOutputStream();
53+
out = new PrintStream(bout);
54+
System.setOut(out);
55+
}
56+
57+
@After
58+
public void tearDown() {
59+
System.setOut(null);
60+
deleteMyLog();
61+
}
62+
63+
@Test
64+
public void testQuickstart() throws Exception {
65+
QuickstartSample.main("my-log");
66+
String got = bout.toString();
67+
assertThat(got).contains("Logged: Hello, world!");
68+
}
69+
}

0 commit comments

Comments
 (0)