Sha256: 3267533f2469151ccf2b9db2c3ec7de62b3c8a9b9d774091da7a8781f54b67ba

Contents?: true

Size: 1.75 KB

Versions: 1

Compression:

Stored size: 1.75 KB

Contents

require 'apache_log/parser/version'
require 'date'

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.1 lib/apache_log/parser.rb