AWS Security ChangesHomeSearch

AWS code-library documentation change

Service: code-library · 2025-10-16 · Documentation low

File: code-library/latest/ug/cloudwatch-logs_example_cloudwatch-logs_DescribeLogStreams_section.md

Summary

Added Java code example demonstrating log stream search with prefix filtering and pattern-based event filtering

Security assessment

The change adds a code example for log analysis but doesn't address vulnerabilities or security features. While log analysis can be part of security monitoring, the example itself doesn't mention security controls, IAM permissions, or specific security use cases. The code demonstrates normal API usage without security hardening guidance.

Diff

diff --git a/code-library/latest/ug/cloudwatch-logs_example_cloudwatch-logs_DescribeLogStreams_section.md b/code-library/latest/ug/cloudwatch-logs_example_cloudwatch-logs_DescribeLogStreams_section.md
index 3f82e9667..090edf2f0 100644
--- a//code-library/latest/ug/cloudwatch-logs_example_cloudwatch-logs_DescribeLogStreams_section.md
+++ b//code-library/latest/ug/cloudwatch-logs_example_cloudwatch-logs_DescribeLogStreams_section.md
@@ -56 +56,85 @@ Java
-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). 
+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-logs#code-examples). 
+
+Searches for log streams within a specified log group that match a given prefix.
+    
+    
+    /**
+     * 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 CloudWatchLogsSearch {
+    
+        public static void main(String[] args) {
+            final String usage = """
+    
+                    Usage:
+                      <logGroupName> <logStreamName> 
+    
+                    Where:
+                      logGroupName - The name of the log group (for example, WeathertopJavaContainerLogs).
+                      logStreamName - The name of the log stream (for example, weathertop-java-stream).
+                      pattern - the pattern to use (for example, INFO) 
+                      
+                    """;
+    
+            if (args.length != 3) {
+                System.out.print(usage);
+                System.exit(1);
+            }
+    
+            String logGroupName = args[0] ;
+            String logStreamName = args[1] ;
+            String pattern = args[2] ;
+    
+            CloudWatchLogsClient cwlClient = CloudWatchLogsClient.builder()
+                    .region(Region.US_EAST_1)
+                    .build();
+    
+            searchLogStreamsAndFilterEvents(cwlClient, logGroupName, logStreamName, pattern);
+        }
+    
+        /**
+         * Searches for log streams with a specific prefix within a log group and filters log events based on a specified pattern.
+         *
+         * @param cwlClient       the CloudWatchLogsClient used to interact with AWS CloudWatch Logs
+         * @param logGroupName    the name of the log group to search within
+         * @param logStreamPrefix the prefix of the log streams to search for
+         * @param pattern         the pattern to filter log events by
+         */
+        public static void searchLogStreamsAndFilterEvents(CloudWatchLogsClient cwlClient, String logGroupName, String logStreamPrefix, String pattern) {
+            DescribeLogStreamsRequest describeLogStreamsRequest = DescribeLogStreamsRequest.builder()
+                    .logGroupName(logGroupName)
+                    .logStreamNamePrefix(logStreamPrefix)
+                    .build();
+    
+            DescribeLogStreamsResponse describeLogStreamsResponse = cwlClient.describeLogStreams(describeLogStreamsRequest);
+            List<LogStream> logStreams = describeLogStreamsResponse.logStreams();
+    
+            for (LogStream logStream : logStreams) {
+                String logStreamName = logStream.logStreamName();
+                System.out.println("Searching in log stream: " + logStreamName);
+    
+                FilterLogEventsRequest filterLogEventsRequest = FilterLogEventsRequest.builder()
+                        .logGroupName(logGroupName)
+                        .logStreamNames(logStreamName)
+                        .filterPattern(pattern)
+                        .build();
+    
+                FilterLogEventsResponse filterLogEventsResponse = cwlClient.filterLogEvents(filterLogEventsRequest);
+    
+                for (FilteredLogEvent event : filterLogEventsResponse.events()) {
+                    System.out.println(event.message());
+                }
+    
+                System.out.println("--------------------------------------------------"); // Separator for better readability
+            }
+        }
+    }
+    
+    
+
+Prints metadata about the most recent log stream in a specified log group.