Skip to content

Commit 97879b0

Browse files
committed
rolled in review comments
1 parent 2654040 commit 97879b0

File tree

2 files changed

+25
-28
lines changed

2 files changed

+25
-28
lines changed

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

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@
2828
public class CloudWatchLogQuery {
2929
public static void main(final String[] args) {
3030
final String usage = """
31-
3231
Usage:
33-
<logGroupName>
32+
<logGroupName>
3433
3534
Where:
3635
logGroupName - The name of the log group (for example, /aws/lambda/ChatAIHandler).
@@ -41,28 +40,28 @@ public static void main(final String[] args) {
4140
System.exit(1);
4241
}
4342

44-
String logGroupName = args[0] ;
43+
String logGroupName = "/aws/lambda/ChatAIHandler" ; //args[0];
4544
CloudWatchLogsClient logsClient = CloudWatchLogsClient.builder()
4645
.region(Region.US_EAST_1)
4746
.build();
4847

49-
getLogEvents(logsClient, logGroupName);
50-
48+
describeMostRecentLogStream(logsClient, logGroupName);
5149
}
5250

5351
/**
54-
* Retrieves and prints log events from the most recent log stream in the specified log group
52+
* Describes and prints metadata about the most recent log stream in the specified log group.
5553
*
56-
* @param logsClient the CloudWatchLogsClient used to interact with AWS CloudWatch Logs
57-
* @param logGroupName the name of the log group from which to retrieve the log events
54+
* @param logsClient the CloudWatchLogsClient used to interact with AWS CloudWatch Logs
55+
* @param logGroupName the name of the log group
5856
*/
59-
public static void getLogEvents(CloudWatchLogsClient logsClient, String logGroupName) {
57+
public static void describeMostRecentLogStream(CloudWatchLogsClient logsClient, String logGroupName) {
6058
DescribeLogStreamsRequest streamsRequest = DescribeLogStreamsRequest.builder()
6159
.logGroupName(logGroupName)
6260
.orderBy(OrderBy.LAST_EVENT_TIME)
6361
.descending(true)
6462
.limit(1)
6563
.build();
64+
6665
try {
6766
DescribeLogStreamsResponse streamsResponse = logsClient.describeLogStreams(streamsRequest);
6867
List<LogStream> logStreams = streamsResponse.logStreams();
@@ -72,23 +71,18 @@ public static void getLogEvents(CloudWatchLogsClient logsClient, String logGroup
7271
return;
7372
}
7473

75-
String logStreamName = logStreams.get(0).logStreamName();
76-
77-
// Get Log Events.
78-
GetLogEventsRequest eventsRequest = GetLogEventsRequest.builder()
79-
.logGroupName(logGroupName)
80-
.logStreamName(logStreamName)
81-
.startFromHead(true)
82-
.build();
83-
84-
GetLogEventsResponse eventsResponse = logsClient.getLogEvents(eventsRequest);
85-
System.out.println("Log events from: " + logStreamName);
86-
for (OutputLogEvent event : eventsResponse.events()) {
87-
System.out.printf("[%s] %s%n", event.timestamp(), event.message());
88-
}
74+
LogStream stream = logStreams.get(0);
75+
System.out.println("Most Recent Log Stream:");
76+
System.out.println(" Name: " + stream.logStreamName());
77+
System.out.println(" ARN: " + stream.arn());
78+
System.out.println(" Creation Time: " + stream.creationTime());
79+
System.out.println(" First Event Time: " + stream.firstEventTimestamp());
80+
System.out.println(" Last Event Time: " + stream.lastEventTimestamp());
81+
System.out.println(" Stored Bytes: " + stream.storedBytes());
82+
System.out.println(" Upload Sequence Token: " + stream.uploadSequenceToken());
8983

9084
} catch (CloudWatchLogsException e) {
91-
System.err.println("Failed to fetch logs: " + e.awsErrorDetails().errorMessage());
85+
System.err.println("Failed to describe log stream: " + e.awsErrorDetails().errorMessage());
9286
}
9387
}
9488
}

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,23 @@ public static void main(String[] args) {
2626
final String usage = """
2727
2828
Usage:
29-
<logStreamName> <logGroupName>
29+
<logGroupName> <logStreamName>
3030
3131
Where:
32-
logStreamName - The name of the log stream (for example, mystream).
3332
logGroupName - The name of the log group (for example, myloggroup).
33+
logStreamName - The name of the log stream (for example, mystream).
34+
3435
""";
3536

3637
if (args.length != 2) {
3738
System.out.print(usage);
3839
System.exit(1);
40+
3941
}
4042

41-
String logStreamName = args[0];
42-
String logGroupName = args[1];
43+
String logGroupName = args[0];
44+
String logStreamName = args[1];
45+
4346
Region region = Region.US_WEST_2;
4447
CloudWatchLogsClient cloudWatchLogsClient = CloudWatchLogsClient.builder()
4548
.region(region)

0 commit comments

Comments
 (0)