Skip to content
Merged
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
25 changes: 25 additions & 0 deletions .doc_gen/metadata/cloudwatch-logs_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ cloudwatch-logs_AssociateKmsKey:
- CloudWatchLogs.dotnetv3.AssociateKmsKeyExample
services:
cloudwatch-logs: {AssociateKmsKey}
cloudwatch-logs_GetLogEvents:
languages:
Java:
versions:
- sdk_version: 2
github: javav2/example_code/cloudwatch
excerpts:
- description:
snippet_tags:
- cloudwatch.java2.get_logs.main
services:
cloudwatch-logs: {GetLogEvents}
cloudwatch-logs_CancelExportTask:
languages:
.NET:
Expand Down Expand Up @@ -99,6 +111,19 @@ cloudwatch-logs_DescribeExportTasks:
- CloudWatchLogs.dotnetv3.DescribeExportTasksExammple
services:
cloudwatch-logs: {DescribeExportTasks}
cloudwatch-logs_DescribeLogStreams:
languages:
Java:
versions:
- sdk_version: 2
github: javav2/example_code/cloudwatch
sdkguide:
excerpts:
- description:
snippet_tags:
- cloudwatch.javav2.describe.log.streams.main
services:
cloudwatch-logs: {DescribeLogStreams}
cloudwatch-logs_DescribeLogGroups:
languages:
.NET:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// 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.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.
* <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 CloudWatchLogQuery {
public static void main(final String[] args) {
final String usage = """
Usage:
<logGroupName>

Where:
logGroupName - The name of the log group (for example, /aws/lambda/ChatAIHandler).
""";

if (args.length != 1) {
System.out.print(usage);
System.exit(1);
}

String logGroupName = "/aws/lambda/ChatAIHandler" ; //args[0];
CloudWatchLogsClient logsClient = CloudWatchLogsClient.builder()
.region(Region.US_EAST_1)
.build();

describeMostRecentLogStream(logsClient, logGroupName);
}

/**
* Describes and prints metadata about the most recent log stream in the specified log group.
*
* @param logsClient the CloudWatchLogsClient used to interact with AWS CloudWatch Logs
* @param logGroupName the name of the log group
*/
public static void describeMostRecentLogStream(CloudWatchLogsClient logsClient, String logGroupName) {
DescribeLogStreamsRequest streamsRequest = DescribeLogStreamsRequest.builder()
.logGroupName(logGroupName)
.orderBy(OrderBy.LAST_EVENT_TIME)
.descending(true)
.limit(1)
.build();

try {
DescribeLogStreamsResponse streamsResponse = logsClient.describeLogStreams(streamsRequest);
List<LogStream> logStreams = streamsResponse.logStreams();

if (logStreams.isEmpty()) {
System.out.println("No log streams found for log group: " + logGroupName);
return;
}

LogStream stream = logStreams.get(0);
System.out.println("Most Recent Log Stream:");
System.out.println(" Name: " + stream.logStreamName());
System.out.println(" ARN: " + stream.arn());
System.out.println(" Creation Time: " + stream.creationTime());
System.out.println(" First Event Time: " + stream.firstEventTimestamp());
System.out.println(" Last Event Time: " + stream.lastEventTimestamp());
System.out.println(" Stored Bytes: " + stream.storedBytes());
System.out.println(" Upload Sequence Token: " + stream.uploadSequenceToken());

} catch (CloudWatchLogsException e) {
System.err.println("Failed to describe log stream: " + e.awsErrorDetails().errorMessage());
}
}
}
// snippet-end:[cloudwatch.javav2.describe.log.streams.main]
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,23 @@ public static void main(String[] args) {
final String usage = """

Usage:
<logStreamName> <logGroupName>
<logGroupName> <logStreamName>

Where:
logStreamName - The name of the log stream (for example, mystream).
logGroupName - The name of the log group (for example, myloggroup).
logStreamName - The name of the log stream (for example, mystream).

""";

if (args.length != 2) {
System.out.print(usage);
System.exit(1);

}

String logStreamName = args[0];
String logGroupName = args[1];
String logGroupName = args[0];
String logStreamName = args[1];

Region region = Region.US_WEST_2;
CloudWatchLogsClient cloudWatchLogsClient = CloudWatchLogsClient.builder()
.region(region)
Expand Down
Loading