AWS code-library documentation change
Summary
Updated GitHub repository links from 'cloudwatch' to 'cloudwatch-logs' path
Security assessment
This change only corrects repository paths and doesn't contain any security-related content or context.
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 a15e75ad4..4efa0ce7f 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 @@ -37 +37 @@ The following code example shows how to use `DeleteSubscriptionFilter`. -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). @@ -109 +109,85 @@ The following code example shows how to use `DescribeLogStreams`. -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. @@ -196 +280 @@ The following code example shows how to use `DescribeSubscriptionFilters`. -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). @@ -302,0 +387,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru + import software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsRequest; + import software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsResponse; @@ -303,0 +390,4 @@ There's more on GitHub. Find the complete example and learn how to set up and ru + import software.amazon.awssdk.services.cloudwatchlogs.model.GetLogEventsResponse; + + import java.time.Instant; + import java.time.temporal.ChronoUnit; @@ -328,3 +418,4 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - if (args.length != 2) { - System.out.print(usage); - System.exit(1); + // if (args.length != 2) { + // System.out.print(usage); + // System.exit(1); + // } @@ -332,4 +423,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - } - - String logGroupName = args[0]; - String logStreamName = args[1]; + String logGroupName = "WeathertopJavaContainerLogs" ; //args[0]; + String logStreamName = "weathertop-java-stream" ; //args[1]; @@ -337 +426 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - Region region = Region.US_WEST_2; + Region region = Region.US_EAST_1 ; @@ -346,2 +435,3 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - public static void getCWLogEvents(CloudWatchLogsClient cloudWatchLogsClient, String logGroupName, - String logStreamName) { + public static void getCWLogEvents(CloudWatchLogsClient cloudWatchLogsClient, + String logGroupName, + String logStreamPrefix) { @@ -348,0 +439,20 @@ There's more on GitHub. Find the complete example and learn how to set up and ru + // First, find the exact log stream name + DescribeLogStreamsRequest describeRequest = DescribeLogStreamsRequest.builder() + .logGroupName(logGroupName) + .logStreamNamePrefix(logStreamPrefix) + .limit(1) // get the first matching stream + .build(); + + DescribeLogStreamsResponse describeResponse = cloudWatchLogsClient.describeLogStreams(describeRequest); + + if (describeResponse.logStreams().isEmpty()) { + System.out.println("No matching log streams found for prefix: " + logStreamPrefix); + return; + } + + String exactLogStreamName = describeResponse.logStreams().get(0).logStreamName(); + System.out.println("Using exact log stream: " + exactLogStreamName); + + long startTime = Instant.now().minus(7, ChronoUnit.DAYS).toEpochMilli(); + long endTime = Instant.now().toEpochMilli(); + @@ -351 +461,3 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - .logStreamName(logStreamName) + .logStreamName(exactLogStreamName) // <-- exact name, not prefix + .startTime(startTime) + .endTime(endTime) @@ -355,4 +467 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - int logLimit = cloudWatchLogsClient.getLogEvents(getLogEventsRequest).events().size(); - for (int c = 0; c < logLimit; c++) { - System.out.println(cloudWatchLogsClient.getLogEvents(getLogEventsRequest).events().get(c).message()); - } + GetLogEventsResponse response = cloudWatchLogsClient.getLogEvents(getLogEventsRequest); @@ -360 +469,5 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - System.out.println("Successfully got CloudWatch log events!"); + if (response.events().isEmpty()) { + System.out.println("No log events found in the past 7 days."); + } else { + response.events().forEach(e -> System.out.println(e.message())); + } @@ -383 +496 @@ The following code example shows how to use `PutSubscriptionFilter`. -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).