Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic log level based on http status of request #276

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
@@ -1,5 +1,6 @@
*.gem
.bundle
.idea
Gemfile.lock
gemfiles/*.lock
pkg/*
3 changes: 3 additions & 0 deletions .rubocop.yml
Expand Up @@ -11,5 +11,8 @@ AllCops:
Metrics/AbcSize:
Enabled: false

Metrics/ClassLength:
Enabled: false

Style/EmptyMethod:
Enabled: false
5 changes: 5 additions & 0 deletions lib/lograge.rb
Expand Up @@ -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>
Expand Down Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion lib/lograge/log_subscriber.rb
Expand Up @@ -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)
Expand Down Expand Up @@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion lib/lograge/version.rb
@@ -1,3 +1,3 @@
module Lograge
VERSION = '0.10.0'.freeze
VERSION = '0.11.0'.freeze
end
52 changes: 52 additions & 0 deletions spec/lograge_logsubscriber_spec.rb
Expand Up @@ -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}" }
Expand Down