Home > Back-end >  How to get Latest Log Stream Name Using Java
How to get Latest Log Stream Name Using Java

Time:12-01

How do we get the latest log stream name from a given log group using java code. I have tried many possibilities and still not able to retrieve it.

AWSLogs awsLogs = AWSLogsClientBuilder
                .standard()
                .withCredentials(new AWSStaticCredentialsProvider(credentials))
                .withRegion(Regions.EU_WEST_1)
                .build();

DescribeLogStreamsRequest describeLogStreamsRequest = new DescribeLogStreamsRequest()
    .withLogGroupName("/aws/lambda/a250348-ecndesc-conversion-lambda-reci-euw1-qe");
describeLogStreamsResult = awsLogs.describeLogStreams(describeLogStreamsRequest);
System.out.println(describeLogStreamsResult.getLogStreams());

for(int i =0; i<describeLogStreamsResult.getLogStreams().size(); i  ) {
    logStream = describeLogStreamsResult.getLogStreams().get(i).getLastEventTimestamp();
    Date date = new Date(logStream);
    dateList.add(date);         
}
latestDate = Collections.max(dateList);
System.out.println(latestDate);

System.out.println(dateList.indexOf(latestDate));
logStream1 = describeLogStreamsResult.getLogStreams().get(dateList.indexOf(latestDate)).getLogStreamName();

System.out.println(logStream1);

CodePudding user response:

Try to order your results:

DescribeLogStreamsRequest describeLogStreamsRequest = new DescribeLogStreamsRequest()
    .withLogGroupName("/aws/lambda/a250348-ecndesc-conversion-lambda-reci-euw1-qe")
    .withOrderBy(com.amazonaws.services.logs.model.OrderBy.LastEventTime)
    .withDescending(true);

This will return first the log stream with the latest event time (and not necessarily the last created). This is how results are presented in AWS console.

  • Related