Class: Log2Json::Spitter

Inherits:
Object
  • Object
show all
Defined in:
lib/log2json.rb

Overview

A generic front-end to filters. It sits between an input and a filter, taking log lines from an input and normalizing them into logstash-compatible JSON log records for filters to consume.

An input represents the source of log records. The only requirement of of an input is that it outputs to stdout a stream of lines(one line for each log record), with the first line indicating the source(eg, file path, url, …) of the log lines that follow it. By default, the format of such source-indicating line is the same as those spit out by the tail utility when multiple files are followed.(ie, ==> file-a.txt <==) The format is customizable via a regex.

For each type of logs that you’d like to ship, there will be 1 input process, 1 log2json process(with perhaps multiple filters configured), and 1 output process. All connected via unix pipes. The idea is that you can implement your log input and output processes as shell scripts, and filters can be implemented in ruby(likely using Log2Json::Filters::GrokFilter) and installed as ruby gems. Then, you will configure and combine filters and create a Spitter that would use them. See the log2json ruby script for details.

Constant Summary collapse

CONFIG =
{
  LOG_INPUT_ENCODING: "UTF-8",
  UTC_TIMESTAMP_FORMAT: "%FT%T.%6NZ",
  SOURCE_SEPERATOR_REGEX: Regexp.new("^==> (.+) <=="),
  # because /.../ screws up syntax highlighting in vim so I use Regexp.new(...)

  KEEP_EMTPY_LINES: false,
  TAGS: '',
  FIELDS: '',
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_file, type, opts = {}) ⇒ Spitter

Returns a new instance of Spitter.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/log2json.rb', line 76

def initialize(input_file, type, opts={})
  @input = input_file
  @type = type || ''
  # type can be either a string or a hash whose keys are pathes specified
  # as regex and values are type strings. 

  @options = CONFIG.merge(opts)

  @source_host = %x(hostname).chomp()
  @source_path = nil
  @tags = options[:TAGS].strip.split(/\s*,\s*/)

  fields = options[:FIELDS].strip.gsub(/,/, ' ').split(/ +/)
  raise "Number of keys or values in fields must be even!" if fields.length % 2 != 0

  @fields = {}
  while not fields.empty? do
    k, v = fields.pop(2)
    @fields[k] = v
  end
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



74
75
76
# File 'lib/log2json.rb', line 74

def options
  @options
end

Instance Method Details

#each_record(&block) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/log2json.rb', line 98

def each_record(&block)
  @input.each_line do |line|
    line.force_encoding(options[:LOG_INPUT_ENCODING])
    line.chomp!
    next if line.empty? unless @options[:KEEP_EMPTY_LINES]
    if line =~ options[:SOURCE_SEPERATOR_REGEX]
      @source_path = $1
      next
    end
    block.call({
      # Every record has a '@type' this is how we match filters to log records.
      # Note: in Ruby 1.9, Hash are ordered, so here we'll be matching source path
      #       against the regex in the order they are defined.
      '@type' => if @type.is_a?(String)
                   @type
                 else  # @type is a Hash
                   if type = @type.find { |re, t| re =~ @source_path }
                     type[1]
                   else
                     @type[nil] || ''
                   end
                 end,
      '@source_path' => @source_path,
      '@source_host' => @source_host,
      '@timestamp' => Time.new.utc.strftime(options[:UTC_TIMESTAMP_FORMAT]),
      '@message' => line,
      '@tags' => @tags.clone,     # defaults to []
      '@fields' => @fields.clone, # defaluts to {}
    })
  end
end