5

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?

Anthony Neace
  • 25,013
  • 7
  • 114
  • 129
Shivkumar Mallesappa
  • 2,875
  • 7
  • 41
  • 68

3 Answers3

2

Pseudocode: Validate that a log group's log stream exists

  1. 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.
  2. Call describeLogStreams.
  3. 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
}
Anthony Neace
  • 25,013
  • 7
  • 114
  • 129
  • Just a heads up, a call to `AWSLogsClient#describeLogStreams` with a `logGroupName` that does not exist will generate a `ResourceNotFoundException`. If you are unsure about the existence of the log group, you should handle the exception. – Trein Dec 28 '17 at 18:45
0

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:

  1. Absence of ResourceNotFoundException.
  2. Existence of a single entry in DescribeLogStreamsResult#getLogStreams matching the logStreamName 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;
    }
}
Trein
  • 3,658
  • 27
  • 36
0

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
user554481
  • 1,875
  • 4
  • 26
  • 47