Sha256: dca69c166aae6ce783d3cace9889ce8c9ef6220d4b77b0f08ed860038e511b9f

Contents?: true

Size: 1.96 KB

Versions: 22

Compression:

Stored size: 1.96 KB

Contents

require 'logger'
require 'rollbar'

module Rollbar
  # This class provides logger interface that can be used to replace
  # the application logger and send all the log messages to Rollbar
  #
  # Usage:
  # require 'rollbar/logger'
  # logger = Rollbar::Logger.new
  # logger.error('Error processing purchase')
  #
  # If using Rails, you can extend the Rails logger so messages are logged
  # normally and also to Rollbar:
  #
  # Rails.logger.extend(ActiveSupport::Logger.broadcast(Rollbar::Logger.new))
  class Logger < ::Logger
    class Error < RuntimeError; end
    class DatetimeFormatNotSupported < Error; end
    class FormatterNotSupported < Error; end

    def initialize
      @level = ERROR
    end

    def add(severity, message = nil, progname = nil)
      return true if severity < @level

      message ||= block_given? ? yield : progname

      return true if message.blank?

      rollbar.log(rollbar_level(severity), message)
    end

    def <<(message)
      error(message)
    end

    def formatter=(_)
      raise(FormatterNotSupported)
    end

    def formatter
      raise(FormatterNotSupported)
    end

    def datetime_format=(_)
      raise(DatetimeFormatNotSupported)
    end

    def datetime_format
      raise(DatetimeFormatNotSupported)
    end

    # Returns a Rollbar::Notifier instance with the current global scope and
    # with a logger writing to /dev/null so we don't have a infinite loop
    # when Rollbar.configuration.logger is Rails.logger.
    def rollbar
      notifier = Rollbar.scope
      notifier.configuration.logger = ::Logger.new('/dev/null')

      notifier
    end

    private

    # Find correct Rollbar level to use using the indexes in Logger::Severity
    # DEBUG = 0
    # INFO = 1
    # WARN = 2
    # ERROR = 3
    # FATAL = 4
    # UNKNOWN = 5
    #
    # If not found we'll use 'error' as the used level
    def rollbar_level(severity)
      [:debug, :info, :warning, :error, :critical, :error][severity] || :error
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
rollbar-2.15.5 lib/rollbar/logger.rb
rollbar-2.15.4 lib/rollbar/logger.rb
rollbar-2.15.3 lib/rollbar/logger.rb
rollbar-2.15.2 lib/rollbar/logger.rb
rollbar-2.15.1 lib/rollbar/logger.rb
rollbar-2.15.0 lib/rollbar/logger.rb
rollbar-2.14.1 lib/rollbar/logger.rb
rollbar-2.14.0 lib/rollbar/logger.rb
rollbar-2.13.3 lib/rollbar/logger.rb
rollbar-2.13.2 lib/rollbar/logger.rb
rollbar-2.13.1 lib/rollbar/logger.rb
rollbar-2.13.0 lib/rollbar/logger.rb
rollbar-2.12.0 lib/rollbar/logger.rb
rollbar-2.11.5 lib/rollbar/logger.rb
rollbar-2.11.4 lib/rollbar/logger.rb
rollbar-2.11.3 lib/rollbar/logger.rb
rollbar-2.11.2 lib/rollbar/logger.rb
rollbar-2.11.1 lib/rollbar/logger.rb
rollbar-2.11.0 lib/rollbar/logger.rb
rollbar-2.10.0 lib/rollbar/logger.rb