class CwlogTail::CwClient

Constants

DEFAULT_LINES
DEFAULT_PAGE_COUNT
TAIL_INTERVAL

Public Instance Methods

client() click to toggle source
# File lib/cwlog_tail/cw_client.rb, line 9
def client
  @cloudwatch ||= Aws::CloudWatchLogs::Client.new
end
log_group_names() click to toggle source
# File lib/cwlog_tail/cw_client.rb, line 17
def log_group_names
  log_groups.map(&:log_group_name)
end
log_groups() click to toggle source
# File lib/cwlog_tail/cw_client.rb, line 13
def log_groups
  client.describe_log_groups.log_groups
end
log_streams(log_group_name, page_count = DEFAULT_PAGE_COUNT) click to toggle source
# File lib/cwlog_tail/cw_client.rb, line 21
def log_streams(log_group_name, page_count = DEFAULT_PAGE_COUNT)
  next_token = nil
  @log_streams ||= page_count.times.each.with_object([]) { |_, results|
    log_streams = client.describe_log_streams(
      log_group_name: log_group_name,
      order_by: :LastEventTime,
      descending: true,
      next_token: next_token)
    results << log_streams.log_streams
    next_token = log_streams.next_token
  }.flatten
end
tail_stream(log_group_name, log_stream_name, options) { |event| ... } click to toggle source
# File lib/cwlog_tail/cw_client.rb, line 34
def tail_stream(log_group_name, log_stream_name, options)
  last_token = nil
  loop do
    log_options = {
      log_group_name: log_group_name,
      log_stream_name: log_stream_name,
      next_token: last_token
    }
    if options[:lines].nil?
      log_options[:start_from_head] = true
      log_options[:limit] = DEFAULT_LINES
    else
      log_options[:start_from_head] = false
      log_options[:limit] = options[:lines]
    end
    pages = client.get_log_events(log_options)
    pages.events.each { |event| yield(event) }

    if last_token == pages.next_forward_token
      if options[:follow]
        sleep TAIL_INTERVAL
      else
        break
      end
    end
    last_token = pages.next_forward_token
  end
end