どのようにJavaを使用して、AWS CloudWatchのログを取得します

KVI Kfに:

私たちは、AWSラムダとCloudWatchのログに取り組んでいます。今、私は、JavaでlogStreamNameを使用せずにCloudWatchのログからすべてのログ・イベントを取得します。

我々は、動的な方法でログ・ストリームを生成しているので、CloudWatchのログ・グループからすべてのログを取得する方法を確認していません。

私たちは、ログ・ストリームの名前を持っている場合は、私たちは、コードの下に使用することができ、知っています

ClientConfiguration clientConfig = getClientConfig();

AWSLogsClientBuilder builder = AWSLogsClientBuilder.standard();

AWSLogs logsClient= builder.withCredentials(new AWSStaticCredentialsProvider(new ProfileCredentialsProvider(profile).getCredentials())).withRegion(Regions.AP_SOUTHEAST_2).withClientConfiguration(clientConfig).build();

GetLogEventsRequest request = new GetLogEventsRequest()
                        .withStartTime(1531231200000L)
                        .withEndTime(1531576800000L)
                        .withLogGroupName("FlowLogs_GroupName")
                        .withLogStreamName("eni-xxxxx");

GetLogEventsResult result = logsClient.getLogEvents(request);

result.getEvents().forEach(outputLogEvent -> {
          System.out.println(outputLogEvent.getMessage());
}); 

このAWSに新しいですので、缶誰もがいくつかのコードサンプルで私を助けてください?

Dushmantha:

あなたは使用することができDescribeLogStreamsRequest、ログ・ストリーム名を取得します。これは役立つだろうホープ

  public static void main( String[] args )
        {
            ClientConfiguration clientConfig = new ClientConfiguration();

            AWSLogsClientBuilder builder = AWSLogsClientBuilder.standard();

            AWSLogs logsClient = builder.withCredentials( new AWSStaticCredentialsProvider( new ProfileCredentialsProvider().getCredentials() ) )
                    .withRegion( Regions.AP_SOUTHEAST_2 )
                    .withClientConfiguration( clientConfig ).build();

            DescribeLogStreamsRequest describeLogStreamsRequest = new DescribeLogStreamsRequest().withLogGroupName( "FlowLogs_GroupName"  );
            DescribeLogStreamsResult describeLogStreamsResult = logsClient.describeLogStreams( describeLogStreamsRequest );

            for ( LogStream logStream : describeLogStreamsResult.getLogStreams() )
            {
                GetLogEventsRequest getLogEventsRequest = new GetLogEventsRequest()
                        .withStartTime( 1531231200000L )
                        .withEndTime( 1531576800000L )
                        .withLogGroupName( "FlowLogs_GroupName" )
                        .withLogStreamName( logStream.getLogStreamName() );

                GetLogEventsResult result = logsClient.getLogEvents( getLogEventsRequest );

                result.getEvents().forEach( outputLogEvent -> {
                    System.out.println( outputLogEvent.getMessage() );
                } );

            }
        }

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=181729&siteId=1