//snippet-sourcedescription:[PutEvents.java demonstrates how to put a sample CloudWatch event.]
//snippet-keyword:[SDK for Java 2.0]
//snippet-keyword:[Code Sample]
//snippet-service:[cloudwatch]
//snippet-sourcetype:[full-example]
//snippet-sourcedate:[03/02/2020]
//snippet-sourceauthor:[scmacdon]

/*
 * Copyright 2010-2020 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;
// snippet-start:[cloudwatch.java2.put_log_events.complete]

// snippet-start:[cloudwatch.java2.put_log_events.import]
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.InputLogEvent;
import software.amazon.awssdk.services.cloudwatchlogs.model.PutLogEventsRequest;
import java.util.Arrays;
// snippet-end:[cloudwatch.java2.put_log_events.import]

/**
 * Puts a sample CloudWatch log event
 */
public class PutLogEvents {
    public static void main(String[] args) {

        final String usage =
                "To run this example, supply a region id (eg. us-east-1), log group, and stream name as command line arguments\n" +
                        "Ex: PutLogEvents <region-id> <log-group-name> <stream-name>\n";

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

        String regionId = args[0];
        String logGroupName = args[1];
        String streamName = args[2];

        // snippet-start:[cloudwatch.java2.put_log_events.main]
        CloudWatchLogsClient logsClient = CloudWatchLogsClient.builder().region(Region.of(regionId)).build();

        // A sequence token is required to put a log event in an existing stream.
        // Look up the stream to find its sequence token.

        // First describe all streams in the log group.
        DescribeLogStreamsRequest logStreamRequest = DescribeLogStreamsRequest.builder()
                .logGroupName(logGroupName)
                .logStreamNamePrefix(streamName)
                .build();
        DescribeLogStreamsResponse describeLogStreamsResponse = logsClient.describeLogStreams(logStreamRequest);

        // Assume that a single stream is returned since a specific stream name was specified in the previous request.
        String sequenceToken = describeLogStreamsResponse.logStreams().get(0).uploadSequenceToken();

        // Build an input log message to put to CloudWatch.
        InputLogEvent inputLogEvent = InputLogEvent.builder()
                .message("{ \"key1\": \"value1\", \"key2\": \"value2\" }")
                .timestamp(System.currentTimeMillis())
                .build();

        // Specify the request parameters.
        PutLogEventsRequest putLogEventsRequest = PutLogEventsRequest.builder()
                .logEvents(Arrays.asList(inputLogEvent))
                .logGroupName(logGroupName)
                .logStreamName(streamName)
                // Sequence token is required so that the log can be written to the
                // latest location in the stream.
                .sequenceToken(sequenceToken)
                .build();
        logsClient.putLogEvents(putLogEventsRequest);
        // snippet-end:[cloudwatch.java2.put_log_events.main]

        System.out.println("Successfully put CloudWatch log event");
    }
}
// snippet-end:[cloudwatch.java2.put_log_events.complete]

@@ -177,22 +177,29 @@
 ervice:%5B
-c
+Amazon C
 loud
-w
+W
 atch%5D%0A//
@@ -321,18 +321,8 @@
 ght 
-2010-2020 
 Amaz
@@ -911,68 +911,8 @@
 tch;
-%0A// snippet-start:%5Bcloudwatch.java2.put_log_events.complete%5D
 %0A%0A//
@@ -2095,24 +2095,25 @@
 gs%5B2%5D;%0A%0A
+%0A
         
 // snipp
@@ -2108,173 +2108,408 @@
     
-// snippet-start:%5Bcloudwatch.java2.put_log_events.main%5D%0A        CloudWatchLogsClient logsClient = CloudWatchLogsClient.builder().region(Region.of(regionId)).build();
+CloudWatchLogsClient logsClient = CloudWatchLogsClient.builder()%0A            .region(Region.of(regionId))%0A            .build();%0A%0A        putCWLogEvents(logsClient, regionId, logGroupName, streamName) ;%0A%0A    %7D%0A%0A    // snippet-start:%5Bcloudwatch.java2.put_log_events.main%5D%0A    public static void putCWLogEvents(CloudWatchLogsClient logsClient, String regionId, String logGroupName, String streamName) %7B%0A
 %0A%0A  
@@ -4194,64 +4194,6 @@
   %7D%0A
+
 %7D%0A
-// snippet-end:%5Bcloudwatch.java2.put_log_events.complete%5D%0A

Update PutLogEvents.java