Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Provide access to logger instance within TaggedLogging blocks

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...
commit e0ee14ed2305a0b2e6ee53c5d5a98450d2f534e4 1 parent 24d244c
@FND FND authored
View
2  activesupport/lib/active_support/tagged_logging.rb
@@ -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
View
7 activesupport/test/tagged_logging_test.rb
@@ -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
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.