AWS Security ChangesHomeSearch

AWS code-library documentation change

Service: code-library · 2025-07-07 · Documentation low

File: code-library/latest/ug/java_2_cloudwatch-logs_code_examples.md

Summary

Added code example demonstrating usage of DescribeLogStreams API to retrieve metadata about CloudWatch log streams

Security assessment

The change adds a standard API usage example for log stream metadata retrieval. There is no evidence of addressing security vulnerabilities or security-related configuration in the code sample.

Diff

diff --git a/code-library/latest/ug/java_2_cloudwatch-logs_code_examples.md b/code-library/latest/ug/java_2_cloudwatch-logs_code_examples.md
index 6267fb6bb..a15e75ad4 100644
--- a//code-library/latest/ug/java_2_cloudwatch-logs_code_examples.md
+++ b//code-library/latest/ug/java_2_cloudwatch-logs_code_examples.md
@@ -101,0 +102,87 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+The following code example shows how to use `DescribeLogStreams`.
+
+**SDK for Java 2.x**
+    
+
+###### Note
+
+There's more on GitHub. Find the complete example and learn how to set up and run in the [AWS Code Examples Repository](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javav2/example_code/cloudwatch#code-examples). 
+    
+    
+    /**
+     * Before running this Java V2 code example, set up your development
+     * environment, including your credentials.
+     * <p>
+     * For more information, see the following documentation topic:
+     * <p>
+     * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
+     */
+    public class CloudWatchLogQuery {
+        public static void main(final String[] args) {
+            final String usage = """
+                    Usage:
+                      <logGroupName>
+    
+                    Where:
+                      logGroupName - The name of the log group (for example, /aws/lambda/ChatAIHandler).
+                    """;
+    
+            if (args.length != 1) {
+                System.out.print(usage);
+                System.exit(1);
+            }
+    
+            String logGroupName = "/aws/lambda/ChatAIHandler" ; //args[0];
+            CloudWatchLogsClient logsClient = CloudWatchLogsClient.builder()
+                    .region(Region.US_EAST_1)
+                    .build();
+    
+            describeMostRecentLogStream(logsClient, logGroupName);
+        }
+    
+        /**
+         * Describes and prints metadata about the most recent log stream in the specified log group.
+         *
+         * @param logsClient   the CloudWatchLogsClient used to interact with AWS CloudWatch Logs
+         * @param logGroupName the name of the log group
+         */
+        public static void describeMostRecentLogStream(CloudWatchLogsClient logsClient, String logGroupName) {
+            DescribeLogStreamsRequest streamsRequest = DescribeLogStreamsRequest.builder()
+                    .logGroupName(logGroupName)
+                    .orderBy(OrderBy.LAST_EVENT_TIME)
+                    .descending(true)
+                    .limit(1)
+                    .build();
+    
+            try {
+                DescribeLogStreamsResponse streamsResponse = logsClient.describeLogStreams(streamsRequest);
+                List<LogStream> logStreams = streamsResponse.logStreams();
+    
+                if (logStreams.isEmpty()) {
+                    System.out.println("No log streams found for log group: " + logGroupName);
+                    return;
+                }
+    
+                LogStream stream = logStreams.get(0);
+                System.out.println("Most Recent Log Stream:");
+                System.out.println("  Name: " + stream.logStreamName());
+                System.out.println("  ARN: " + stream.arn());
+                System.out.println("  Creation Time: " + stream.creationTime());
+                System.out.println("  First Event Time: " + stream.firstEventTimestamp());
+                System.out.println("  Last Event Time: " + stream.lastEventTimestamp());
+                System.out.println("  Stored Bytes: " + stream.storedBytes());
+                System.out.println("  Upload Sequence Token: " + stream.uploadSequenceToken());
+    
+            } catch (CloudWatchLogsException e) {
+                System.err.println("Failed to describe log stream: " + e.awsErrorDetails().errorMessage());
+            }
+        }
+    }
+    
+    
+
+  * For API details, see [DescribeLogStreams](https://docs.aws.amazon.com/goto/SdkForJavaV2/logs-2014-03-28/DescribeLogStreams) in _AWS SDK for Java 2.x API Reference_. 
+
+
+
+
@@ -198,0 +286,86 @@ There's more on GitHub. Find the complete example and learn how to set up and ru
+
+
+
+
+The following code example shows how to use `GetLogEvents`.
+
+**SDK for Java 2.x**
+    
+
+###### Note
+
+There's more on GitHub. Find the complete example and learn how to set up and run in the [AWS Code Examples Repository](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javav2/example_code/cloudwatch#code-examples). 
+    
+    
+    import software.amazon.awssdk.regions.Region;
+    import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException;
+    import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient;
+    import software.amazon.awssdk.services.cloudwatchlogs.model.GetLogEventsRequest;
+    
+    /**
+     * Before running this Java V2 code example, set up your development
+     * environment, including your credentials.
+     *
+     * For more information, see the following documentation topic:
+     *
+     * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
+     */
+    public class GetLogEvents {
+    
+        public static void main(String[] args) {
+    
+            final String usage = """
+    
+                    Usage:
+                      <logGroupName> <logStreamName> 
+    
+                    Where:
+                      logGroupName - The name of the log group (for example, myloggroup).
+                      logStreamName - The name of the log stream (for example, mystream).
+                      
+                    """;
+    
+            if (args.length != 2) {
+                System.out.print(usage);
+                System.exit(1);
+    
+            }
+    
+            String logGroupName = args[0];
+            String logStreamName = args[1];
+    
+            Region region = Region.US_WEST_2;
+            CloudWatchLogsClient cloudWatchLogsClient = CloudWatchLogsClient.builder()
+                    .region(region)
+                    .build();
+    
+            getCWLogEvents(cloudWatchLogsClient, logGroupName, logStreamName);
+            cloudWatchLogsClient.close();
+        }
+    
+        public static void getCWLogEvents(CloudWatchLogsClient cloudWatchLogsClient, String logGroupName,
+                String logStreamName) {
+            try {
+                GetLogEventsRequest getLogEventsRequest = GetLogEventsRequest.builder()
+                        .logGroupName(logGroupName)
+                        .logStreamName(logStreamName)
+                        .startFromHead(true)
+                        .build();
+    
+                int logLimit = cloudWatchLogsClient.getLogEvents(getLogEventsRequest).events().size();
+                for (int c = 0; c < logLimit; c++) {
+                    System.out.println(cloudWatchLogsClient.getLogEvents(getLogEventsRequest).events().get(c).message());
+                }
+    
+                System.out.println("Successfully got CloudWatch log events!");
+    
+            } catch (CloudWatchException e) {
+                System.err.println(e.awsErrorDetails().errorMessage());
+                System.exit(1);
+            }
+        }
+    }
+    
+    
+
+  * For API details, see [GetLogEvents](https://docs.aws.amazon.com/goto/SdkForJavaV2/logs-2014-03-28/GetLogEvents) in _AWS SDK for Java 2.x API Reference_.