java - 如何使用 java 获取 Aws cloudwatch 日志

标签 java amazon-web-services aws-lambda amazon-cloudwatchlogs

我们正在处理 AWS Lambda 和 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 的新手,任何人都可以帮我提供一些代码示例吗?

最佳答案

您可以使用 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() );
                } );

            }
        }

关于java - 如何使用 java 获取 Aws cloudwatch 日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54072657/

相关文章:

java - jOOQ 中的交错/分阶段/条件 SQL 构建

java.net.SocketTimeoutException : Read timed out when querying url

node.js - 使用 Lambda Node 从 S3 上的文件在 S3 上创建一个 zip 文件

java - 我想从 ListView 中选择用户名文本字段并使用该用户名删除 mysql 数据库中存在的数据

java - 服务器启动时出现 org.glassfish.jersey.servlet.ServletContainer ClassNotFoundException

linux - 我怎样才能用 yas3fs 做一份 Upstart 的工作?

amazon-web-services - 带有到私有(private) ALB 的自定义链接的 AWS HTTP API 网关

amazon-web-services - 如何在 RedShift/ParAccel 中测量磁盘上的表空间

amazon-web-services - AWS Batch EC2 配置时间

node.js - 无服务器 NodeJS 文件/文件夹结构