From 50708e1aa09d76af36c8f217284250b484a15ef5 Mon Sep 17 00:00:00 2001 From: st0012 Date: Thu, 27 Aug 2020 18:49:11 +0800 Subject: [PATCH] Don't log warning messages when it doesn't need to This SDK can be enabled by the environments config. So when it's not enabled in the current environment, it shouldn't log any messages. --- lib/raven/configuration.rb | 6 ++++- lib/raven/instance.rb | 1 + spec/raven/instance_spec.rb | 47 +++++++++++++++++++++++-------------- 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/lib/raven/configuration.rb b/lib/raven/configuration.rb index 6b69e62ba..c8f71d95a 100644 --- a/lib/raven/configuration.rb +++ b/lib/raven/configuration.rb @@ -342,6 +342,10 @@ def exception_class_allowed?(exc) end end + def enabled_in_current_env? + environments.empty? || environments.include?(current_environment) + end + private def detect_project_root @@ -423,7 +427,7 @@ def detect_release_from_env end def capture_in_current_environment? - return true unless environments.any? && !environments.include?(current_environment) + return true if enabled_in_current_env? @errors << "Not configured to send/capture in environment '#{current_environment}'" false diff --git a/lib/raven/instance.rb b/lib/raven/instance.rb index 8109bbdff..c35d81c4c 100644 --- a/lib/raven/instance.rb +++ b/lib/raven/instance.rb @@ -50,6 +50,7 @@ def client # Tell the log that the client is good to go def report_status + return unless configuration.enabled_in_current_env? return if configuration.silence_ready if configuration.capture_allowed? diff --git a/spec/raven/instance_spec.rb b/spec/raven/instance_spec.rb index 6acb8c017..f22006759 100644 --- a/spec/raven/instance_spec.rb +++ b/spec/raven/instance_spec.rb @@ -194,29 +194,42 @@ def ivars(object) end let(:not_ready_message) do - "Raven #{Raven::VERSION} configured not to capture errors." + "Raven #{Raven::VERSION} configured not to capture errors: DSN not set" end - it 'logs a ready message when configured' do - subject.configuration.silence_ready = false + context "when current environment is included in environments" do + before do + subject.configuration.silence_ready = false + subject.configuration.environments = ["default"] + end - expect(subject.logger).to receive(:info).with(ready_message) - subject.report_status - end + it 'logs a ready message when configured' do + expect(subject.logger).to receive(:info).with(ready_message) + subject.report_status + end + + it 'logs a warning message when not properly configured' do + # dsn not set + subject.configuration = Raven::Configuration.new - it 'logs not ready message if the config does not send in current environment' do - subject.configuration.silence_ready = false - subject.configuration.environments = ["production"] - expect(subject.logger).to receive(:info).with( - "Raven #{Raven::VERSION} configured not to capture errors: Not configured to send/capture in environment 'default'" - ) - subject.report_status + expect(subject.logger).to receive(:info).with(not_ready_message) + subject.report_status + end + + it 'logs nothing if "silence_ready" configuration is true' do + subject.configuration.silence_ready = true + expect(subject.logger).not_to receive(:info) + subject.report_status + end end - it 'logs nothing if "silence_ready" configuration is true' do - subject.configuration.silence_ready = true - expect(subject.logger).not_to receive(:info) - subject.report_status + context "when current environment is not included in environments" do + it "doesn't log any message" do + subject.configuration.silence_ready = false + subject.configuration.environments = ["production"] + expect(subject.logger).not_to receive(:info) + subject.report_status + end end end