在从日志组获取日志事件之前,java中是否有任何方法可以检查给定的日志组和日志流是否存在?
答案 0 :(得分:1)
伪代码:验证日志组的日志流是否存在
withLogGroupName
setter上传递给定的日志组名称。withLogStreamNamePrefix
setter。Java:验证日志组的日志流是否存在(注意:未经测试)
AWSLogsClient logs = new AWSLogsClient();
DescribeLogStreamsRequest req = new DescribeLogStreamsRequest("myLogGroupName")
.withLogStreamNamePrefix("myLogStreamName");
DescribeLogStreamsResult res = logs.describeLogStreams(req);
if(res != null && res.getLogStreams() != null && !res.getLogStreams().isEmpty())
{
// Log Stream exists, do work here
}
答案 1 :(得分:0)
实际上,使用不存在的AWSLogsClient#describeLogStreams
来调用logGroupName
会生成ResourceNotFoundException
。因此,您应该检查:
ResourceNotFoundException
。DescribeLogStreamsResult#getLogStreams
中与提供的logStreamName
匹配的单个条目的存在。将执行此操作的方法的代码段:
private boolean doesLogStreamExist() {
DescribeLogStreamsRequest request = new DescribeLogStreamsRequest()
.withLogGroupName(logGroupName)
.withLogStreamNamePrefix(logStreamName);
try {
return client.describeLogStreams(request).getLogStreams()
.stream()
.anyMatch(it -> it.getLogStreamName().equals(logStreamName));
} catch (ResourceNotFoundException e) {
// log group does not exist
return false;
}
}