AWS AmazonCloudWatch documentation change
Summary
Added Java code example for log stream search and event filtering functionality
Security assessment
The change adds implementation details for log analysis but contains no security-specific content. The code example includes standard credential setup without addressing security vulnerabilities.
Diff
diff --git a/AmazonCloudWatch/latest/logs/example_cloudwatch-logs_DescribeLogStreams_section.md b/AmazonCloudWatch/latest/logs/example_cloudwatch-logs_DescribeLogStreams_section.md index 9978cc041..a952ba431 100644 --- a//AmazonCloudWatch/latest/logs/example_cloudwatch-logs_DescribeLogStreams_section.md +++ b//AmazonCloudWatch/latest/logs/example_cloudwatch-logs_DescribeLogStreams_section.md @@ -54 +54,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.