|
1 | 1 | // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | 3 |
|
4 | | -package com.example.cloudwatch.logs; |
| 4 | +package com.example.cloudwatch; |
5 | 5 |
|
6 | 6 | import software.amazon.awssdk.regions.Region; |
7 | 7 | import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient; |
8 | 8 | import software.amazon.awssdk.services.cloudwatchlogs.model.CloudWatchLogsException; |
9 | 9 | import software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsRequest; |
10 | 10 | 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; |
11 | 14 | import software.amazon.awssdk.services.cloudwatchlogs.model.GetLogEventsRequest; |
12 | 15 | import software.amazon.awssdk.services.cloudwatchlogs.model.GetLogEventsResponse; |
13 | 16 | import software.amazon.awssdk.services.cloudwatchlogs.model.LogStream; |
|
25 | 28 | */ |
26 | 29 | // snippet-start:[cloudwatch.javav2.describe.log group.main] |
27 | 30 | public class CloudWatchReadLogs { |
| 31 | + |
28 | 32 | public static void main(final String[] args) { |
29 | 33 | 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) |
34 | 38 | """; |
35 | | - if (args.length != 3) { |
36 | | - System.out.print(usage); |
| 39 | + |
| 40 | + if (args.length != 1) { |
| 41 | + System.out.println(usage); |
37 | 42 | System.exit(1); |
38 | 43 | } |
39 | 44 |
|
40 | 45 | String logGroupName = args[0]; |
41 | | - CloudWatchLogsClient logsClient = CloudWatchLogsClient.builder() |
| 46 | + try (CloudWatchLogsClient logsClient = CloudWatchLogsClient.builder() |
42 | 47 | .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 | + } |
45 | 53 | } |
46 | 54 |
|
47 | 55 | /** |
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. |
50 | 57 | * |
51 | 58 | * @param logsClient the CloudWatchLogsClient used to interact with AWS CloudWatch Logs |
52 | 59 | * @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) |
55 | 60 | */ |
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() |
58 | 63 | .logGroupName(logGroupName) |
59 | | - .orderBy(OrderBy.LAST_EVENT_TIME) |
60 | | - .descending(true) |
61 | | - .limit(1) |
| 64 | + .limit(50) // Adjust as needed |
62 | 65 | .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 | | - } |
70 | 66 |
|
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 | + } |
79 | 72 |
|
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()); |
89 | 76 | } |
90 | 77 | } |
91 | 78 | } |
|
0 commit comments