-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Stackdriver logging samples (JUL, Logback, Client library) #826
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Getting Started with Stackdriver Logging using `java.util.logging` | ||
|
||
[Stackdriver Logging][logging] allows you to store, search, analyze, monitor, | ||
and alert on log data and events from Google Cloud Platform and Amazon Web | ||
Services. | ||
These sample Java applications demonstrate how to write logs to Stackdriver using | ||
the default Java Logging API (`java.util.logging`) handler for | ||
[Google Cloud Client Library for Java][google-cloud-java]. | ||
|
||
[logging]: https://cloud.google.com/logging/ | ||
[google-cloud-java]: https://github.com/GoogleCloudPlatform/google-cloud-java | ||
|
||
## Setup | ||
|
||
Install [Maven](http://maven.apache.org/). | ||
|
||
Build your project with: | ||
|
||
mvn clean package -DskipTests | ||
|
||
[Setup authentication](https://cloud.google.com/docs/authentication) using a service account. | ||
|
||
## Configuration | ||
|
||
Update [logging.properties](src/main/resources/logging.properties) to configure the handler. | ||
|
||
## Enhancers | ||
|
||
[ExampleEnhancer.java](src/main/java/com/example/logging/jul/enhancers/ExampleEnhancer.java) | ||
provides an example of enhancing log entries with additional labels. | ||
|
||
|
||
## Writing log entries | ||
mvn exec:java -Dexec.mainClass=com.example.logging.jul.Quickstart \ | ||
-Dexec.args="-Djava.util.logging.file=src/main/resources/logging.properties" | ||
|
||
Logs can be viewed using the [Logs Viewer Console](https://pantheon.corp.google.com/logs/viewer). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<!-- | ||
Copyright 2016 Google Inc. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
<project> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.example.logging</groupId> | ||
<artifactId>logging-google-cloud-samples-jul</artifactId> | ||
<packaging>jar</packaging> | ||
|
||
<!-- Parent defines config for testing & linting. --> | ||
<parent> | ||
<groupId>com.google.cloud.logging.samples</groupId> | ||
<artifactId>cloud-logging-samples</artifactId> | ||
<version>1.0.0</version> | ||
<relativePath>..</relativePath> | ||
</parent> | ||
|
||
<properties> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>google-cloud-logging</artifactId> | ||
<version>1.4.0</version> | ||
</dependency> | ||
|
||
<!-- Test dependencies --> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.12</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.truth</groupId> | ||
<artifactId>truth</artifactId> | ||
<version>0.34</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
31 changes: 31 additions & 0 deletions
31
logging/jul/src/main/java/com/example/logging/jul/Quickstart.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
Copyright 2017, Google, Inc. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't think the "," is nesc. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. Fixed missing licenses too. |
||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package com.example.logging.jul; | ||
|
||
// [START jul_quickstart] | ||
|
||
import java.util.logging.Logger; | ||
|
||
public class Quickstart { | ||
private static final Logger logger = Logger.getLogger(Quickstart.class.getName()); | ||
|
||
public static void main(String[] args) { | ||
logger.info("Logging INFO with java.util.logging"); | ||
logger.severe("Logging ERROR with java.util.logging"); | ||
} | ||
} | ||
// [END jul_quickstart] |
31 changes: 31 additions & 0 deletions
31
logging/jul/src/main/java/com/example/logging/jul/enhancers/ExampleEnhancer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
/* | ||
* Copyright 2017 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.logging.jul.enhancers; | ||
|
||
import com.google.cloud.logging.LogEntry; | ||
import com.google.cloud.logging.LoggingEnhancer; | ||
|
||
// Add / update additional fields to the log entry | ||
public class ExampleEnhancer implements LoggingEnhancer { | ||
|
||
@Override | ||
public void enhanceLogEntry(LogEntry.Builder logEntry) { | ||
// add additional labels | ||
logEntry.addLabel("test-label-1", "test-value-1"); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# A default java.util.logging configuration. | ||
# | ||
# [START jul_config] | ||
# To use this configuration, add to system properties : -Djava.util.logging.config.file="/path/to/file" | ||
# | ||
.level = INFO | ||
|
||
# it is recommended that io.grpc and sun.net logging level is kept at INFO level, | ||
# as both these packages are used by Stackdriver internals and can result in verbose / initialization problems. | ||
io.grpc.netty.level=INFO | ||
sun.net.level=INFO | ||
|
||
com.example.logging.jul.Quickstart.handlers=com.google.cloud.logging.LoggingHandler | ||
# default : java.log | ||
com.google.cloud.logging.LoggingHandler.log=custom_log | ||
|
||
# default : INFO | ||
com.google.cloud.logging.LoggingHandler.level=FINE | ||
|
||
# default : ERROR | ||
com.google.cloud.logging.LoggingHandler.flushLevel=ERROR | ||
|
||
# custom formatter | ||
com.google.cloud.logging.LoggingHandler.formatter=java.util.logging.SimpleFormatter | ||
java.util.logging.SimpleFormatter.format=%3$s: %5$s%6$s | ||
|
||
#optional enhancers (to add additional fields, labels) | ||
com.google.cloud.logging.LoggingHandler.enhancers=com.example.logging.jul.enhancers.ExampleEnhancer | ||
# [END jul_config] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Out of curiosity, do we do anything to improve the odds that logs get written even if the app crashes? Can we? Big try / catch in main?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we do offer a logging.flush() but would n't help with the try / catch in main.
I get your point though, so will look into it separately as an issue on
google-cloud-java
.