-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Java V2 Added a new example for CloudWatch logs for tix V1818828107 #7512
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
+121
−4
Merged
Changes from 1 commit
Commits
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
95 changes: 95 additions & 0 deletions
95
javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/CloudWatchLogQuery.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,95 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
|
|
||
| package com.example.cloudwatch; | ||
|
|
||
| import software.amazon.awssdk.regions.Region; | ||
| import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient; | ||
| import software.amazon.awssdk.services.cloudwatchlogs.model.CloudWatchLogsException; | ||
| import software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsRequest; | ||
| import software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsResponse; | ||
| import software.amazon.awssdk.services.cloudwatchlogs.model.GetLogEventsRequest; | ||
| import software.amazon.awssdk.services.cloudwatchlogs.model.GetLogEventsResponse; | ||
| import software.amazon.awssdk.services.cloudwatchlogs.model.LogStream; | ||
| import software.amazon.awssdk.services.cloudwatchlogs.model.OrderBy; | ||
| import software.amazon.awssdk.services.cloudwatchlogs.model.OutputLogEvent; | ||
| import java.util.List; | ||
|
|
||
| // snippet-start:[cloudwatch.javav2.describe.log.streams.main] | ||
| /** | ||
| * Before running this Java V2 code example, set up your development | ||
| * environment, including your credentials. | ||
| * <p> | ||
| * For more information, see the following documentation topic: | ||
| * <p> | ||
| * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html | ||
| */ | ||
| public class CloudWatchLogQuery { | ||
| public static void main(final String[] args) { | ||
| final String usage = """ | ||
| Usage: | ||
| <logGroupName> <startTime> <endTime> | ||
tkhill-AWS marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Where: | ||
| logGroupName - The name of the log group (for example, /aws/lambda/ChatAIHandler). | ||
| """; | ||
|
|
||
| if (args.length != 1) { | ||
| System.out.print(usage); | ||
| System.exit(1); | ||
| } | ||
|
|
||
| String logGroupName = args[0] ; | ||
| CloudWatchLogsClient logsClient = CloudWatchLogsClient.builder() | ||
| .region(Region.US_EAST_1) | ||
| .build(); | ||
|
|
||
| getLogEvents(logsClient, logGroupName); | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Retrieves and prints the log events from the specified log group and the most recent log stream within that group. | ||
tkhill-AWS marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * | ||
| * @param logsClient the CloudWatchLogsClient used to interact with AWS CloudWatch Logs | ||
| * @param logGroupName the name of the log group from which to retrieve the log events | ||
| */ | ||
| public static void getLogEvents(CloudWatchLogsClient logsClient, String logGroupName) { | ||
| DescribeLogStreamsRequest streamsRequest = DescribeLogStreamsRequest.builder() | ||
| .logGroupName(logGroupName) | ||
| .orderBy(OrderBy.LAST_EVENT_TIME) | ||
| .descending(true) | ||
| .limit(1) | ||
| .build(); | ||
| try { | ||
| DescribeLogStreamsResponse streamsResponse = logsClient.describeLogStreams(streamsRequest); | ||
| List<LogStream> logStreams = streamsResponse.logStreams(); | ||
|
|
||
| if (logStreams.isEmpty()) { | ||
| System.out.println("No log streams found for log group: " + logGroupName); | ||
| return; | ||
| } | ||
|
|
||
| String logStreamName = logStreams.get(0).logStreamName(); | ||
|
|
||
| // Get Log Events. | ||
| GetLogEventsRequest eventsRequest = GetLogEventsRequest.builder() | ||
| .logGroupName(logGroupName) | ||
| .logStreamName(logStreamName) | ||
| .startFromHead(true) | ||
| .build(); | ||
|
|
||
| GetLogEventsResponse eventsResponse = logsClient.getLogEvents(eventsRequest); | ||
| System.out.println("Log events from: " + logStreamName); | ||
| for (OutputLogEvent event : eventsResponse.events()) { | ||
| System.out.printf("[%s] %s%n", event.timestamp(), event.message()); | ||
| } | ||
|
|
||
| } catch (CloudWatchLogsException e) { | ||
| System.err.println("Failed to fetch logs: " + e.awsErrorDetails().errorMessage()); | ||
| } | ||
| } | ||
| } | ||
| // snippet-end:[cloudwatch.javav2.describe.log.streams.main] | ||
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.
Uh oh!
There was an error while loading. Please reload this page.