# # This module is copied from fluentd/lib/fluent/parser.rb and # fixed not to overwrite 'time' (reserve nil) when time not found in parsed string. module FluentExt; end class FluentExt::TextParser class RegexpParser include Fluent::Configurable config_param :time_format, :string, :default => nil def initialize(regexp, conf={}) super() @regexp = regexp unless conf.empty? configure(conf) end end def call(text) m = @regexp.match(text) unless m $log.debug "pattern not match: #{text}" # TODO? return nil, nil end time = nil record = {} m.names.each {|name| if value = m[name] case name when "time" if @time_format time = Time.strptime(value, @time_format).to_i else time = Time.parse(value).to_i end else record[name] = value end end } return time, record end end class JSONParser include Fluent::Configurable config_param :time_key, :string, :default => 'time' config_param :time_format, :string, :default => nil def call(text) record = Yajl.load(text) time = nil if value = record.delete(@time_key) if @time_format time = Time.strptime(value, @time_format).to_i else time = value.to_i end end return time, record rescue Yajl::ParseError return nil, nil end end TEMPLATES = { 'apache' => RegexpParser.new(/^(?[^ ]*) [^ ]* (?[^ ]*) \[(?