Module: Logasm::Utils

Defined in:
lib/logasm/utils.rb

Constant Summary collapse

DECIMAL_FRACTION_OF_SECOND =
3

Class Method Summary collapse

Class Method Details

.application_name(service_name) ⇒ String

Return application name

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

Parameters:

  • service_name (String)

Returns:

  • (String)


31
32
33
# File 'lib/logasm/utils.rb', line 31

def self.application_name(service_name)
  underscore(service_name)
end

.build_event(metadata, level, application_name) ⇒ Hash

Build logstash json compatible event

Parameters:

  • metadata (Hash)
  • level (#to_s)
  • service_name (String)

Returns:

  • (Hash)


14
15
16
17
18
19
20
21
# File 'lib/logasm/utils.rb', line 14

def self.build_event(, level, application_name)
  overwritable_params
    .merge(serialize_time_objects!(.dup))
    .merge(
      application: application_name,
      level: level.to_s
    )
end

.serialize_time_objects!(object) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/logasm/utils.rb', line 57

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