Sha256: c57ee99c596ee0d1d53c5ac5cadda1821aa776ee85962d1f29d8b9ecfd794ed4

Contents?: true

Size: 1.55 KB

Versions: 5

Compression:

Stored size: 1.55 KB

Contents

require 'slf4r'
module Ixtlan
  
  class UserLogger
    
    def initialize(arg)
      @logger = Slf4r::LoggerFacade.new(arg)
    end

    private 

    def login_from(controller)
      user = controller.respond_to?(:current_user) ? controller.send(:current_user) : nil
      user.nil? ? nil: user.login
    end

    public 

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

    def log_action(controller, message = nil)
      log_user(login_from(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)
              "#{controller.params[:controller]}##{controller.params[:action]} #{audit.model}(#{audit.key})#{as_xml}#{message}"
            else
              "#{controller.params[:controller]}##{controller.params[:action]}#{as_xml}#{message}"
            end
          end
        else
          "params=#{controller.params.inspect}"
        end
      end
    end
     
    def log_user(user, message = nil, &block)
      user = "???" unless user
      @logger.info {"[#{user}] #{message}#{block.call if block}" } 
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ixtlan-0.2.4 lib/ixtlan/user_logger.rb
ixtlan-0.2.3 lib/ixtlan/user_logger.rb
ixtlan-0.2.2 lib/ixtlan/user_logger.rb
ixtlan-0.2.1 lib/ixtlan/user_logger.rb
ixtlan-0.2.0 lib/ixtlan/user_logger.rb