Is there any method in java to check whether a given Log Group and Log Stream exists, before getting log events from the Log Group?
Pseudocode: Validate that a log group's log stream exists
- Build describeLogStreamsRequest:
- Pass in your given log group name on the constructor, or on the request's
withLogGroupName
setter. - Pass in log stream name in on the request's
withLogStreamNamePrefix
setter.
- Pass in your given log group name on the constructor, or on the request's
- Call describeLogStreams.
- Inspect the resulting log streams on the DescribeLogStreamsResult object. If the list isn't empty, you're safe to further operate on that stream.
Java: Validate that a log group's log stream exists (note: untested)
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
}
I know this question asks for a Java solution, but this was the top-ranked question on Google when I had the same question for Python. The boto3
documentation doesn't seem to directly support a "does this log group exist?" function as of today, so here is what I wrote instead. I use the paginator because by default the boto3
API only lets you pull 50 log groups at a time. The paginator will keep fetching log group names until it goes through all of your log groups.
import boto3
def log_group_exists(log_group_name):
client = boto3.client('logs')
paginator = client.get_paginator('describe_log_groups')
for page in paginator.paginate():
for group in page['logGroups']:
if group['logGroupName'].lower() == log_group_name.lower():
return True
return False
In reality, a call to AWSLogsClient#describeLogStreams
with a logGroupName
that does not exist will generate a ResourceNotFoundException
. For that reason, you should check for:
- Absence of
ResourceNotFoundException
. - Existence of a single entry in
DescribeLogStreamsResult#getLogStreams
matching thelogStreamName
provided.
Code snippet of a method that will do that:
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;
}
}
© 2022 - 2025 — McMap. All rights reserved.
AWSLogsClient#describeLogStreams
with alogGroupName
that does not exist will generate aResourceNotFoundException
. If you are unsure about the existence of the log group, you should handle the exception. – Webfooted