Skip to content

Commit 258d82b

Browse files
committed
added a new example that shows how search for log group using a pattern
1 parent be8209c commit 258d82b

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

.doc_gen/metadata/cloudwatch-logs_metadata.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,10 @@ cloudwatch-logs_DescribeLogStreams:
119119
github: javav2/example_code/cloudwatch
120120
sdkguide:
121121
excerpts:
122-
- description:
122+
- description: Searches for log streams within a specified log group that match a given prefix.
123+
snippet_tags:
124+
- cloudwatch.javav2.read.log.streams.main
125+
- description: Prints metadata about the most recent log stream in a specified log group.
123126
snippet_tags:
124127
- cloudwatch.javav2.describe.log.streams.main
125128
services:
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package com.example.cloudwatch;
5+
6+
import software.amazon.awssdk.regions.Region;
7+
import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient;
8+
import software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsRequest;
9+
import software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsResponse;
10+
import software.amazon.awssdk.services.cloudwatchlogs.model.FilterLogEventsRequest;
11+
import software.amazon.awssdk.services.cloudwatchlogs.model.FilterLogEventsResponse;
12+
import software.amazon.awssdk.services.cloudwatchlogs.model.FilteredLogEvent;
13+
import software.amazon.awssdk.services.cloudwatchlogs.model.LogStream;
14+
import java.util.List;
15+
16+
17+
// snippet-start:[cloudwatch.javav2.read.log.streams.main]
18+
/**
19+
* Before running this Java V2 code example, set up your development
20+
* environment, including your credentials.
21+
* <p>
22+
* For more information, see the following documentation topic:
23+
* <p>
24+
* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
25+
*/
26+
public class CloudWatchLogsSearch {
27+
28+
private static final String LOG_GROUP_NAME = "WeathertopJavaContainerLogs";
29+
private static final String LOG_STREAM_PREFIX = "weathertop-java-stream";
30+
private static final String PATTERN = "INFO";
31+
private static final Region REGION = Region.US_EAST_1;
32+
33+
public static void main(String[] args) {
34+
CloudWatchLogsClient cwlClient = CloudWatchLogsClient.builder()
35+
.region(REGION)
36+
.build();
37+
38+
searchLogStreamsAndFilterEvents(cwlClient, LOG_GROUP_NAME, LOG_STREAM_PREFIX, PATTERN);
39+
}
40+
41+
/**
42+
* Searches for log streams with a specific prefix within a log group and filters log events based on a specified pattern.
43+
*
44+
* @param cwlClient the CloudWatchLogsClient used to interact with AWS CloudWatch Logs
45+
* @param logGroupName the name of the log group to search within
46+
* @param logStreamPrefix the prefix of the log streams to search for
47+
* @param pattern the pattern to filter log events by
48+
*/
49+
private static void searchLogStreamsAndFilterEvents(CloudWatchLogsClient cwlClient, String logGroupName, String logStreamPrefix, String pattern) {
50+
DescribeLogStreamsRequest describeLogStreamsRequest = DescribeLogStreamsRequest.builder()
51+
.logGroupName(logGroupName)
52+
.logStreamNamePrefix(logStreamPrefix)
53+
.build();
54+
55+
DescribeLogStreamsResponse describeLogStreamsResponse = cwlClient.describeLogStreams(describeLogStreamsRequest);
56+
57+
List<LogStream> logStreams = describeLogStreamsResponse.logStreams();
58+
59+
for (LogStream logStream : logStreams) {
60+
String logStreamName = logStream.logStreamName();
61+
System.out.println("Searching in log stream: " + logStreamName);
62+
63+
FilterLogEventsRequest filterLogEventsRequest = FilterLogEventsRequest.builder()
64+
.logGroupName(logGroupName)
65+
.logStreamNames(logStreamName)
66+
.filterPattern(pattern)
67+
.build();
68+
69+
FilterLogEventsResponse filterLogEventsResponse = cwlClient.filterLogEvents(filterLogEventsRequest);
70+
71+
for (FilteredLogEvent event : filterLogEventsResponse.events()) {
72+
System.out.println(event.message());
73+
}
74+
75+
System.out.println("--------------------------------------------------"); // Separator for better readability
76+
}
77+
}
78+
}
79+
// snippet-end:[cloudwatch.javav2.read.log.streams.main]

0 commit comments

Comments
 (0)