Use GetLogEvents
with an Amazon SDK or CLI
The following code examples show how to use GetLogEvents
.
- CLI
-
- Amazon 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 Amazon 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 Amazon 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.GetLogEventsRequest; /** * 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 = args[0]; String logStreamName = args[1]; Region region = Region.US_WEST_2; CloudWatchLogsClient cloudWatchLogsClient = CloudWatchLogsClient.builder() .region(region) .build(); getCWLogEvents(cloudWatchLogsClient, logGroupName, logStreamName); cloudWatchLogsClient.close(); } public static void getCWLogEvents(CloudWatchLogsClient cloudWatchLogsClient, String logGroupName, String logStreamName) { try { GetLogEventsRequest getLogEventsRequest = GetLogEventsRequest.builder() .logGroupName(logGroupName) .logStreamName(logStreamName) .startFromHead(true) .build(); int logLimit = cloudWatchLogsClient.getLogEvents(getLogEventsRequest).events().size(); for (int c = 0; c < logLimit; c++) { System.out.println(cloudWatchLogsClient.getLogEvents(getLogEventsRequest).events().get(c).message()); } System.out.println("Successfully got CloudWatch log events!"); } catch (CloudWatchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
-
For API details, see GetLogEvents in Amazon SDK for Java 2.x API Reference.
-
For a complete list of Amazon SDK developer guides and code examples, see Using CloudWatch Logs with an Amazon SDK. This topic also includes information about getting started and details about previous SDK versions.