Skip to content

Commit d74cda7

Browse files
committed
updated examples
1 parent 8b98266 commit d74cda7

File tree

11 files changed

+40
-53
lines changed

11 files changed

+40
-53
lines changed

javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/CloudWatchReadLogs.java

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
package com.example.cloudwatch.logs;
4+
package com.example.cloudwatch;
55

66
import software.amazon.awssdk.regions.Region;
77
import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient;
88
import software.amazon.awssdk.services.cloudwatchlogs.model.CloudWatchLogsException;
99
import software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsRequest;
1010
import software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsResponse;
11+
import software.amazon.awssdk.services.cloudwatchlogs.model.FilterLogEventsRequest;
12+
import software.amazon.awssdk.services.cloudwatchlogs.model.FilterLogEventsResponse;
13+
import software.amazon.awssdk.services.cloudwatchlogs.model.FilteredLogEvent;
1114
import software.amazon.awssdk.services.cloudwatchlogs.model.GetLogEventsRequest;
1215
import software.amazon.awssdk.services.cloudwatchlogs.model.GetLogEventsResponse;
1316
import software.amazon.awssdk.services.cloudwatchlogs.model.LogStream;
@@ -25,67 +28,51 @@
2528
*/
2629
// snippet-start:[cloudwatch.javav2.describe.log group.main]
2730
public class CloudWatchReadLogs {
31+
2832
public static void main(final String[] args) {
2933
final String usage = """
30-
Usage:
31-
<logGroupName>
32-
Where:
33-
logGroupName - The name of the log group (for example, /aws/lambda/ChatAIHandler).
34+
Usage:
35+
<logGroupName>
36+
Where:
37+
logGroupName - The name of the log group (e.g., /aws/lambda/ChatAIHandler)
3438
""";
35-
if (args.length != 3) {
36-
System.out.print(usage);
39+
40+
if (args.length != 1) {
41+
System.out.println(usage);
3742
System.exit(1);
3843
}
3944

4045
String logGroupName = args[0];
41-
CloudWatchLogsClient logsClient = CloudWatchLogsClient.builder()
46+
try (CloudWatchLogsClient logsClient = CloudWatchLogsClient.builder()
4247
.region(Region.US_EAST_1)
43-
.build();
44-
getLogEvents(logsClient, logGroupName, startTime, endTime);
48+
.build()) {
49+
fetchRecentLogs(logsClient, logGroupName);
50+
} catch (CloudWatchLogsException e) {
51+
System.err.println("Error accessing CloudWatch Logs: " + e.awsErrorDetails().errorMessage());
52+
}
4553
}
4654

4755
/**
48-
* Retrieves and prints the log events from the specified log group and the most recent log stream within that group,
49-
* filtered by the provided start and end times.
56+
* Retrieves and prints recent log events from the specified log group across all log streams.
5057
*
5158
* @param logsClient the CloudWatchLogsClient used to interact with AWS CloudWatch Logs
5259
* @param logGroupName the name of the log group from which to retrieve the log events
53-
* @param startTime the start time for the log events (in milliseconds since epoch)
54-
* @param endTime the end time for the log events (in milliseconds since epoch)
5560
*/
56-
public static void getLogEvents(CloudWatchLogsClient logsClient, String logGroupName, long startTime, long endTime) {
57-
DescribeLogStreamsRequest streamsRequest = DescribeLogStreamsRequest.builder()
61+
public static void fetchRecentLogs(CloudWatchLogsClient logsClient, String logGroupName) {
62+
FilterLogEventsRequest request = FilterLogEventsRequest.builder()
5863
.logGroupName(logGroupName)
59-
.orderBy(OrderBy.LAST_EVENT_TIME)
60-
.descending(true)
61-
.limit(1)
64+
.limit(50) // Adjust as needed
6265
.build();
63-
try {
64-
DescribeLogStreamsResponse streamsResponse = logsClient.describeLogStreams(streamsRequest);
65-
List<LogStream> logStreams = streamsResponse.logStreams();
66-
if (logStreams.isEmpty()) {
67-
System.out.println("No log streams found for log group: " + logGroupName);
68-
return;
69-
}
7066

71-
String logStreamName = logStreams.get(0).logStreamName();
72-
GetLogEventsRequest eventsRequest = GetLogEventsRequest.builder()
73-
.logGroupName(logGroupName)
74-
.logStreamName(logStreamName)
75-
.startFromHead(true)
76-
.startTime(startTime)
77-
.endTime(endTime)
78-
.build();
67+
FilterLogEventsResponse response = logsClient.filterLogEvents(request);
68+
if (response.events().isEmpty()) {
69+
System.out.println("No log events found.");
70+
return;
71+
}
7972

80-
GetLogEventsResponse eventsResponse = logsClient.getLogEvents(eventsRequest);
81-
System.out.println("Log events from: " + logStreamName);
82-
for (OutputLogEvent event : eventsResponse.events()) {
83-
if (event.timestamp() >= startTime && event.timestamp() <= endTime) {
84-
System.out.printf("[%s] %s%n", event.timestamp(), event.message());
85-
}
86-
}
87-
} catch (CloudWatchLogsException e) {
88-
System.err.println("Failed to fetch logs: " + e.awsErrorDetails().errorMessage());
73+
System.out.println("Recent log events:");
74+
for (FilteredLogEvent event : response.events()) {
75+
System.out.printf("[%s] %s%n", event.timestamp(), event.message());
8976
}
9077
}
9178
}

javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/DeleteSubscriptionFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
package com.example.cloudwatch.logs;
4+
package com.example.cloudwatch;
55

66
// snippet-start:[cloudwatch.java2.delete_subscription_filter.main]
77
// snippet-start:[cloudwatch.java2.delete_subscription_filter.import]

javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/DescribeRule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
package com.example.cloudwatch.events;
4+
package com.example.cloudwatch;
55

66
// snippet-start:[cloudwatch.javav2.describe_rule.main]
77
// snippet-start:[cloudwatch.javav2.describe_rule.import]

javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/DescribeSubscriptionFilters.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
package com.example.cloudwatch.logs;
4+
package com.example.cloudwatch;
55

66
// snippet-start:[cloudwatch.java2.describe_subscription_filters.main]
77
// snippet-start:[cloudwatch.java2.describe_subscription_filters.import]

javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/FilterLogEvents.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
package com.example.cloudwatch.logs;
4+
package com.example.cloudwatch;
55

66
// snippet-start:[cloudwatch.java2.filter_logs.main]
77
// snippet-start:[cloudwatch.java2.filter_logs.import]

javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/GetLogEvents.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
package com.example.cloudwatch.logs;
4+
package com.example.cloudwatch;
55

66
// snippet-start:[cloudwatch.java2.get_logs.main]
77
// snippet-start:[cloudwatch.java2.get_logs.import]

javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/PutEvents.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
package com.example.cloudwatch.events;
4+
package com.example.cloudwatch;
55

66
// snippet-start:[cloudwatch.java2.put_events.main]
77
// snippet-start:[cloudwatch.java2.put_events.import]

javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/PutLogEvents.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
package com.example.cloudwatch.events;
4+
package com.example.cloudwatch;
55

66
// snippet-start:[cloudwatch.java2.put_log_events.main]
77
// snippet-start:[cloudwatch.java2.put_log_events.import]

javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/PutRule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
package com.example.cloudwatch.events;
4+
package com.example.cloudwatch;
55

66
// snippet-start:[cloudwatch.java2.put_rule.main]
77
// snippet-start:[cloudwatch.java2.put_rule.import]

javav2/example_code/cloudwatch/src/main/java/com/example/cloudwatch/PutSubscriptionFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
package com.example.cloudwatch.logs;
4+
package com.example.cloudwatch;
55

66
// snippet-start:[cloudwatch.java2.put_subscription_filter.main]
77
// snippet-start:[cloudwatch.java2.put_subscription_filter.import]

0 commit comments

Comments
 (0)