Skip to content
Open
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
13 changes: 8 additions & 5 deletions .doc_gen/metadata/cloudwatch-logs_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,13 @@ cloudwatch-logs_DescribeLogStreams:
Java:
versions:
- sdk_version: 2
github: javav2/example_code/cloudwatch
github: javav2/example_code/cloudwatch-logs
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 Expand Up @@ -158,7 +161,7 @@ cloudwatch-logs_DescribeSubscriptionFilters:
Java:
versions:
- sdk_version: 2
github: javav2/example_code/cloudwatch
github: javav2/example_code/cloudwatch-logs
sdkguide:
excerpts:
- description:
Expand Down Expand Up @@ -208,7 +211,7 @@ cloudwatch-logs_DeleteSubscriptionFilter:
Java:
versions:
- sdk_version: 2
github: javav2/example_code/cloudwatch
github: javav2/example_code/cloudwatch-logs
sdkguide:
excerpts:
- description:
Expand Down Expand Up @@ -248,7 +251,7 @@ cloudwatch-logs_PutSubscriptionFilter:
Java:
versions:
- sdk_version: 2
github: javav2/example_code/cloudwatch
github: javav2/example_code/cloudwatch-logs
sdkguide:
excerpts:
- description:
Expand Down
38 changes: 38 additions & 0 deletions javav2/example_code/cloudwatch-logs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
82 changes: 82 additions & 0 deletions javav2/example_code/cloudwatch-logs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# CloudWatch Logs code examples for the SDK for Java 2.x

## Overview

Shows how to use the AWS SDK for Java 2.x to work with Amazon CloudWatch Logs.

<!--custom.overview.start-->
<!--custom.overview.end-->

_CloudWatch Logs monitor, store, and access your log files from Amazon Elastic Compute Cloud instances, AWS CloudTrail, or other sources._

## ⚠ Important

* 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/).
* Running the tests might result in charges to your AWS account.
* 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).
* 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).

<!--custom.important.start-->
<!--custom.important.end-->

## Code examples

### Prerequisites

For prerequisites, see the [README](../../README.md#Prerequisites) in the `javav2` folder.


<!--custom.prerequisites.start-->
<!--custom.prerequisites.end-->

### Single actions

Code excerpts that show you how to call individual service functions.

- [DeleteSubscriptionFilter](src/main/java/com/example/logs/DeleteSubscriptionFilter.java#L6)
- [DescribeLogStreams](src/main/java/com/example/logs/CloudWatchLogQuery.java#L17)
- [DescribeSubscriptionFilters](src/main/java/com/example/logs/DescribeSubscriptionFilters.java#L6)
- [GetLogEvents](src/main/java/com/example/logs/GetLogEvents.java#L6)
- [PutSubscriptionFilter](src/main/java/com/example/logs/PutSubscriptionFilter.java#L6)


<!--custom.examples.start-->
<!--custom.examples.end-->

## Run the examples

### Instructions


<!--custom.instructions.start-->
<!--custom.instructions.end-->



### Tests

⚠ Running tests might result in charges to your AWS account.


To find instructions for running these tests, see the [README](../../README.md#Tests)
in the `javav2` folder.



<!--custom.tests.start-->
<!--custom.tests.end-->

## Additional resources

- [CloudWatch Logs User Guide](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/WhatIsCloudWatchLogs.html)
- [CloudWatch Logs API Reference](https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/Welcome.html)
- [SDK for Java 2.x CloudWatch Logs reference](https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/cloudwatch-logs/package-summary.html)

<!--custom.resources.start-->
<!--custom.resources.end-->

---

Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
108 changes: 108 additions & 0 deletions javav2/example_code/cloudwatch-logs/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>cloudwatch_logs</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>21</java.version>
<maven.compiler.target>21</maven.compiler.target>
<maven.compiler.source>21</maven.compiler.source>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.2</version>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>2.31.8</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-bom</artifactId>
<version>2.23.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.11.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.14.2</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>secretsmanager</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.14.2</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>cloudwatch</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>cloudwatchlogs</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>sso</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>ssooidc</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>netty-nio-client</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.13</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j2-impl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@
// SPDX-License-Identifier: Apache-2.0


package com.example.cloudwatch;
package com.example.logs;

import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient;
import software.amazon.awssdk.services.cloudwatchlogs.model.CloudWatchLogsException;
import software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsRequest;
import software.amazon.awssdk.services.cloudwatchlogs.model.DescribeLogStreamsResponse;
import software.amazon.awssdk.services.cloudwatchlogs.model.GetLogEventsRequest;
import software.amazon.awssdk.services.cloudwatchlogs.model.GetLogEventsResponse;
import software.amazon.awssdk.services.cloudwatchlogs.model.LogStream;
import software.amazon.awssdk.services.cloudwatchlogs.model.OrderBy;
import software.amazon.awssdk.services.cloudwatchlogs.model.OutputLogEvent;

import java.util.List;

// snippet-start:[cloudwatch.javav2.describe.log.streams.main]

/**
* Before running this Java V2 code example, set up your development
* environment, including your credentials.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package com.example.logs;

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