Sha256: 15b66729d1598e0dc48d311cfab1d167b519288a9354de1384cd3a0b02083b98

Contents?: true

Size: 1.74 KB

Versions: 1

Compression:

Stored size: 1.74 KB

Contents

require "apache_log/parser/version"

module ApacheLog
  module Parser

    def self.parse(line, format, additional_fields=[])

      common_fields   = %w(remote_host identity_check user datetime request status size)
      combined_fields = common_fields + %w(referer user_agent)

      common_pattern     = '(\S+)\s+(\S+)\s+(\S+)\s+\[(\d{2}\/.*\d{4}:\d{2}:\d{2}:\d{2}\s.*)\]\s+"(\S+\s\S+\s\S+)"\s+(\S+)\s+(\S+)'
      combined_pattern   = common_pattern + '\s+"([^"]*)"\s+"([^"]*)"'
      additional_pattern = ''

      additional_fields.each do
        additional_pattern += '\s+"?([^"]*)"?'
      end

      case format
      when 'common'
        fields = common_fields + additional_fields
        pattern = /^#{common_pattern}#{additional_pattern}$/
      when 'combined'
        fields = combined_fields + additional_fields
        pattern = /^#{combined_pattern}#{additional_pattern}$/
      else
        raise "format error\n no such format: <#{format}> \n"
      end

      match = pattern.match(line)
      raise "parse error\n at line: <#{line}> \n" if match.nil?

      columns = match.to_a

      parsed_hash = {}
      fields.each.with_index do |val, idx|
        val = val.to_sym
        if val == :datetime
          parsed_hash[val] = to_datetime(columns[idx+1])
        elsif val == :request
          parsed_hash[val] = parse_request(columns[idx+1])
        else
          parsed_hash[val] = columns[idx+1]
        end
      end

      parsed_hash
    end

    private
      def self.to_datetime(str)
        DateTime.strptime( str, '%d/%b/%Y:%T %z')
      end

      def self.parse_request(str)
        method, path, protocol = str.split
        {
          method:   method,
          path:     path,
          protocol: protocol,
        }
      end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
apache_log-parser-2.0.0 lib/apache_log/parser.rb