class Fluent::TextFormatter::TextToJsonFormatter

Public Instance Methods

configure(conf) click to toggle source
Calls superclass method
# File lib/fluent/plugin/formatter_text_to_json.rb, line 12
def configure(conf)
  super
  @fields = @json_field.split(",").map{|field| "\"" + field + "\""}
end
format(tag, time, record) click to toggle source
# File lib/fluent/plugin/formatter_text_to_json.rb, line 17
def format(tag, time, record)
  
  tmp_field = record["message"].values.join("").split(@field_splitter)
  
  tmp_field[1] = @field_splitter + tmp_field[1]
  

  #sdr parameters to json format
  tmp_field[0] = tmp_field[0].split("\n")
  tmp_field[0] = tmp_field[0].map{|value| (value == "")? nil:value} # HBase plugin ignores nil values
  tmp_field[0] = tmp_field[0].map{|value| (value == nil)? nil:"\""+value+"\""}
  
  tmp_field[0] = @fields[2,14].zip(tmp_field[0])
  tmp_field[0] = tmp_field[0].map{|field, value|
        new_value = (value==nil)? "null":value
        field+":"+new_value}.join(",")
  

  #factor paramters to json format
  #Set factor ID as a second field's name
  head, *tail = tmp_field[1].split("\n")
  head = head.split("|")
  head = head.map{|field| "\""+field+"\""}


  tail = tail.map{|values| values.split("|")}

  new_tail = Array.new(head.length)
  
  for i in 0..head.length-1
  new_tail[i] = tail.map{|values| values[i]}
  new_tail[i] = "\"" + new_tail[i][0,new_tail[i].length-2].join("|") + "\""
  end

  tmp_field[1] = head.zip(new_tail).map{|field,value| field + ":" + value }.join(",") 
  
  out_string = "{#{@fields[0]}:{#{tmp_field[0]}},#{@fields[1]}:{#{tmp_field[1]}}}"
  JSON.parse(out_string.gsub('\"','"'))

end