Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add a sample snippet for use of the tailLogEntries API
Add sample in Java for use of tailLogEntries API * 🦉 Updates from OwlBot See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
- Loading branch information
1 parent
5a7845a
commit 8115888
Showing
9 changed files
with
132 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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
55 changes: 55 additions & 0 deletions
55
samples/snippets/src/main/java/com/example/logging/TailLogEntries.java
This file contains 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,55 @@ | ||
/* | ||
* Copyright 2021 Google LLC | ||
* | ||
* 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; | ||
|
||
// [START logging_write_log_entry] | ||
import com.google.cloud.logging.LogEntry; | ||
import com.google.cloud.logging.LogEntryServerStream; | ||
import com.google.cloud.logging.Logging; | ||
import com.google.cloud.logging.Logging.TailOption; | ||
import com.google.cloud.logging.LoggingOptions; | ||
|
||
public class TailLogEntries { | ||
|
||
public static void main(String[] args) throws Exception { | ||
// TODO(developer): Optionally provide the logname as an argument. | ||
String logName = args.length > 0 ? args[0] : ""; | ||
|
||
LoggingOptions options = LoggingOptions.getDefaultInstance(); | ||
try (Logging logging = options.getService()) { | ||
|
||
// Optionally compose a filter to tail log entries only from specific log | ||
LogEntryServerStream stream; | ||
|
||
if (logName != "") { | ||
stream = | ||
logging.tailLogEntries( | ||
TailOption.filter( | ||
"logName=projects/" + options.getProjectId() + "/logs/" + logName)); | ||
} else { | ||
stream = logging.tailLogEntries(); | ||
} | ||
System.out.println("start streaming.."); | ||
for (LogEntry log : stream) { | ||
System.out.println(log); | ||
// cancel infinite streaming after receiving first entry | ||
stream.cancel(); | ||
} | ||
} | ||
} | ||
} | ||
// [END logging_write_log_entry] |
This file contains 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