Skip to content

Commit

Permalink
Fix RuntimeError in Lograge::Formatters::Graylog2
Browse files Browse the repository at this point in the history
Rubocop auto-corrected `data_clone.each.keys` to `data_clone.each_key`
which resulted in the following error:

     RuntimeError:
       can't add a new key into hash during iteration

To fix this, we use Hash#transform_keys (introduced in Ruby 2.5.0).
  • Loading branch information
ivy authored and Ivy Evans committed Nov 17, 2021
1 parent a7bf717 commit 2eeb433
Showing 1 changed file with 3 additions and 13 deletions.
16 changes: 3 additions & 13 deletions lib/lograge/formatters/graylog2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,10 @@ class Graylog2
include Lograge::Formatters::Helpers::MethodAndPath

def call(data)
# Cloning because we don't want to mess with the original when mutating keys.
data_clone = data.clone

base = {
short_message: short_message(data_clone)
}

# Add underscore to every key to follow GELF additional field syntax.
data_clone.each_key do |key|
data_clone[underscore_prefix(key)] = data_clone[key]
data_clone.delete(key)
end

data_clone.merge(base)
data.transform_keys { |k| underscore_prefix(k) }.merge(
short_message: short_message(data)
)
end

def underscore_prefix(key)
Expand Down

0 comments on commit 2eeb433

Please sign in to comment.