Skip to content

Commit 485739e

Browse files
authored
Java Added CloudWatch Logs examples in own project with its readme (#7623)
* added cloudlog examples in own project and own readme. Modified tests and YAML and updated readmes
1 parent e885d6e commit 485739e

File tree

15 files changed

+985
-502
lines changed

15 files changed

+985
-502
lines changed

.doc_gen/metadata/cloudwatch-logs_metadata.yaml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,13 @@ cloudwatch-logs_DescribeLogStreams:
116116
Java:
117117
versions:
118118
- sdk_version: 2
119-
github: javav2/example_code/cloudwatch
119+
github: javav2/example_code/cloudwatch-logs
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:
@@ -158,7 +161,7 @@ cloudwatch-logs_DescribeSubscriptionFilters:
158161
Java:
159162
versions:
160163
- sdk_version: 2
161-
github: javav2/example_code/cloudwatch
164+
github: javav2/example_code/cloudwatch-logs
162165
sdkguide:
163166
excerpts:
164167
- description:
@@ -208,7 +211,7 @@ cloudwatch-logs_DeleteSubscriptionFilter:
208211
Java:
209212
versions:
210213
- sdk_version: 2
211-
github: javav2/example_code/cloudwatch
214+
github: javav2/example_code/cloudwatch-logs
212215
sdkguide:
213216
excerpts:
214217
- description:
@@ -248,7 +251,7 @@ cloudwatch-logs_PutSubscriptionFilter:
248251
Java:
249252
versions:
250253
- sdk_version: 2
251-
github: javav2/example_code/cloudwatch
254+
github: javav2/example_code/cloudwatch-logs
252255
sdkguide:
253256
excerpts:
254257
- description:
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
!**/src/main/**/target/
4+
!**/src/test/**/target/
5+
6+
### IntelliJ IDEA ###
7+
.idea/modules.xml
8+
.idea/jarRepositories.xml
9+
.idea/compiler.xml
10+
.idea/libraries/
11+
*.iws
12+
*.iml
13+
*.ipr
14+
15+
### Eclipse ###
16+
.apt_generated
17+
.classpath
18+
.factorypath
19+
.project
20+
.settings
21+
.springBeans
22+
.sts4-cache
23+
24+
### NetBeans ###
25+
/nbproject/private/
26+
/nbbuild/
27+
/dist/
28+
/nbdist/
29+
/.nb-gradle/
30+
build/
31+
!**/src/main/**/build/
32+
!**/src/test/**/build/
33+
34+
### VS Code ###
35+
.vscode/
36+
37+
### Mac OS ###
38+
.DS_Store
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# CloudWatch Logs code examples for the SDK for Java 2.x
2+
3+
## Overview
4+
5+
Shows how to use the AWS SDK for Java 2.x to work with Amazon CloudWatch Logs.
6+
7+
<!--custom.overview.start-->
8+
<!--custom.overview.end-->
9+
10+
_CloudWatch Logs monitor, store, and access your log files from Amazon Elastic Compute Cloud instances, AWS CloudTrail, or other sources._
11+
12+
## ⚠ Important
13+
14+
* Running this code might result in charges to your AWS account. For more details, see [AWS Pricing](https://aws.amazon.com/pricing/) and [Free Tier](https://aws.amazon.com/free/).
15+
* Running the tests might result in charges to your AWS account.
16+
* We recommend that you grant your code least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege).
17+
* This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services).
18+
19+
<!--custom.important.start-->
20+
<!--custom.important.end-->
21+
22+
## Code examples
23+
24+
### Prerequisites
25+
26+
For prerequisites, see the [README](../../README.md#Prerequisites) in the `javav2` folder.
27+
28+
29+
<!--custom.prerequisites.start-->
30+
<!--custom.prerequisites.end-->
31+
32+
### Single actions
33+
34+
Code excerpts that show you how to call individual service functions.
35+
36+
- [DeleteSubscriptionFilter](src/main/java/com/example/logs/DeleteSubscriptionFilter.java#L6)
37+
- [DescribeLogStreams](src/main/java/com/example/logs/CloudWatchLogsSearch.java#L18)
38+
- [DescribeSubscriptionFilters](src/main/java/com/example/logs/DescribeSubscriptionFilters.java#L6)
39+
- [GetLogEvents](src/main/java/com/example/logs/GetLogEvents.java#L6)
40+
- [PutSubscriptionFilter](src/main/java/com/example/logs/PutSubscriptionFilter.java#L6)
41+
42+
43+
<!--custom.examples.start-->
44+
<!--custom.examples.end-->
45+
46+
## Run the examples
47+
48+
### Instructions
49+
50+
51+
<!--custom.instructions.start-->
52+
<!--custom.instructions.end-->
53+
54+
55+
56+
### Tests
57+
58+
⚠ Running tests might result in charges to your AWS account.
59+
60+
61+
To find instructions for running these tests, see the [README](../../README.md#Tests)
62+
in the `javav2` folder.
63+
64+
65+
66+
<!--custom.tests.start-->
67+
<!--custom.tests.end-->
68+
69+
## Additional resources
70+
71+
- [CloudWatch Logs User Guide](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/WhatIsCloudWatchLogs.html)
72+
- [CloudWatch Logs API Reference](https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/Welcome.html)
73+
- [SDK for Java 2.x CloudWatch Logs reference](https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/cloudwatch-logs/package-summary.html)
74+
75+
<!--custom.resources.start-->
76+
<!--custom.resources.end-->
77+
78+
---
79+
80+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
81+
82+
SPDX-License-Identifier: Apache-2.0
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.example</groupId>
8+
<artifactId>cloudwatch_logs</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13+
<java.version>21</java.version>
14+
<maven.compiler.target>21</maven.compiler.target>
15+
<maven.compiler.source>21</maven.compiler.source>
16+
</properties>
17+
<build>
18+
<plugins>
19+
<plugin>
20+
<groupId>org.apache.maven.plugins</groupId>
21+
<artifactId>maven-surefire-plugin</artifactId>
22+
<version>3.5.2</version>
23+
</plugin>
24+
</plugins>
25+
</build>
26+
<dependencyManagement>
27+
<dependencies>
28+
<dependency>
29+
<groupId>software.amazon.awssdk</groupId>
30+
<artifactId>bom</artifactId>
31+
<version>2.31.8</version>
32+
<type>pom</type>
33+
<scope>import</scope>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.apache.logging.log4j</groupId>
37+
<artifactId>log4j-bom</artifactId>
38+
<version>2.23.1</version>
39+
<type>pom</type>
40+
<scope>import</scope>
41+
</dependency>
42+
</dependencies>
43+
</dependencyManagement>
44+
<dependencies>
45+
<dependency>
46+
<groupId>org.junit.jupiter</groupId>
47+
<artifactId>junit-jupiter</artifactId>
48+
<version>5.11.4</version>
49+
<scope>test</scope>
50+
</dependency>
51+
<dependency>
52+
<groupId>com.fasterxml.jackson.core</groupId>
53+
<artifactId>jackson-core</artifactId>
54+
<version>2.14.2</version>
55+
</dependency>
56+
<dependency>
57+
<groupId>software.amazon.awssdk</groupId>
58+
<artifactId>secretsmanager</artifactId>
59+
</dependency>
60+
<dependency>
61+
<groupId>com.google.code.gson</groupId>
62+
<artifactId>gson</artifactId>
63+
<version>2.10.1</version>
64+
</dependency>
65+
<dependency>
66+
<groupId>com.fasterxml.jackson.core</groupId>
67+
<artifactId>jackson-databind</artifactId>
68+
<version>2.14.2</version>
69+
</dependency>
70+
<dependency>
71+
<groupId>software.amazon.awssdk</groupId>
72+
<artifactId>cloudwatch</artifactId>
73+
</dependency>
74+
<dependency>
75+
<groupId>software.amazon.awssdk</groupId>
76+
<artifactId>cloudwatchlogs</artifactId>
77+
</dependency>
78+
<dependency>
79+
<groupId>software.amazon.awssdk</groupId>
80+
<artifactId>sso</artifactId>
81+
</dependency>
82+
<dependency>
83+
<groupId>software.amazon.awssdk</groupId>
84+
<artifactId>ssooidc</artifactId>
85+
</dependency>
86+
<dependency>
87+
<groupId>org.apache.logging.log4j</groupId>
88+
<artifactId>log4j-core</artifactId>
89+
</dependency>
90+
<dependency>
91+
<groupId>software.amazon.awssdk</groupId>
92+
<artifactId>netty-nio-client</artifactId>
93+
</dependency>
94+
<dependency>
95+
<groupId>org.slf4j</groupId>
96+
<artifactId>slf4j-api</artifactId>
97+
<version>2.0.13</version>
98+
</dependency>
99+
<dependency>
100+
<groupId>org.apache.logging.log4j</groupId>
101+
<artifactId>log4j-slf4j2-impl</artifactId>
102+
</dependency>
103+
<dependency>
104+
<groupId>org.apache.logging.log4j</groupId>
105+
<artifactId>log4j-1.2-api</artifactId>
106+
</dependency>
107+
</dependencies>
108+
</project>
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,20 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44

5-
package com.example.cloudwatch;
5+
package com.example.logs;
66

77
import software.amazon.awssdk.regions.Region;
88
import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient;
99
import software.amazon.awssdk.services.cloudwatchlogs.model.CloudWatchLogsException;
1010
import software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsRequest;
1111
import software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsResponse;
12-
import software.amazon.awssdk.services.cloudwatchlogs.model.GetLogEventsRequest;
13-
import software.amazon.awssdk.services.cloudwatchlogs.model.GetLogEventsResponse;
1412
import software.amazon.awssdk.services.cloudwatchlogs.model.LogStream;
1513
import software.amazon.awssdk.services.cloudwatchlogs.model.OrderBy;
16-
import software.amazon.awssdk.services.cloudwatchlogs.model.OutputLogEvent;
14+
1715
import java.util.List;
1816

1917
// snippet-start:[cloudwatch.javav2.describe.log.streams.main]
18+
2019
/**
2120
* Before running this Java V2 code example, set up your development
2221
* environment, including your credentials.
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package com.example.logs;
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+
15+
import java.util.List;
16+
17+
18+
// snippet-start:[cloudwatch.javav2.read.log.streams.main]
19+
20+
/**
21+
* Before running this Java V2 code example, set up your development
22+
* environment, including your credentials.
23+
* <p>
24+
* For more information, see the following documentation topic:
25+
* <p>
26+
* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
27+
*/
28+
public class CloudWatchLogsSearch {
29+
30+
public static void main(String[] args) {
31+
final String usage = """
32+
33+
Usage:
34+
<logGroupName> <logStreamName>
35+
36+
Where:
37+
logGroupName - The name of the log group (for example, WeathertopJavaContainerLogs).
38+
logStreamName - The name of the log stream (for example, weathertop-java-stream).
39+
pattern - the pattern to use (for example, INFO)
40+
41+
""";
42+
43+
if (args.length != 3) {
44+
System.out.print(usage);
45+
System.exit(1);
46+
}
47+
48+
String logGroupName = args[0] ;
49+
String logStreamName = args[1] ;
50+
String pattern = args[2] ;
51+
52+
CloudWatchLogsClient cwlClient = CloudWatchLogsClient.builder()
53+
.region(Region.US_EAST_1)
54+
.build();
55+
56+
searchLogStreamsAndFilterEvents(cwlClient, logGroupName, logStreamName, pattern);
57+
}
58+
59+
/**
60+
* Searches for log streams with a specific prefix within a log group and filters log events based on a specified pattern.
61+
*
62+
* @param cwlClient the CloudWatchLogsClient used to interact with AWS CloudWatch Logs
63+
* @param logGroupName the name of the log group to search within
64+
* @param logStreamPrefix the prefix of the log streams to search for
65+
* @param pattern the pattern to filter log events by
66+
*/
67+
public static void searchLogStreamsAndFilterEvents(CloudWatchLogsClient cwlClient, String logGroupName, String logStreamPrefix, String pattern) {
68+
DescribeLogStreamsRequest describeLogStreamsRequest = DescribeLogStreamsRequest.builder()
69+
.logGroupName(logGroupName)
70+
.logStreamNamePrefix(logStreamPrefix)
71+
.build();
72+
73+
DescribeLogStreamsResponse describeLogStreamsResponse = cwlClient.describeLogStreams(describeLogStreamsRequest);
74+
List<LogStream> logStreams = describeLogStreamsResponse.logStreams();
75+
76+
for (LogStream logStream : logStreams) {
77+
String logStreamName = logStream.logStreamName();
78+
System.out.println("Searching in log stream: " + logStreamName);
79+
80+
FilterLogEventsRequest filterLogEventsRequest = FilterLogEventsRequest.builder()
81+
.logGroupName(logGroupName)
82+
.logStreamNames(logStreamName)
83+
.filterPattern(pattern)
84+
.build();
85+
86+
FilterLogEventsResponse filterLogEventsResponse = cwlClient.filterLogEvents(filterLogEventsRequest);
87+
88+
for (FilteredLogEvent event : filterLogEventsResponse.events()) {
89+
System.out.println(event.message());
90+
}
91+
92+
System.out.println("--------------------------------------------------"); // Separator for better readability
93+
}
94+
}
95+
}
96+
// snippet-end:[cloudwatch.javav2.read.log.streams.main]

0 commit comments

Comments
 (0)