Class: Ixtlan::UserLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/ixtlan/user_logger.rb

Constant Summary collapse

AUDIT =
Ixtlan::Models::AUDIT.nil? ? nil : Object.full_const_get(Ixtlan::Models::AUDIT)

Instance Method Summary collapse

Constructor Details

#initialize(arg) ⇒ UserLogger

Returns a new instance of UserLogger.



7
8
9
# File 'lib/ixtlan/user_logger.rb', line 7

def initialize(arg)
  @logger = Slf4r::LoggerFacade.new(arg)
end

Instance Method Details

#log(controller, message = nil, &block) ⇒ Object



20
21
22
# File 'lib/ixtlan/user_logger.rb', line 20

def log(controller, message = nil, &block)
  log_user((controller), message, &block)
end

#log_action(controller, message = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ixtlan/user_logger.rb', line 24

def log_action(controller, message = nil)
  log_user((controller)) do
    as_xml = controller.response.content_type == 'application/xml' ? " - xml" : ""
    if controller.params[:controller]
      audits = controller.instance_variable_get("@#{controller.params[:controller].to_sym}")
      if(audits)
        "#{controller.params[:controller]}##{controller.params[:action]} #{audits.model.to_s.plural}[#{audits.size}]#{as_xml}#{message}"
      else
        audit = controller.instance_variable_get("@#{controller.params[:controller].singular.to_sym}")
        if(audit)
          errors = if(audit.respond_to?(:errors) && !audit.errors.empty?)
                     " - errors: " + audit.errors.full_messages.join(", ")
                   end
          audit_log = audit.respond_to?(:to_log) ? audit.to_log : "#{audit.model}(#{audit.key})"
          "#{controller.params[:controller]}##{controller.params[:action]} #{audit_log}#{as_xml}#{message}#{errors}"
        else
          "#{controller.params[:controller]}##{controller.params[:action]}#{as_xml}#{message}"
        end
      end
    else
      "params=#{controller.params.inspect}#{message}"
    end
  end
end

#log_user(user, message = nil, &block) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/ixtlan/user_logger.rb', line 49

def log_user(user, message = nil, &block)
  user ||= "???"
  msg = "#{message}#{block.call if block}"
  if AUDIT
    AUDIT.new(:date => DateTime.now, :message => msg, :login => user).push
  end
  @logger.info {"[#{user}] #{msg}" }
end