Class: EffectiveLogger

Inherits:
Object
  • Object
show all
Defined in:
app/models/effective_logger.rb

Overview

Call EffectiveLog.info or EffectiveLog.success EffectiveLog.error

Class Method Summary collapse

Class Method Details

.log(message, status = EffectiveLogging.statuses.first, options = {}) ⇒ Object


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/effective_logger.rb', line 6

def self.log(message, status = EffectiveLogging.statuses.first, options = {})
  return if EffectiveLogging.supressed?

  options = Hash(options).delete_if { |k, v| v.blank? }

  if options[:user].present? && !options[:user].class.name.end_with?('User')
    raise ArgumentError.new('Log.log :user => ... argument must be a User object')
  end

  if options[:parent].present? && !options[:parent].kind_of?(Effective::Log)
    raise ArgumentError.new('Log.log :parent => ... argument must be an Effective::Log object')
  end

  if options[:associated].present? && !options[:associated].kind_of?(ActiveRecord::Base)
    raise ArgumentError.new('Log.log :associated => ... argument must be an ActiveRecord::Base object')
  end

  if options[:request].present? && options[:request].respond_to?(:referrer)
    request = options.delete(:request)

    options[:ip] ||= request.ip
    options[:referrer] ||= request.referrer
    options[:user_agent] ||= request.user_agent
  end

  log = Effective::Log.new(
    message: message,
    status: status,
    user_id: options.delete(:user_id),
    user_type: options.delete(:user_type),
    user: options.delete(:user),
    parent: options.delete(:parent),
    associated: options.delete(:associated),
    associated_to_s: options.delete(:associated_to_s),
    details: options
  )

  if log.associated.present?
    log.associated_to_s ||= log.associated.to_s
  end

  log.save!

  log
end