Class: LogStash::Inputs::CloudStorage::FileReader

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/inputs/cloud_storage/file_reader.rb

Overview

FileReader provides a unified way to read different types of log files with predictable callbacks.

Class Method Summary collapse

Class Method Details

.gzip?(filename) ⇒ Boolean

gzip? returns true if the given filename has a gzip file extension.

Returns:

  • (Boolean)


25
26
27
28
# File 'lib/logstash/inputs/cloud_storage/file_reader.rb', line 25

def self.gzip?(filename)
  magic = MimeMagic.by_magic(::File.open(filename))
  magic ? magic.subtype == "gzip" : false
end

.read_gzip_lines(filename, &block) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/logstash/inputs/cloud_storage/file_reader.rb', line 38

def self.read_gzip_lines(filename, &block)
  line_num = 1
  Zlib::GzipReader.open(filename).each_line do |line|
    block.call(line, line_num)
    line_num += 1
  end
end

.read_lines(filename, decode_gzip, &block) ⇒ Object

read_lines reads lines from a file one at a time, optionally decoding the file as gzip if decode_gzip is true.

Handles files with both UNIX and Windows line endings.



16
17
18
19
20
21
22
# File 'lib/logstash/inputs/cloud_storage/file_reader.rb', line 16

def self.read_lines(filename, decode_gzip, &block)
  if decode_gzip && gzip?(filename)
    read_gzip_lines(filename, &block)
  else
    read_plain_lines(filename, &block)
  end
end

.read_plain_lines(filename, &block) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/logstash/inputs/cloud_storage/file_reader.rb', line 30

def self.read_plain_lines(filename, &block)
  line_num = 1
  ::File.open(filename).each do |line|
    block.call(line, line_num)
    line_num += 1
  end
end