From 1dc52639b289295240acd60a243831c53ee337dd Mon Sep 17 00:00:00 2001 From: Brian Durand Date: Thu, 30 Jul 2020 13:24:52 -0700 Subject: [PATCH] allow setting log tags with frozen hash --- CHANGELOG.md | 4 ++++ VERSION | 2 +- lib/lumberjack/logger.rb | 2 +- spec/logger_spec.rb | 7 +++++++ 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 07d6f63..f45f820 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.2.7 + +* Allow passing frozen hashes to `Logger.tag`. Tags passed to this method are now duplicated so the logger maintains it's own copy of the hash. + ## 1.2.6 * Fix Logger#tag so it only ads to the current block's logger tags instead of the global tags if called inside a `Logger#tag` block. diff --git a/VERSION b/VERSION index 3c43790..c04c650 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.2.6 +1.2.7 diff --git a/lib/lumberjack/logger.rb b/lib/lumberjack/logger.rb index bad536e..dc4536a 100644 --- a/lib/lumberjack/logger.rb +++ b/lib/lumberjack/logger.rb @@ -357,7 +357,7 @@ def tag(tags, &block) tags = Tags.stringify_keys(tags) thread_tags = thread_local_value(:lumberjack_logger_tags) if block - merged_tags = (thread_tags ? thread_tags.merge(tags) : tags) + merged_tags = (thread_tags ? thread_tags.merge(tags) : tags.dup) push_thread_local_value(:lumberjack_logger_tags, merged_tags, &block) elsif thread_tags thread_tags.merge!(tags) diff --git a/spec/logger_spec.rb b/spec/logger_spec.rb index 9939d64..a2255dd 100644 --- a/spec/logger_spec.rb +++ b/spec/logger_spec.rb @@ -523,6 +523,13 @@ line = output.string.chomp expect(line).to eq "message - 200 - [foo:cba]" end + + it "should work with a frozen hash" do + logger.tag({foo: "bar"}.freeze) + logger.tag(other: 1) do + expect(logger.tags).to eq("foo" => "bar", "other" => 1) + end + end end describe "log helper methods" do