Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .doc_gen/metadata/cloudwatch-logs_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ cloudwatch-logs_DescribeLogStreams:
github: javav2/example_code/cloudwatch
sdkguide:
excerpts:
- description:
- description: Searches for log streams within a specified log group that match a given prefix.
snippet_tags:
- cloudwatch.javav2.read.log.streams.main
- description: Prints metadata about the most recent log stream in a specified log group.
snippet_tags:
- cloudwatch.javav2.describe.log.streams.main
services:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package com.example.cloudwatch;

import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient;
import software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsRequest;
import software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsResponse;
import software.amazon.awssdk.services.cloudwatchlogs.model.FilterLogEventsRequest;
import software.amazon.awssdk.services.cloudwatchlogs.model.FilterLogEventsResponse;
import software.amazon.awssdk.services.cloudwatchlogs.model.FilteredLogEvent;
import software.amazon.awssdk.services.cloudwatchlogs.model.LogStream;
import java.util.List;


// snippet-start:[cloudwatch.javav2.read.log.streams.main]
/**
* 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
*/
private 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
}
}
}
// snippet-end:[cloudwatch.javav2.read.log.streams.main]
Loading