class MicroserviceLogger::JsonEventLogger

Public Class Methods

new(service_name:, clock:, output:, scoped_properties:, event_type:) click to toggle source
# File lib/json_event_logger.rb, line 4
def initialize(service_name:,
               clock:,
               output:,
               scoped_properties:,
               event_type:)
  @service_name = service_name
  @clock = clock
  @output = output
  @scoped_properties = scoped_properties
  @event_type = event_type
end

Public Instance Methods

critical(extra_properties) click to toggle source
# File lib/json_event_logger.rb, line 32
def critical(extra_properties)
  log 'CRITICAL', normalize(extra_properties)
end
debug(extra_properties) click to toggle source
# File lib/json_event_logger.rb, line 16
def debug(extra_properties)
  log 'DEBUG', normalize(extra_properties)
end
error(extra_properties) click to toggle source
# File lib/json_event_logger.rb, line 28
def error(extra_properties)
  log 'ERROR', normalize(extra_properties)
end
info(extra_properties) click to toggle source
# File lib/json_event_logger.rb, line 20
def info(extra_properties)
  log 'INFO', normalize(extra_properties)
end
warning(extra_properties) click to toggle source
# File lib/json_event_logger.rb, line 24
def warning(extra_properties)
  log 'WARNING', normalize(extra_properties)
end

Private Instance Methods

log(severity, extra_properties) click to toggle source
# File lib/json_event_logger.rb, line 38
def log(severity, extra_properties)
  properties =
    required_properties(severity)
    .merge(@scoped_properties)
    .merge(extra_properties)

  @output.puts(properties.to_json)
end
normalize(properties) click to toggle source
# File lib/json_event_logger.rb, line 56
def normalize(properties)
  case properties
  when Hash
    properties
  else
    { message: properties.to_s }
  end
end
required_properties(severity) click to toggle source
# File lib/json_event_logger.rb, line 47
def required_properties(severity)
  {
    service: @service_name,
    event_type: @event_type,
    timestamp: @clock.now.utc.iso8601(3),
    severity: severity
  }
end