Skip to content

Commit caee933

Browse files
committed
added cloudlog examples in own project and own readme
1 parent e758b03 commit caee933

File tree

13 files changed

+1063
-0
lines changed

13 files changed

+1063
-0
lines changed
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: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
5+
package com.example.logs;
6+
7+
import software.amazon.awssdk.regions.Region;
8+
import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient;
9+
import software.amazon.awssdk.services.cloudwatchlogs.model.CloudWatchLogsException;
10+
import software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsRequest;
11+
import software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsResponse;
12+
import software.amazon.awssdk.services.cloudwatchlogs.model.LogStream;
13+
import software.amazon.awssdk.services.cloudwatchlogs.model.OrderBy;
14+
15+
import java.util.List;
16+
17+
// snippet-start:[cloudwatch.javav2.describe.log.streams.main]
18+
19+
/**
20+
* Before running this Java V2 code example, set up your development
21+
* environment, including your credentials.
22+
* <p>
23+
* For more information, see the following documentation topic:
24+
* <p>
25+
* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
26+
*/
27+
public class CloudWatchLogQuery {
28+
public static void main(final String[] args) {
29+
final String usage = """
30+
Usage:
31+
<logGroupName>
32+
33+
Where:
34+
logGroupName - The name of the log group (for example, /aws/lambda/ChatAIHandler).
35+
""";
36+
37+
if (args.length != 1) {
38+
System.out.print(usage);
39+
System.exit(1);
40+
}
41+
42+
String logGroupName = "/aws/lambda/ChatAIHandler" ; //args[0];
43+
CloudWatchLogsClient logsClient = CloudWatchLogsClient.builder()
44+
.region(Region.US_EAST_1)
45+
.build();
46+
47+
describeMostRecentLogStream(logsClient, logGroupName);
48+
}
49+
50+
/**
51+
* Describes and prints metadata about the most recent log stream in the specified log group.
52+
*
53+
* @param logsClient the CloudWatchLogsClient used to interact with AWS CloudWatch Logs
54+
* @param logGroupName the name of the log group
55+
*/
56+
public static void describeMostRecentLogStream(CloudWatchLogsClient logsClient, String logGroupName) {
57+
DescribeLogStreamsRequest streamsRequest = DescribeLogStreamsRequest.builder()
58+
.logGroupName(logGroupName)
59+
.orderBy(OrderBy.LAST_EVENT_TIME)
60+
.descending(true)
61+
.limit(1)
62+
.build();
63+
64+
try {
65+
DescribeLogStreamsResponse streamsResponse = logsClient.describeLogStreams(streamsRequest);
66+
List<LogStream> logStreams = streamsResponse.logStreams();
67+
68+
if (logStreams.isEmpty()) {
69+
System.out.println("No log streams found for log group: " + logGroupName);
70+
return;
71+
}
72+
73+
LogStream stream = logStreams.get(0);
74+
System.out.println("Most Recent Log Stream:");
75+
System.out.println(" Name: " + stream.logStreamName());
76+
System.out.println(" ARN: " + stream.arn());
77+
System.out.println(" Creation Time: " + stream.creationTime());
78+
System.out.println(" First Event Time: " + stream.firstEventTimestamp());
79+
System.out.println(" Last Event Time: " + stream.lastEventTimestamp());
80+
System.out.println(" Stored Bytes: " + stream.storedBytes());
81+
System.out.println(" Upload Sequence Token: " + stream.uploadSequenceToken());
82+
83+
} catch (CloudWatchLogsException e) {
84+
System.err.println("Failed to describe log stream: " + e.awsErrorDetails().errorMessage());
85+
}
86+
}
87+
}
88+
// snippet-end:[cloudwatch.javav2.describe.log.streams.main]

0 commit comments

Comments
 (0)