Module: Logasm::Utils

Defined in:
lib/logasm/utils.rb

Constant Summary collapse

HOST =
::Socket.gethostname
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)


33
34
35
# File 'lib/logasm/utils.rb', line 33

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

.build_event(metadata, level, service_name) ⇒ Hash

Build logstash json compatible event

Parameters:

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

Returns:

  • (Hash)


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

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

.serialize_time_objects(object) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/logasm/utils.rb', line 44

def self.serialize_time_objects(object)
  if object.is_a?(Hash)
    object.reduce({}) do |hash, (key, value)|
      hash.merge(key => serialize_time_objects(value))
    end
  elsif object.is_a?(Array)
    object.map(&method(:serialize_time_objects))
  elsif object.is_a?(Time) || object.is_a?(Date)
    object.iso8601
  else
    object
  end
end