Skip to content

Commit 993d022

Browse files
committed
Add logging sample.
1 parent 47fb0c5 commit 993d022

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

logging/cloud-client/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Getting Started with Stackdriver Logging and the Google Cloud Client libraries
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 access the Cloud Storage API using
7+
the [Google Cloud Client Library for Java][google-cloud-java].
8+
9+
[logging]: https://cloud.google.com/logging/
10+
[google-cloud-java]: https://github.com/GoogleCloudPlatform/google-cloud-java
11+
12+
## Quickstart
13+
14+
Install [Maven](http://maven.apache.org/).
15+
16+
Build your project with:
17+
18+
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"
25+
26+
### Writing a log entry (using the quickstart sample)
27+
28+
mvn exec:java -Dexec.mainClass=com.example.logging.QuickstartSample \
29+
-Dexec.args="my-log"

logging/cloud-client/pom.xml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<!--
2+
Copyright 2016 Google Inc. All Rights Reserved.
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>come.example.logging</groupId>
19+
<artifactId>logging-google-cloud-samples</artifactId>
20+
<packaging>jar</packaging>
21+
22+
<!-- Parent defines config for testing & linting. -->
23+
<parent>
24+
<artifactId>doc-samples</artifactId>
25+
<groupId>com.google.cloud</groupId>
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>0.6.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.30</version>
54+
<scope>test</scope>
55+
</dependency>
56+
</dependencies>
57+
</project>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
// Imports the Google Cloud client library
21+
import com.google.cloud.MonitoredResource;
22+
import com.google.cloud.logging.LogEntry;
23+
import com.google.cloud.logging.Logging;
24+
import com.google.cloud.logging.LoggingOptions;
25+
import com.google.cloud.logging.Payload.StringPayload;
26+
27+
import java.util.Collections;
28+
29+
public class QuickstartSample {
30+
public static void main(String... args) throws Exception {
31+
// Instantiates a client
32+
Logging logging = LoggingOptions.defaultInstance().service();
33+
34+
// The name of the log to write to
35+
String logName = args[0]; // "my-log";
36+
37+
// The data to write to the log
38+
String text = "Hello, world!";
39+
40+
LogEntry entry = LogEntry.newBuilder(StringPayload.of(text))
41+
.setLogName(logName)
42+
.setResource(MonitoredResource.builder("global").build())
43+
.build();
44+
45+
// Writes the log entry
46+
logging.write(Collections.singleton(entry));
47+
48+
System.out.printf("Logged: %s%n", text);
49+
}
50+
}
51+
// [END logging_quickstart]

0 commit comments

Comments
 (0)