software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient Java Examples

The following examples show how to use software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: IntegrationTestBase.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
/**
 * @return The LogGroup object included in the DescribeLogGroups response, or null if such group
 *         is not found.
 */
protected static LogGroup findLogGroupByName(final CloudWatchLogsClient awsLogs, final String groupName) {
    String nextToken = null;

    do {
        DescribeLogGroupsResponse result = awsLogs
                .describeLogGroups(DescribeLogGroupsRequest.builder().nextToken(nextToken).build());

        for (LogGroup group : result.logGroups()) {
            if (group.logGroupName().equals(groupName)) {
                return group;
            }
        }
        nextToken = result.nextToken();
    } while (nextToken != null);

    return null;
}
 
Example #2
Source File: IntegrationTestBase.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
/**
 * @return The LogStream object included in the DescribeLogStreams response, or null if such
 *         stream is not found in the specified group.
 */
protected static LogStream findLogStreamByName(final CloudWatchLogsClient awsLogs,
                                               final String logGroupName,
                                               final String logStreamName) {
    String nextToken = null;

    do {
        DescribeLogStreamsResponse result = awsLogs
                .describeLogStreams(DescribeLogStreamsRequest.builder()
                                                             .logGroupName(logGroupName)
                                                             .nextToken(nextToken)
                                                             .build());

        for (LogStream stream : result.logStreams()) {
            if (stream.logStreamName().equals(logStreamName)) {
                return stream;
            }
        }
        nextToken = result.nextToken();
    } while (nextToken != null);

    return null;
}
 
Example #3
Source File: IntegrationTestBase.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
/**
 * @return The MetricFilter object included in the DescribeMetricFilters response, or null if
 *         such filter is not found in the specified group.
 */
protected static MetricFilter findMetricFilterByName(final CloudWatchLogsClient awsLogs,
                                                     final String logGroupName,
                                                     final String filterName) {
    String nextToken = null;

    do {
        DescribeMetricFiltersResponse result = awsLogs
                .describeMetricFilters(DescribeMetricFiltersRequest.builder()
                                                                   .logGroupName(logGroupName)
                                                                   .nextToken(nextToken)
                                                                   .build());

        for (MetricFilter mf : result.metricFilters()) {
            if (mf.filterName().equals(filterName)) {
                return mf;
            }
        }
        nextToken = result.nextToken();
    } while (nextToken != null);

    return null;
}
 
Example #4
Source File: DescribeSubscriptionFilters.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {

        final String USAGE =
                "To run this example, supply a log group name\n" +
                        "Ex: DescribeSubscriptionFilters <log-group-name>\n";

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

        String logGroup = args[0];
        CloudWatchLogsClient logs = CloudWatchLogsClient.builder().build();

        describeFilters(logs, logGroup);

    }
 
Example #5
Source File: DeleteSubscriptionFilter.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {

        final String USAGE =
                "To run this example, supply a filter name and log group name\n" +
                        "Ex: DeleteSubscriptionFilter <filter-name> <log-group-name>\n";

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

        String filter = args[0];
        String logGroup = args[1];

        CloudWatchLogsClient logs = CloudWatchLogsClient.builder()
                .build();

        deleteSubFilter(logs, filter, logGroup );

    }
 
Example #6
Source File: DeleteSubscriptionFilter.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void deleteSubFilter(CloudWatchLogsClient logs, String filter, String logGroup) {

       try {

           DeleteSubscriptionFilterRequest request =
                DeleteSubscriptionFilterRequest.builder()
                        .filterName(filter)
                        .logGroupName(logGroup).build();

           DeleteSubscriptionFilterResponse response =
                logs.deleteSubscriptionFilter(request);

       } catch (CloudWatchException e) {
           System.err.println(e.awsErrorDetails().errorMessage());
           System.exit(1);
       }

       System.out.printf(
                "Successfully deleted CloudWatch log subscription filter %s",
                filter);
    }
 
Example #7
Source File: GetLogEvents.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {

        final String usage =
                "To run this example, supply a logGroupName and streamName as command line arguments\n" +
                        "Ex: GetLogEvents <logGroupName> <streamName>\n";

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

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

        // Create a CloudWatchLogClient
        Region region = Region.US_WEST_2;
        CloudWatchLogsClient cloudWatchLogsClient = CloudWatchLogsClient.builder()
                .region(region)
                .build();

        getCWLogEvebts(cloudWatchLogsClient, logGroupName, logStreamName) ;
    }
 
Example #8
Source File: GetLogEvents.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void getCWLogEvebts(CloudWatchLogsClient cloudWatchLogsClient, String logGroupName, String logStreamName) {


        try {

            // Designate the logGroupName and logStream you want to get logs from
            // Assume only one stream name exists, however, this isn't 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());
            }
        } catch (CloudWatchException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }

        System.out.println("Successfully got CloudWatch log events!");
        // snippet-end:[cloudwatch.java2.get_logs.main]
    }
 
Example #9
Source File: PutLogEvents.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {

        final String usage =
                "To run this example, supply an AWS Region ID (e.g., us-east-1), log group, and stream name as command line arguments\n" +
                        "Example: 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];


        CloudWatchLogsClient logsClient = CloudWatchLogsClient.builder()
            .region(Region.of(regionId))
            .build();

        putCWLogEvents(logsClient, regionId, logGroupName, streamName) ;

    }
 
Example #10
Source File: PutSubscriptionFilter.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {

        final String USAGE =
                "To run this example, supply:\n" +
                        "* a filter name\n" +
                        "* filter pattern\n" +
                        "* log group name\n" +
                        "* a role arn \n" +
                        "* lambda function arn\n\n" +
                        "Ex: PutSubscriptionFilter <filter-name> \\\n" +
                        "                          <filter pattern> \\\n" +
                        "                          <log-group-name> \\\n" +
                        "                          <role-arn> \\\n" +
                        "                          <lambda-function-arn>\n";

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

        String filter = args[0];
        String pattern = args[1];
        String logGroup = args[2];
        String roleArn = args[3];
        String functionArn = args[4];

        Region region = Region.US_WEST_2;
        CloudWatchLogsClient cwl = CloudWatchLogsClient.builder()
                .region(region)
                .build();

        putSubFilters(cwl, filter, pattern, logGroup, roleArn, functionArn ) ;
    }
 
Example #11
Source File: PutSubscriptionFilter.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void putSubFilters(CloudWatchLogsClient cwl,
                                 String filter,
                                 String pattern,
                                 String logGroup,
                                 String roleArn,
                                 String functionArn) {

    try {
        PutSubscriptionFilterRequest request =
                PutSubscriptionFilterRequest.builder()
                        .filterName(filter)
                        .filterPattern(pattern)
                        .roleArn(roleArn)
                        .logGroupName(logGroup)
                        .destinationArn(functionArn)
                        .build();

        PutSubscriptionFilterResponse response =
                cwl.putSubscriptionFilter(request);

    } catch (CloudWatchLogsException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }

    System.out.printf(
            "Successfully created CloudWatch log subscription filter %s",
            filter);
}
 
Example #12
Source File: PutLogEvents.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void putCWLogEvents(CloudWatchLogsClient logsClient, String regionId, String logGroupName, String streamName) {


        // 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 because 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");
    }
 
Example #13
Source File: CloudWatchServiceIntegrationTest.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
@BeforeAll
public static void setUp() throws IOException {

    Region region = Region.US_WEST_2;;
    cw = CloudWatchClient.builder()
            .region(region)
            .build();
    cloudWatchLogsClient = CloudWatchLogsClient.builder().build();

    try (InputStream input = CloudWatchServiceIntegrationTest.class.getClassLoader().getResourceAsStream("config.properties")) {

        Properties prop = new Properties();

        if (input == null) {
            System.out.println("Sorry, unable to find config.properties");
            return;
        }

        //load a properties file from class path, inside static method
        prop.load(input);

        // Populate the data members required for all tests
        logGroup = prop.getProperty("logGroup");
        alarmName = prop.getProperty("alarmName");
        streamName = prop.getProperty("streamName");
        ruleResource = prop.getProperty("ruleResource");
        metricId = prop.getProperty("metricId");
        filterName = prop.getProperty("filterName");
        destinationArn = prop.getProperty("destinationArn");
        roleArn= prop.getProperty("roleArn");
        filterPattern= prop.getProperty("filterPattern");
        instanceId= prop.getProperty("instanceId");

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}
 
Example #14
Source File: CloudWatchLog.java    From pegasus with Apache License 2.0 5 votes vote down vote up
/**
 * Initialize the log.
 *
 * @param awsRegion the aws region
 * @param logLevel the logging level
 * @param logGroup the cloud watch log group
 */
public void initialze(Region awsRegion, Level logLevel, String logGroup) {
    //"405596411149";
    mLogger = Logger.getLogger(Synch.class.getName());
    mLogger.setLevel(logLevel);
    mLogGroup = logGroup;
    mBatchClient = BatchClient.builder().region(awsRegion).build();
    mCWL = CloudWatchLogsClient.builder().region(awsRegion).build();
}
 
Example #15
Source File: CloudWatchLogsProvider.java    From cloudformation-cli-java-plugin with Apache License 2.0 4 votes vote down vote up
public CloudWatchLogsClient get() {
    return defaultClient(CloudWatchLogsClient.builder()).build();
}
 
Example #16
Source File: IntegrationTestBase.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
/**
 * Loads the AWS account info for the integration tests and creates an CloudWatch Logs client
 * for tests to use.
 */
@BeforeClass
public static void setupFixture() throws FileNotFoundException, IOException {
    awsLogs = CloudWatchLogsClient.builder().credentialsProvider(CREDENTIALS_PROVIDER_CHAIN).build();
}