AWS Code Sample
Catalog

The AWS Documentation website is getting a new look!
Try it now and let us know what you think. Switch to the new look >>

You can return to the original look by selecting English in the language selector above.

GetLogEvents.java

GetLogEvents.java demonstrates how to get log events from CloudWatch in a specified region.

/* * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. * A copy of the License is located at * * http://aws.amazon.com/apache2.0 * * or in the "license" file accompanying this file. This file is distributed * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing * permissions and limitations under the License. */ package com.example.cloudwatch; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient; import software.amazon.awssdk.services.cloudwatchlogs.model.GetLogEventsRequest; /** * Gets logs events from CloudWatch */ public class GetLogEvents { public static void main(String[] args) { final String usage = "To run this example, supply a regionName (e.g. us-east-1), logGroupName, and streamName as command line arguments\n" + "Ex: GetLogEvents <regionName> <logGroupName> <streamName>\n"; if (args.length != 3) { System.out.print(usage); System.exit(1); } String region = args[0]; String logStreamName = args[1]; String logGroupName = args[2]; // Create a CloudWatchLogClient CloudWatchLogsClient cloudWatchLogsClient = CloudWatchLogsClient.builder() .region(Region.of(region)) .build(); // Designate logGroupName and logStream you want to get logs from // Assume only one stream name exist, this is not always the case 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++) { // Prints the messages to the console System.out.println(cloudWatchLogsClient.getLogEvents(getLogEventsRequest).events().get(c).message()); } System.out.println("Successfully got CloudWatch log events!"); } }

Sample Details

Service: cloudwatch

Last tested: 2019-06-20

Author: ceruleancee

Type: full-example

On this page: