Sha256: ebab348fab327f2bbd75337b42ae0a7a2d36a60df285f3d44d09d4f70b98c512

Contents?: true

Size: 1.43 KB

Versions: 6

Compression:

Stored size: 1.43 KB

Contents

module Nexaas
  module Auditor
    class AuditLogger

      VALID_LEVELS = %w(degug info warn error fatal)

      attr_accessor :logger

      def initialize(logger=nil)
        @logger = logger
      end

      def logger
        @logger ||= Nexaas::Auditor.configuration.logger
      end

      def log(options={})
        level = options.delete(:level)
        raise ArgumentError, "required key `:level` not found" if level.nil?
        raise ArgumentError, "key `:level` is invalid: '#{level}'" unless VALID_LEVELS.include?(level.to_s)
        check_message!(options)
        safe_call { logger.send(level, to_message(options)) }
      end

      private

      def to_message(options)
        # TODO move this logic to a dedicated class and add more tests
        options.inject(['audit_log=true']) do |array, (key,value)|
          value = value.respond_to?(:iso8601) ? value.iso8601 : value
          array << "#{key}=#{value.to_s.strip.gsub(/\s/, '-')}"
          array
        end.join(' ')
      end

      def safe_call(&block)
        begin
          yield(block)
        rescue => exception
          logger.fatal("role=audit_logger class=#{self.class} measure=errors.unable_to_log exception=#{exception.class}")
        end
      end

      def check_message!(options)
        measure = options[:measure] || options['measure']
        raise ArgumentError, "required key 'measure' not found or empty" if measure.to_s == ''
      end

    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
nexaas-auditor-1.1.0 lib/nexaas/auditor/audit_logger.rb
nexaas-auditor-1.0.4 lib/nexaas/auditor/audit_logger.rb
nexaas-auditor-1.0.3 lib/nexaas/auditor/audit_logger.rb
nexaas-auditor-1.0.2 lib/nexaas/auditor/audit_logger.rb
nexaas-auditor-1.0.1 lib/nexaas/auditor/audit_logger.rb
nexaas-auditor-1.0.0 lib/nexaas/auditor/audit_logger.rb