Sha256: 20c3fb33fe7c4a5e476394857363825e7f9fae7b122261f8706b08caa5da22ea

Contents?: true

Size: 1.73 KB

Versions: 11

Compression:

Stored size: 1.73 KB

Contents

# Call EffectiveLog.info or EffectiveLog.success  EffectiveLog.error

class EffectiveLogger
  def self.log(message, status = EffectiveLogging.statuses.first, options = {})
    if options[:user].present? && !options[:user].kind_of?(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

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

    if log.associated.present?
      log.associated_to_s ||= (log.associated.to_s rescue nil)
    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.details = options.delete_if { |k, v| v.blank? } if options.kind_of?(Hash)

    log.save
  end

  # Dynamically add logging methods based on the defined statuses
  # EffectiveLogging.info 'my message'
  (EffectiveLogging.statuses || []).each do |status|
    self.singleton_class.send(:define_method, status) { |message, options={}| log(message, status, options) }
  end

end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
effective_logging-2.0.2 app/models/effective_logger.rb
effective_logging-2.0.1 app/models/effective_logger.rb
effective_logging-2.0.0 app/models/effective_logger.rb
effective_logging-1.11.5 app/models/effective_logger.rb
effective_logging-1.11.4 app/models/effective_logger.rb
effective_logging-1.11.3 app/models/effective_logger.rb
effective_logging-1.11.2 app/models/effective_logger.rb
effective_logging-1.11.1 app/models/effective_logger.rb
effective_logging-1.11.0 app/models/effective_logger.rb
effective_logging-1.10.18 app/models/effective_logger.rb
effective_logging-1.10.17 app/models/effective_logger.rb