1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+
5
+ package com .example .cloudwatch ;
6
+
7
+ import software .amazon .awssdk .regions .Region ;
8
+ import software .amazon .awssdk .services .cloudwatchlogs .CloudWatchLogsClient ;
9
+ import software .amazon .awssdk .services .cloudwatchlogs .model .CloudWatchLogsException ;
10
+ import software .amazon .awssdk .services .cloudwatchlogs .model .DescribeLogStreamsRequest ;
11
+ import software .amazon .awssdk .services .cloudwatchlogs .model .DescribeLogStreamsResponse ;
12
+ import software .amazon .awssdk .services .cloudwatchlogs .model .GetLogEventsRequest ;
13
+ import software .amazon .awssdk .services .cloudwatchlogs .model .GetLogEventsResponse ;
14
+ import software .amazon .awssdk .services .cloudwatchlogs .model .LogStream ;
15
+ import software .amazon .awssdk .services .cloudwatchlogs .model .OrderBy ;
16
+ import software .amazon .awssdk .services .cloudwatchlogs .model .OutputLogEvent ;
17
+ import java .util .List ;
18
+
19
+ // snippet-start:[cloudwatch.javav2.describe.log.streams.main]
20
+ /**
21
+ * Before running this Java V2 code example, set up your development
22
+ * environment, including your credentials.
23
+ * <p>
24
+ * For more information, see the following documentation topic:
25
+ * <p>
26
+ * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
27
+ */
28
+ public class CloudWatchLogQuery {
29
+ public static void main (final String [] args ) {
30
+ final String usage = """
31
+ Usage:
32
+ <logGroupName>
33
+
34
+ Where:
35
+ logGroupName - The name of the log group (for example, /aws/lambda/ChatAIHandler).
36
+ """ ;
37
+
38
+ if (args .length != 1 ) {
39
+ System .out .print (usage );
40
+ System .exit (1 );
41
+ }
42
+
43
+ String logGroupName = "/aws/lambda/ChatAIHandler" ; //args[0];
44
+ CloudWatchLogsClient logsClient = CloudWatchLogsClient .builder ()
45
+ .region (Region .US_EAST_1 )
46
+ .build ();
47
+
48
+ describeMostRecentLogStream (logsClient , logGroupName );
49
+ }
50
+
51
+ /**
52
+ * Describes and prints metadata about the most recent log stream in the specified log group.
53
+ *
54
+ * @param logsClient the CloudWatchLogsClient used to interact with AWS CloudWatch Logs
55
+ * @param logGroupName the name of the log group
56
+ */
57
+ public static void describeMostRecentLogStream (CloudWatchLogsClient logsClient , String logGroupName ) {
58
+ DescribeLogStreamsRequest streamsRequest = DescribeLogStreamsRequest .builder ()
59
+ .logGroupName (logGroupName )
60
+ .orderBy (OrderBy .LAST_EVENT_TIME )
61
+ .descending (true )
62
+ .limit (1 )
63
+ .build ();
64
+
65
+ try {
66
+ DescribeLogStreamsResponse streamsResponse = logsClient .describeLogStreams (streamsRequest );
67
+ List <LogStream > logStreams = streamsResponse .logStreams ();
68
+
69
+ if (logStreams .isEmpty ()) {
70
+ System .out .println ("No log streams found for log group: " + logGroupName );
71
+ return ;
72
+ }
73
+
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 ());
83
+
84
+ } catch (CloudWatchLogsException e ) {
85
+ System .err .println ("Failed to describe log stream: " + e .awsErrorDetails ().errorMessage ());
86
+ }
87
+ }
88
+ }
89
+ // snippet-end:[cloudwatch.javav2.describe.log.streams.main]
0 commit comments