There are more AWS SDK examples available in the AWS Doc SDK Examples
Use GetLogEvents
with an AWS SDK or CLI
The following code examples show how to use GetLogEvents
.
- CLI
-
- AWS CLI
-
The following command retrieves log events from a log stream named
20150601
in the log groupmy-logs
:aws logs get-log-events --log-group-name
my-logs
--log-stream-name20150601
Output:
{ "nextForwardToken": "f/31961209122447488583055879464742346735121166569214640130", "events": [ { "ingestionTime": 1433190494190, "timestamp": 1433190184356, "message": "Example Event 1" }, { "ingestionTime": 1433190516679, "timestamp": 1433190184356, "message": "Example Event 1" }, { "ingestionTime": 1433190494190, "timestamp": 1433190184358, "message": "Example Event 2" } ], "nextBackwardToken": "b/31961209122358285602261756944988674324553373268216709120" }
-
For API details, see GetLogEvents
in AWS CLI Command Reference.
-
- Java
-
- SDK for Java 2.x
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.cloudwatch.model.CloudWatchException; 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.GetLogEventsRequest; import software.amazon.awssdk.services.cloudwatchlogs.model.GetLogEventsResponse; import java.time.Instant; import java.time.temporal.ChronoUnit; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class GetLogEvents { public static void main(String[] args) { final String usage = """ Usage: <logGroupName> <logStreamName> Where: 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 logGroupName = "WeathertopJavaContainerLogs" ; //args[0]; String logStreamName = "weathertop-java-stream" ; //args[1]; Region region = Region.US_EAST_1 ; CloudWatchLogsClient cloudWatchLogsClient = CloudWatchLogsClient.builder() .region(region) .build(); getCWLogEvents(cloudWatchLogsClient, logGroupName, logStreamName); cloudWatchLogsClient.close(); } public static void getCWLogEvents(CloudWatchLogsClient cloudWatchLogsClient, String logGroupName, String logStreamPrefix) { try { // First, find the exact log stream name DescribeLogStreamsRequest describeRequest = DescribeLogStreamsRequest.builder() .logGroupName(logGroupName) .logStreamNamePrefix(logStreamPrefix) .limit(1) // get the first matching stream .build(); DescribeLogStreamsResponse describeResponse = cloudWatchLogsClient.describeLogStreams(describeRequest); if (describeResponse.logStreams().isEmpty()) { System.out.println("No matching log streams found for prefix: " + logStreamPrefix); return; } String exactLogStreamName = describeResponse.logStreams().get(0).logStreamName(); System.out.println("Using exact log stream: " + exactLogStreamName); long startTime = Instant.now().minus(7, ChronoUnit.DAYS).toEpochMilli(); long endTime = Instant.now().toEpochMilli(); GetLogEventsRequest getLogEventsRequest = GetLogEventsRequest.builder() .logGroupName(logGroupName) .logStreamName(exactLogStreamName) // <-- exact name, not prefix .startTime(startTime) .endTime(endTime) .startFromHead(true) .build(); GetLogEventsResponse response = cloudWatchLogsClient.getLogEvents(getLogEventsRequest); if (response.events().isEmpty()) { System.out.println("No log events found in the past 7 days."); } else { response.events().forEach(e -> System.out.println(e.message())); } } catch (CloudWatchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
-
For API details, see GetLogEvents in AWS SDK for Java 2.x API Reference.
-
DescribeSubscriptionFilters
GetQueryResults