Sha256: 6235fc4a8bef1f2cec4eb28b7c7f0f1a34322ec1b44aaad6b376f68d463e8173

Contents?: true

Size: 1.38 KB

Versions: 4

Compression:

Stored size: 1.38 KB

Contents

module LogStats
  module LineParser
    def self.parse(log_data, config)
      data = {}
      log_data.split("\n").each do |line_string|
        config[:events].each do |event, event_config|
          if event_config[:line_pattern] =~ line_string
            data[event] ||= []
            data[event].push(parse_line(line_string, event_config))
          end
        end
      end
      data
    end

    def self.strip_quotes(value)
      if value && value.start_with?('"')
        value[1..-2]
      else
        value
      end
    end

    def self.parse_numeric(value)
      value[/\d+/].to_i
    end

    def self.parse_field(field, line_string, event_config)
      if field[:parse]
        value = field[:parse].call(line_string)
      else
        value = /\b#{field[:name]}=(\S+)/.match(line_string).to_a[1]
        value = strip_quotes(value)
        value = parse_numeric(value) if field[:numeric]
      end
      puts "Parsing failed field=#{field} line=#{line_string}" unless (value || field[:optional])
      value
    rescue Exception => e
      puts "Parsing failed field=#{field} line=#{line_string}: #{e.message}"
      nil
    end

    def self.parse_line(line_string, event_config)
      event_config[:fields].reduce({}) do |acc, field|
        if value = parse_field(field, line_string, event_config)
          acc[field[:name]] = value
        end
        acc
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
log_stats-0.4.2 lib/log_stats/line_parser.rb
log_stats-0.4.1 lib/log_stats/line_parser.rb
log_stats-0.4.0 lib/log_stats/line_parser.rb
log_stats-0.3.0 lib/log_stats/line_parser.rb