Skip to content

Commit

Permalink
Provide access to logger instance within TaggedLogging blocks
Browse files Browse the repository at this point in the history
this improves encapsulation, simplifying occurrences like the following:

    Rails.logger.tagged("DEBUG") { Rails.logger.debug(msg) }

... by removing the need to rely on (i.e. repeat) outer variables:

    Rails.logger.tagged("DEBUG") { |logger| logger.debug(msg) }
  • Loading branch information
FND committed Mar 19, 2012
1 parent 24d244c commit e0ee14e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
2 changes: 1 addition & 1 deletion activesupport/lib/active_support/tagged_logging.rb
Expand Up @@ -45,7 +45,7 @@ def tagged(*new_tags)
tags = formatter.current_tags
new_tags = new_tags.flatten.reject(&:blank?)
tags.concat new_tags
yield
yield self
ensure
tags.pop(new_tags.size)
end
Expand Down
7 changes: 6 additions & 1 deletion activesupport/test/tagged_logging_test.rb
Expand Up @@ -18,7 +18,7 @@ def flush(*)
@logger.tagged("BCX") { @logger.info "Funky time" }
assert_equal "[BCX] Funky time\n", @output.string
end

test "tagged twice" do
@logger.tagged("BCX") { @logger.tagged("Jason") { @logger.info "Funky time" } }
assert_equal "[BCX] [Jason] Funky time\n", @output.string
Expand All @@ -29,6 +29,11 @@ def flush(*)
assert_equal "[BCX] [Jason] [New] Funky time\n", @output.string
end

test "provides access to the logger instance" do
@logger.tagged("BCX") { |logger| logger.info "Funky time" }
assert_equal "[BCX] Funky time\n", @output.string
end

test "tagged once with blank and nil" do
@logger.tagged(nil, "", "New") { @logger.info "Funky time" }
assert_equal "[New] Funky time\n", @output.string
Expand Down

0 comments on commit e0ee14e

Please sign in to comment.