Skip to content

Commit 1842375

Browse files
committed
Add logging to README.md and TESTING.md, fix package grouping
1 parent a783200 commit 1842375

3 files changed

Lines changed: 102 additions & 27 deletions

File tree

README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ This client supports the following Google Cloud Platform services:
1818
- [Google Cloud Compute] (#google-cloud-compute-alpha) (Alpha)
1919
- [Google Cloud Datastore] (#google-cloud-datastore)
2020
- [Google Cloud DNS] (#google-cloud-dns-alpha) (Alpha)
21+
- [Stackdriver Logging] (#stackdriver-logging-alpha) (Alpha - Not working on App Engine Standard)
2122
- [Google Cloud Pub/Sub] (#google-cloud-pubsub-alpha) (Alpha - Not working on App Engine Standard)
2223
- [Google Cloud Resource Manager] (#google-cloud-resource-manager-alpha) (Alpha)
2324
- [Google Cloud Storage] (#google-cloud-storage)
@@ -371,6 +372,75 @@ ChangeRequestInfo changeRequest = changeBuilder.build();
371372
zone.applyChangeRequest(changeRequest);
372373
```
373374
375+
Stackdriver Logging (Alpha)
376+
----------------------
377+
- [API Documentation][logging-api]
378+
- [Official Documentation][stackdriver-logging-docs]
379+
380+
*Follow the [activation instructions][stackdriver-logging-activation] to use the Stackdriver Logging
381+
API with your project.*
382+
383+
#### Preview
384+
385+
Here are two code snippets showing simple usage examples from within Compute Engine/App Engine
386+
Flexible. Note that you must [supply credentials](#authentication) and a project ID if running this
387+
snippet elsewhere.
388+
389+
The first snippet shows how to write and list log entries. Complete source code can be found on
390+
[WriteAndListLogEntries.java](./gcloud-java-examples/src/main/java/com/google/cloud/examples/logging/snippets/WriteAndListLogEntries.java).
391+
392+
```java
393+
import com.google.cloud.MonitoredResource;
394+
import com.google.cloud.Page;
395+
import com.google.cloud.logging.LogEntry;
396+
import com.google.cloud.logging.Logging;
397+
import com.google.cloud.logging.Logging.EntryListOption;
398+
import com.google.cloud.logging.LoggingOptions;
399+
import com.google.cloud.logging.Payload.StringPayload;
400+
401+
import java.util.Collections;
402+
import java.util.Iterator;
403+
404+
LoggingOptions options = LoggingOptions.defaultInstance();
405+
try(Logging logging = options.service()) {
406+
407+
LogEntry firstEntry = LogEntry.builder(StringPayload.of("message"))
408+
.logName("test-log")
409+
.resource(MonitoredResource.builder("global")
410+
.addLabel("project_id", options.projectId())
411+
.build())
412+
.build();
413+
logging.write(Collections.singleton(firstEntry));
414+
415+
Page<LogEntry> entries = logging.listLogEntries(
416+
EntryListOption.filter("logName=projects/" + options.projectId() + "/logs/test-log"));
417+
Iterator<LogEntry> entryIterator = entries.iterateAll();
418+
while (entryIterator.hasNext()) {
419+
System.out.println(entryIterator.next());
420+
}
421+
}
422+
```
423+
424+
The second snippet shows how to use a `java.util.logging.Logger` to write log entries to Stackdriver
425+
Logging. The snippet installs a Stackdriver Logging handler using
426+
`LoggingHandler.addHandler(Logger, LoggingHandler)`. Notice that this could also be done through the
427+
`logging.properties` file, adding the following line:
428+
```
429+
com.google.cloud.examples.logging.snippets.AddLoggingHandler.handlers=com.google.cloud.logging.LoggingHandler
430+
```
431+
The complete code can be found on
432+
[AddLoggingHandler.java](./gcloud-java-examples/src/main/java/com/google/cloud/examples/logging/snippets/AddLoggingHandler.java).
433+
434+
```java
435+
import com.google.cloud.logging.LoggingHandler;
436+
437+
import java.util.logging.Logger;
438+
439+
Logger logger = Logger.getLogger(AddLoggingHandler.class.getName());
440+
LoggingHandler.addHandler(logger, new LoggingHandler());
441+
logger.warning("test warning");
442+
```
443+
374444
Google Cloud Pub/Sub (Alpha)
375445
----------------------
376446
@@ -554,6 +624,10 @@ Apache 2.0 - See [LICENSE] for more information.
554624
[cloud-dns-docs]: https://cloud.google.com/dns/docs
555625
[cloud-dns-activation]: https://console.cloud.google.com/start/api?id=dns
556626
627+
[logging-api]: http://googlecloudplatform.github.io/gcloud-java/apidocs/index.html?com/google/cloud/logging/package-summary.html
628+
[stackdriver-logging-docs]: https://cloud.google.com/logging/docs
629+
[stackdriver-logging-activation]: https://console.cloud.google.com/start/api?id=logging
630+
557631
[pubsub-api]: http://googlecloudplatform.github.io/gcloud-java/apidocs/index.html?com/google/cloud/pubsub/package-summary.html
558632
[cloud-pubsub]: https://cloud.google.com/pubsub/
559633
[cloud-pubsub-docs]: https://cloud.google.com/pubsub/docs

TESTING.md

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ This library provides tools to help write tests for code that uses the following
66
- [Compute] (#testing-code-that-uses-compute)
77
- [Datastore] (#testing-code-that-uses-datastore)
88
- [DNS] (#testing-code-that-uses-dns)
9+
- [Logging] (#testing-code-that-uses-logging)
910
- [PubSub] (#testing-code-that-uses-pubsub)
1011
- [Resource Manager] (#testing-code-that-uses-resource-manager)
1112
- [Storage] (#testing-code-that-uses-storage)
@@ -146,6 +147,31 @@ You can test against an in-memory local DNS by following these steps:
146147

147148
This method will block until the server thread has been terminated.
148149

150+
### Testing code that uses Logging
151+
152+
Currently, there isn't an emulator for Stackdriver Logging, so an alternative is to create a test
153+
project. `RemoteLoggingHelper` contains convenience methods to make setting up the test project
154+
easier. To use this class, follow the steps below:
155+
156+
1. Create a test Google Cloud project.
157+
158+
2. Download a [JSON service account credentials file][create-service-account] from the Google
159+
Developer's Console.
160+
161+
3. Create a `RemoteLoggingHelper` object using your project ID and JSON key. Here is an example that
162+
uses the `RemoteLoggingHelper` to create a metric.
163+
```java
164+
RemoteLoggingHelper loggingHelper =
165+
RemoteLoggingHelper.create(PROJECT_ID, new FileInputStream("/path/to/my/JSON/key.json"));
166+
Logging logging = loggingHelper.options().service();
167+
// Pick a name for the resource with low probability of clashing
168+
String metricName = RemoteLoggingHelper.formatForTest("test-metric");
169+
MetricInfo metricInfo = MetricInfo.of(name, "logName:syslog");
170+
Metric metric = logging.create(metricInfo);
171+
```
172+
173+
4. Run your tests.
174+
149175
### Testing code that uses Pub/Sub
150176

151177
#### On your machine
@@ -218,31 +244,6 @@ You can test against an in-memory local Resource Manager by following these step
218244

219245
This method will block until the server thread has been terminated.
220246

221-
### Testing code that uses Logging
222-
223-
Currently, there isn't an emulator for Stackdriver Logging, so an alternative is to create a test
224-
project. `RemoteLoggingHelper` contains convenience methods to make setting up the test project
225-
easier. To use this class, follow the steps below:
226-
227-
1. Create a test Google Cloud project.
228-
229-
2. Download a [JSON service account credentials file][create-service-account] from the Google
230-
Developer's Console.
231-
232-
3. Create a `RemoteLoggingHelper` object using your project ID and JSON key. Here is an example that
233-
uses the `RemoteLoggingHelper` to create a metric.
234-
```java
235-
RemoteLoggingHelper loggingHelper =
236-
RemoteLoggingHelper.create(PROJECT_ID, new FileInputStream("/path/to/my/JSON/key.json"));
237-
Logging logging = loggingHelper.options().service();
238-
// Pick a name for the resource with low probability of clashing
239-
String metricName = RemoteLoggingHelper.formatForTest("test-metric");
240-
MetricInfo metricInfo = MetricInfo.of(name, "logName:syslog");
241-
Metric metric = logging.create(metricInfo);
242-
```
243-
244-
4. Run your tests.
245-
246247
### Testing code that uses Storage
247248

248249
Currently, there isn't an emulator for Google Cloud Storage, so an alternative is to create a test project. `RemoteStorageHelper` contains convenience methods to make setting up and cleaning up the test project easier. To use this class, follow the steps below:

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,15 +401,15 @@
401401
</group>
402402
<group>
403403
<title>Test helpers packages</title>
404-
<packages>com.google.cloud.bigquery.testing:com.google.cloud.compute.testing:com.google.cloud.datastore.testing:com.google.cloud.dns.testing:com.google.cloud.pubsub.testing:com.google.cloud.resourcemanager.testing:com.google.cloud.storage.testing</packages>
404+
<packages>com.google.cloud.bigquery.testing:com.google.cloud.compute.testing:com.google.cloud.datastore.testing:com.google.cloud.dns.testing:com.google.cloud.logging.testing:com.google.cloud.pubsub.testing:com.google.cloud.resourcemanager.testing:com.google.cloud.storage.testing</packages>
405405
</group>
406406
<group>
407407
<title>Example packages</title>
408408
<packages>com.google.cloud.examples.*:com.google.cloud.nio.examples</packages>
409409
</group>
410410
<group>
411411
<title>SPI packages</title>
412-
<packages>com.google.cloud.spi:com.google.cloud.bigquery.spi:com.google.cloud.compute.spi:com.google.cloud.datastore.spi:com.google.cloud.dns.spi:com.google.cloud.pubsub.spi:com.google.cloud.pubsub.spi.*:com.google.cloud.resourcemanager.spi:com.google.cloud.storage.spi</packages>
412+
<packages>com.google.cloud.spi:com.google.cloud.bigquery.spi:com.google.cloud.compute.spi:com.google.cloud.datastore.spi:com.google.cloud.dns.spi:com.google.cloud.logging.spi:com.google.cloud.logging.spi.*:com.google.cloud.pubsub.spi:com.google.cloud.pubsub.spi.*:com.google.cloud.resourcemanager.spi:com.google.cloud.storage.spi</packages>
413413
</group>
414414
</groups>
415415
</configuration>

0 commit comments

Comments
 (0)