Sha256: 5103dabbcc951360a5e4a9e01d8cb2d9979d7348ad4b98200e88b121469d538b

Contents?: true

Size: 1.67 KB

Versions: 3

Compression:

Stored size: 1.67 KB

Contents

require 'active_support/core_ext/hash/indifferent_access'

module SentinelApi
  class Notifier
    class << self

      def warn(*args)
        log('WARN', args)
      end

      def info(*args)
        log('INFO', args)
      end

      def error(*args)
        log('ERROR', args)
      end

      private_class_method

      def log(level = nil, args)
        level ||= configuration.level
        message, info, tags = extract_arguments(args)

        SentinelApi::Client.send_to_server({ level: level, message: message, info: info, tags: tags })
      end

      def extract_arguments(args)
        message = nil
        exception = nil
        tags = nil
        if args.count == 1 && args[0].is_a?(Hash)
          result = args[0].with_indifferent_access
          return [result[:message], result[:info], result[:tags]]
        elsif args.count == 1 && args[0].is_a?(Exception)
          return [args[0].message, prepare_info(args[0]), []]
        else
          args.each do |arg|
            if arg.is_a?(String)
              message = arg
            elsif arg.is_a?(Exception)
              exception = arg
            elsif RUBY_PLATFORM == 'java' && arg.is_a?(java.lang.Exception)
              exception = arg
            elsif arg.is_a?(Array)
              tags = arg

              tags = nil if tags.empty?
            end
          end
        end

        [message, prepare_info(exception), tags]
      end

      def prepare_info(exception)
        "#{exception.class}: #{exception.message}, \n#{exception.backtrace.join("\n")}"
      rescue
        "#{exception.class}: #{exception.message}"
      end


      def configuration
        SentinelApi.configuration
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
logkeeper_api-0.0.4 lib/sentinel_api/notifier.rb
logkeeper_api-0.0.3 lib/sentinel_api/notifier.rb
logkeeper_api-0.0.2 lib/sentinel_api/notifier.rb