module Logasm::Utils

Constants

DECIMAL_FRACTION_OF_SECOND
DUMP_OPTIONS

Public Class Methods

application_name(service_name) click to toggle source

Return application name

Returns lower snake case application name. This allows the application value to be used in the elasticsearch index name.

@param [String] service_name

@return [String]

# File lib/logasm/utils.rb, line 31
def self.application_name(service_name)
  underscore(service_name)
end
build_event(metadata, level, application_name) click to toggle source

Build logstash json compatible event

@param [Hash] metadata @param [#to_s] level @param [String] service_name

@return [Hash]

# File lib/logasm/utils.rb, line 14
def self.build_event(metadata, level, application_name)
  overwritable_params
    .merge(metadata)
    .merge(
      application: application_name,
      level: level
    )
end
generate_json(obj) click to toggle source
# File lib/logasm/utils.rb, line 65
def self.generate_json(obj)
  JrJackson::Json.dump(obj, DUMP_OPTIONS)
end
serialize_time_objects!(object) click to toggle source
# File lib/logasm/utils.rb, line 41
def self.serialize_time_objects!(object)
  if object.is_a?(Hash)
    object.each do |key, value|
      object[key] = serialize_time_objects!(value)
    end
  elsif object.is_a?(Array)
    object.each_index do |index|
      object[index] = serialize_time_objects!(object[index])
    end
  elsif object.is_a?(Time) || object.is_a?(Date)
    object.iso8601
  else
    object
  end
end
underscore(input) click to toggle source
# File lib/logasm/utils.rb, line 79
def self.underscore(input)
  word = input.to_s.dup
  word.gsub!(/::/, '/')
  word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
  word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
  word.tr!("-", "_")
  word.downcase!
  word
end

Private Class Methods

overwritable_params() click to toggle source
# File lib/logasm/utils.rb, line 35
def self.overwritable_params
  {
    :@timestamp => Time.now.utc.iso8601(DECIMAL_FRACTION_OF_SECOND)
  }
end