class MnoEnterprise::EventLogger

EventLogger to log various action performed by the end users (eg: sign in, add an app, …) The EventLogger will enqueue notifications and dispatch them to the various listeners. The listeners can then process these event in any way they see fit (Audit Log, Analytics, …)

Public Class Methods

format_metadata(metadata, object) click to toggle source

Get the metadata from the object if not provided

# File lib/mno_enterprise/event_logger.rb, line 42
def self.format_metadata(metadata, object)
  if object.respond_to?(:to_audit_event)
    metadata.merge(object.to_audit_event)
  else
    metadata
  end
end
info(key, current_user_id, description, object, metadata = {}) click to toggle source

Enqueue a logging job to be performed later

@param [String] key unique key identifying the event type @param [Integer] current_user_id user_id of the user triggering the event @param [String] description humanised description @param [Object] metadata @param [Object] object

# File lib/mno_enterprise/event_logger.rb, line 18
def self.info(key, current_user_id, description, object, metadata = {})
  formatted_metadata = format_metadata(metadata, object)
  subject_type = object.class.name
  subject_id = object.id
  # TODO: improve
  # Bypass Job queuing in specs or we'd have to stub lots of Her call for the deserialization
  if Rails.env.test?
    self.send_info(key, current_user_id, description, subject_type, subject_id, formatted_metadata)
  else
    MnoEnterprise::EventLoggerJob.perform_later('info', key, current_user_id, description, subject_type, subject_id, formatted_metadata)
  end
rescue ActiveJob::SerializationError
  Rails.logger.warn "[MnoEnterprise::EventLogger] Serialization error, skipping #{key} event"
end
send_info(key, current_user_id, description, subject_type, subject_id, metadata) click to toggle source

Send the event to the listeners @see .info for the params description

# File lib/mno_enterprise/event_logger.rb, line 35
def self.send_info(key, current_user_id, description, subject_type, subject_id, metadata)
  @@listeners.each do |listener|
    listener.info(key, current_user_id, description, subject_type, subject_id, metadata)
  end
end