diff --git a/.gitignore b/.gitignore index 22a8ab89..3cbbe88e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ *.gem .bundle +.idea Gemfile.lock gemfiles/*.lock pkg/* diff --git a/.rubocop.yml b/.rubocop.yml index b33c71ee..0cd05747 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -11,5 +11,8 @@ AllCops: Metrics/AbcSize: Enabled: false +Metrics/ClassLength: + Enabled: false + Style/EmptyMethod: Enabled: false diff --git a/lib/lograge.rb b/lib/lograge.rb index b7376f5d..e5e91ca2 100644 --- a/lib/lograge.rb +++ b/lib/lograge.rb @@ -87,6 +87,10 @@ def ignore?(event) mattr_accessor :log_level self.log_level = :info + # Log level may be calculated based on http status by setting map_log_level = true + mattr_accessor :map_log_level + self.map_log_level = false + # The emitted log format # # Currently supported formats are> @@ -171,6 +175,7 @@ def set_lograge_log_options Lograge.custom_options = lograge_config.custom_options Lograge.before_format = lograge_config.before_format Lograge.log_level = lograge_config.log_level || :info + Lograge.map_log_level = lograge_config.map_log_level end def disable_rack_cache_verbose_output diff --git a/lib/lograge/log_subscriber.rb b/lib/lograge/log_subscriber.rb index ebb5b702..020a53ff 100644 --- a/lib/lograge/log_subscriber.rb +++ b/lib/lograge/log_subscriber.rb @@ -12,8 +12,9 @@ def process_action(event) payload = event.payload data = extract_request(event, payload) data = before_format(data, payload) + log_level = Lograge.map_log_level ? log_level_for_status(payload) : Lograge.log_level formatted_message = Lograge.formatter.call(data) - logger.send(Lograge.log_level, formatted_message) + logger.send(log_level, formatted_message) end def redirect_to(event) @@ -70,6 +71,19 @@ def extract_format(payload) end end + def log_level_for_status(payload) + status = extract_status(payload)[:status] + if status < 300 + :info + elsif status < 400 + :warn + elsif status < 500 + :error + else + :fatal + end + end + def extract_status(payload) if (status = payload[:status]) { status: status.to_i } diff --git a/lib/lograge/version.rb b/lib/lograge/version.rb index 91c208e8..a9da8f52 100644 --- a/lib/lograge/version.rb +++ b/lib/lograge/version.rb @@ -1,3 +1,3 @@ module Lograge - VERSION = '0.10.0'.freeze + VERSION = '0.11.0'.freeze end diff --git a/spec/lograge_logsubscriber_spec.rb b/spec/lograge_logsubscriber_spec.rb index 46785110..c191e7d2 100644 --- a/spec/lograge_logsubscriber_spec.rb +++ b/spec/lograge_logsubscriber_spec.rb @@ -32,8 +32,60 @@ ) end + let(:event_with_error) do + ActiveSupport::Notifications::Event.new( + 'process_action.action_controller', + Time.now, + Time.now, + 2, + status: 400, + controller: 'HomeController', + action: 'index', + format: 'application/json', + method: 'GET', + path: '/home?foo=bar', + params: event_params, + db_runtime: 0.02, + view_runtime: 0.01 + ) + end + before { Lograge.logger = logger } + context 'log_level' do + context 'with map_log_level = false (default)' do + it 'outputs ok response as INFO' do + expect(logger).to receive(:info) + expect(logger).to_not receive(:error) + subscriber.process_action(event) + end + + it 'outputs error response as INFO' do + expect(logger).to receive(:info) + expect(logger).to_not receive(:error) + subscriber.process_action(event_with_error) + end + end + + context 'with map_log_level = true' do + before do + Lograge.map_log_level = true + end + + it 'outputs ok response as INFO' do + expect(logger).to receive(:info) + expect(logger).to_not receive(:error) + subscriber.process_action(event) + end + + it 'outputs error response as ERROR' do + expect(logger).to_not receive(:info) + expect(logger).to receive(:error) + subscriber.process_action(event_with_error) + end + end + end + context 'with custom_options configured for cee output' do before do Lograge.formatter = ->(data) { "My test: #{data}" }