From 9d25f74de6cfec3c9b078830b247a47564a4bc51 Mon Sep 17 00:00:00 2001 From: Vikram Kumar Date: Thu, 7 May 2020 15:27:11 -0400 Subject: [PATCH 1/3] support report_level configuration just like rollbar.js --- lib/rollbar/configuration.rb | 2 ++ lib/rollbar/notifier.rb | 11 +++++++++++ spec/rollbar_bc_spec.rb | 23 +++++++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/lib/rollbar/configuration.rb b/lib/rollbar/configuration.rb index 6c30b257..0bdc54da 100644 --- a/lib/rollbar/configuration.rb +++ b/lib/rollbar/configuration.rb @@ -39,6 +39,7 @@ class Configuration attr_accessor :open_timeout attr_accessor :request_timeout attr_accessor :net_retries + attr_accessor :report_level attr_accessor :root attr_accessor :js_options attr_accessor :js_enabled @@ -122,6 +123,7 @@ def initialize @js_enabled = false @js_options = {} @locals = {} + @report_level = :info @scrub_fields = [:passwd, :password, :password_confirmation, :secret, :confirm_password, :password_confirmation, :secret_token, :api_key, :access_token, :accessToken, :session_id] diff --git a/lib/rollbar/notifier.rb b/lib/rollbar/notifier.rb index 3f93437b..1b16c07d 100644 --- a/lib/rollbar/notifier.rb +++ b/lib/rollbar/notifier.rb @@ -22,6 +22,15 @@ class Notifier MUTEX = Mutex.new EXTENSION_REGEXP = /.rollbar\z/.freeze + # helps cache the hierarchy of the rreport levels from low to high + # as both symbols and strings + REPORT_LEVELS = + begin + h = Hash.new(100) # configure with a default level of 100, so it allows unknown log levels + h.merge!([:debug, :info, :warning, :error, :critical].each_with_index.to_a.to_h) + h.merge!(h.transform_keys(&:to_s)) + end.freeze + def initialize(parent_notifier = nil, payload_options = nil, scope = nil) if parent_notifier self.configuration = parent_notifier.configuration.clone @@ -130,6 +139,8 @@ def silenced def log(level, *args) return 'disabled' unless enabled? + return 'not_reported' if REPORT_LEVELS[level] < REPORT_LEVELS[configuration.report_level] + message, exception, extra, context = extract_arguments(args) use_exception_level_filters = use_exception_level_filters?(extra) diff --git a/spec/rollbar_bc_spec.rb b/spec/rollbar_bc_spec.rb index 5b45121e..c4c53aa6 100644 --- a/spec/rollbar_bc_spec.rb +++ b/spec/rollbar_bc_spec.rb @@ -172,6 +172,29 @@ Rollbar.report_exception(@exception).should == 'disabled' end + it 'should not report when level is lower than report_level' do + Rollbar.debug('debug message').should == 'not_reported' + + Rollbar.configure do |config| + config.enabled = true + config.report_level = 'error' + end + + Rollbar.configuration.report_level.should == 'error' + Rollbar.warning('warning message').should == 'not_reported' + end + + + it 'should report when level is higher than or equal to report_level' do + Rollbar.configure do |config| + config.report_level = 'error' + end + + logger_mock.should_receive(:info).with('[Rollbar] Scheduling item') + logger_mock.should_receive(:info).with('[Rollbar] Success') + Rollbar.report_exception(@exception) + end + it 'should stay disabled if configure is called again' do Rollbar.clear_notifier! From c41b54ddb1d654ac7b3d6f9edf99ce4700f7c8e2 Mon Sep 17 00:00:00 2001 From: Vikram B Kumar Date: Tue, 19 May 2020 09:48:11 -0400 Subject: [PATCH 2/3] Update configuration.rb --- lib/rollbar/configuration.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rollbar/configuration.rb b/lib/rollbar/configuration.rb index 0bdc54da..cc5cf4e3 100644 --- a/lib/rollbar/configuration.rb +++ b/lib/rollbar/configuration.rb @@ -123,7 +123,7 @@ def initialize @js_enabled = false @js_options = {} @locals = {} - @report_level = :info + @report_level = :debug @scrub_fields = [:passwd, :password, :password_confirmation, :secret, :confirm_password, :password_confirmation, :secret_token, :api_key, :access_token, :accessToken, :session_id] From a0e60d3a6b2492145ce145dda7c9cb7ecda55320 Mon Sep 17 00:00:00 2001 From: Vikram Kumar Date: Tue, 19 May 2020 11:08:23 -0400 Subject: [PATCH 3/3] handle ruby < 2.3 --- lib/rollbar/notifier.rb | 13 +++++++++---- spec/rollbar_bc_spec.rb | 2 -- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/rollbar/notifier.rb b/lib/rollbar/notifier.rb index 1b16c07d..ffd2a9b8 100644 --- a/lib/rollbar/notifier.rb +++ b/lib/rollbar/notifier.rb @@ -22,13 +22,18 @@ class Notifier MUTEX = Mutex.new EXTENSION_REGEXP = /.rollbar\z/.freeze - # helps cache the hierarchy of the rreport levels from low to high + # helps cache the hierarchy of the report levels from low to high # as both symbols and strings REPORT_LEVELS = begin - h = Hash.new(100) # configure with a default level of 100, so it allows unknown log levels - h.merge!([:debug, :info, :warning, :error, :critical].each_with_index.to_a.to_h) - h.merge!(h.transform_keys(&:to_s)) + h = Hash.new(100) # configure with default of 10 + + [:debug, :info, :warning, :error, :critical].each_with_index do |level, i| + h[level] = i + h[level.to_s] = i + end + + h end.freeze def initialize(parent_notifier = nil, payload_options = nil, scope = nil) diff --git a/spec/rollbar_bc_spec.rb b/spec/rollbar_bc_spec.rb index c4c53aa6..bde55fce 100644 --- a/spec/rollbar_bc_spec.rb +++ b/spec/rollbar_bc_spec.rb @@ -173,8 +173,6 @@ end it 'should not report when level is lower than report_level' do - Rollbar.debug('debug message').should == 'not_reported' - Rollbar.configure do |config| config.enabled = true config.report_level = 'error'