Skip to content

Commit

Permalink
FIX: Stop env mutation to allow all chained loggers to have the same …
Browse files Browse the repository at this point in the history
…env (#110)

* FIX: Stop env mutation to allow all chained loggers to have the same env

* 2.3.4 doesn't like duping nil
  • Loading branch information
OsamaSayegh committed Mar 2, 2020
1 parent 3c7a2bb commit f404c51
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/logster/base_store.rb
Expand Up @@ -156,7 +156,7 @@ def report(severity, progname, msg, opts = {})
msg = truncate_message(msg)
message = Logster::Message.new(severity, progname, msg, opts[:timestamp], count: opts[:count])

env = opts[:env] || {}
env = opts[:env]&.dup || {}
backtrace = opts[:backtrace]
if Hash === env && env[:backtrace]
# Special - passing backtrace through env
Expand Down
12 changes: 12 additions & 0 deletions test/logster/test_base_store.rb
Expand Up @@ -176,4 +176,16 @@ def test_log_message_is_truncated_when_above_maximum_message_length
ensure
Logster.config.maximum_message_length = orig
end

def test_chained_loggers_shouldnt_mutate_env_passed_to_them
logger = Logster::Logger.new(@store)
other_store = Logster::TestStore.new
other_logger = Logster::Logger.new(other_store)
logger.chain(other_logger)
logger.add(Logger::WARN, "this is warning", '', { env: { backtrace: '11' } })
[@store, other_store].each do |store|
assert_equal('11', store.reported.first.backtrace)
refute_includes(store.reported.first.env.keys.map(&:to_sym), :backtrace)
end
end
end

0 comments on commit f404c51

Please sign in to comment.