Skip to content

Commit c995180

Browse files
authored
Merge pull request #12 from hhoopes/hlh/rails-7.1-splatting
Update TaggedHashFormatter to handle nil edge cases gracefully
2 parents e1b53b7 + b196d02 commit c995180

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
Bug Fixes:
66

7-
* None
7+
* Formatter is now more defensive about incoming nil and array-of-nil values, after changes with Rails 7.x Rack::Logger identified the gap
88

99
Enhancements:
1010

lib/fluent_logger_rails/tagged_hash_formatter.rb

+6-3
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,13 @@ def format_severity(severity)
4343
end
4444

4545
def tagged(*tags)
46-
add_tags(*tags)
47-
yield self
46+
tags = tags.flatten.compact if tags.is_a?(Array)
47+
48+
add_tags(*tags) if tags
49+
50+
yield(self)
4851
ensure
49-
remove_tags(*tags)
52+
remove_tags(*tags) if tags
5053
end
5154

5255
def add_tags(*tags)

spec/integration/fluent_logger_rails/tagged_hash_formatter_spec.rb

+40
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,46 @@
3333
end
3434
end
3535

36+
describe '#tagged' do
37+
let(:tags) { nil }
38+
39+
context 'nil tag' do
40+
it 'does not attempt to process the tags' do
41+
expect(formatter).not_to(receive(:remove_tags).with(anything))
42+
43+
formatter.tagged(tags) { expect(formatter.current_tags).to(eq({ tags: [] })) }
44+
end
45+
end
46+
47+
context 'with a nested array of nil tags' do
48+
let(:tags) { [nil] }
49+
50+
it 'does not attempt to process the tags' do
51+
expect(formatter).not_to(receive(:remove_tags).with(anything))
52+
53+
formatter.tagged([tags]) { expect(formatter.current_tags).to(eq({ tags: [] })) }
54+
end
55+
end
56+
57+
context 'with a string tag' do
58+
let(:tags) { 'tag' }
59+
60+
it 'adds the tag' do
61+
formatter.tagged(tags) { expect(formatter.current_tags).to(eq({ tags: [tags] })) }
62+
end
63+
end
64+
65+
context 'with a hash' do
66+
let(:tags) { { port: 80, host: '127.0.0.1' } }
67+
68+
it 'adds the tags' do
69+
formatter.tagged(**tags) do
70+
expect(formatter.current_tags).to(eq(tags))
71+
end
72+
end
73+
end
74+
end
75+
3676
describe '#add_tags' do
3777
context 'for a hash' do
3878
before { formatter.add_tags(host: '127.0.0.1', port: '80')}

0 commit comments

Comments
 (0)