2828public 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}
0 commit comments