class LoggingElf::GelfData

Attributes

additional_fields[RW]

Public Class Methods

add_exception_details(gelf_data, error) click to toggle source
# File lib/logging_elf/gelf_data.rb, line 71
def self.add_exception_details(gelf_data, error)
  gelf_data.full_message = gelf_data.short_message =
    "<#{error.class.name}> #{error.message}"
  if error.backtrace
    gelf_data.full_message << "\n\t" << error.backtrace.join("\n\t")
  end
end
add_hash_data(gelf_data, data) click to toggle source
# File lib/logging_elf/gelf_data.rb, line 50
def self.add_hash_data(gelf_data, data)
  gelf_data.add_fields(data)

  unless gelf_data.short_message
    gelf_data.short_message = data[:message] || data["message"]
    if gelf_data.short_message.blank?
      gelf_data.short_message = data.map { |k, v| "#{k}='#{v}'" }.join(" ")
    end
  end
end
from_log_event(log_event) click to toggle source
# File lib/logging_elf/gelf_data.rb, line 39
def self.from_log_event(log_event)
  gd = GelfData.new(level: log_event.level, facility: log_event.logger)
  case log_event.data
  when String     then gd.short_message = log_event.data
  when Hash       then add_hash_data(gd, log_event.data)
  when Exception  then add_exception_details gd, log_event.data
  end
  set_backtrace_data gd, log_event
  gd
end
new(gelf_data = {}) click to toggle source
Calls superclass method
# File lib/logging_elf/gelf_data.rb, line 23
def initialize(gelf_data = {})
  super
  self.additional_fields ||= {}
  self.host ||= LoggingElf.config.host if LoggingElf.config
  return if gelf_data.nil?
  add_fields(gelf_data)
end
set_backtrace_data(gelf_data, event) click to toggle source
# File lib/logging_elf/gelf_data.rb, line 79
def self.set_backtrace_data(gelf_data, event)
  gelf_data.add_fields(file: event.file) if event.file
  gelf_data.add_fields(file: event.line) if event.line
  gelf_data.add_fields(file: event.method) if event.method
end

Public Instance Methods

add_fields(params_hash) click to toggle source
# File lib/logging_elf/gelf_data.rb, line 61
def add_fields(params_hash)
  params_hash.each do |key, value|
    if required_gelf_attributes.keys.include? key
      send("#{key}=", value)
    else
      additional_fields["_#{key}".to_sym] = value
    end
  end
end
required_gelf_attributes() click to toggle source
# File lib/logging_elf/gelf_data.rb, line 35
def required_gelf_attributes
  attributes
end
to_gelf() click to toggle source
# File lib/logging_elf/gelf_data.rb, line 31
def to_gelf
  required_gelf_attributes.merge additional_fields
end