class NginxUtils::Logreader
Public Class Methods
new(log, options={})
click to toggle source
# File lib/nginx_utils/logreader.rb, line 7 def initialize(log, options={}) @log = File.open(log) if options[:parser] raise ArgumentError, "invalid argument" unless options[:parser].is_a? Regexp @format = :custom @parser = options[:parser] else @format = options.fetch(:format, :ltsv) end end
Public Instance Methods
each() { |parse(chomp)| ... }
click to toggle source
# File lib/nginx_utils/logreader.rb, line 19 def each @log.each do |line| yield parse(line.chomp) end end
Private Instance Methods
parse(line)
click to toggle source
# File lib/nginx_utils/logreader.rb, line 26 def parse(line) case @format.to_sym when :ltsv then Hash[ line.split("\t").map do |f| a = f.split(":") a.size > 2 ? [a[0].to_sym, a[1..-1].join(":")] : [a[0].to_sym, a[1]] end ] when :combined then if /([0-9.]+)\s-\s([^\s]+)\s\[(.*?)\]\s"(.*?)"\s([0-9]+)\s([0-9]+)\s"(.*?)"\s"(.*?)".*/ =~ line { remote_addr: $1, remote_user: $2, time_local: $3, request: $4, status: $5, body_bytes_sent: $6, http_referer: $7, http_user_agent: $8 } else {unknown: line} end when :custom then line.scan @parser else raise "format error" end end