aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2012-09-26 10:08:20 -0700
committerJeremy Kemper <jeremy@bitsweat.net>2012-09-26 10:16:40 -0700
commitd36f57f430c011e889c7d45335beea4ea0dc9769 (patch)
tree34bfd79d41fa1ede8f0e848348fe58bd1f1bcfcd /activesupport
parentd1cbcd781bdb974c4232c3d63e3d1b4d1f9c4bd5 (diff)
downloadrails-d36f57f430c011e889c7d45335beea4ea0dc9769.tar.gz
rails-d36f57f430c011e889c7d45335beea4ea0dc9769.tar.bz2
rails-d36f57f430c011e889c7d45335beea4ea0dc9769.zip
Add logger.push_tags and .pop_tags to complement logger.tagged
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/CHANGELOG.md14
-rw-r--r--activesupport/lib/active_support/tagged_logging.rb42
-rw-r--r--activesupport/test/tagged_logging_test.rb17
3 files changed, 59 insertions, 14 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md
index 05a573076e..e8b7392777 100644
--- a/activesupport/CHANGELOG.md
+++ b/activesupport/CHANGELOG.md
@@ -1,5 +1,19 @@
## Rails 4.0.0 (unreleased) ##
+* Add logger.push_tags and .pop_tags to complement logger.tagged:
+
+ class Job
+ def before
+ Rails.logger.push_tags :jobs, self.class.name
+ end
+
+ def after
+ Rails.logger.pop_tags 2
+ end
+ end
+
+ *Jeremy Kemper*
+
* Allow delegation to the class using the `:class` keyword, replacing
`self.class` usage:
diff --git a/activesupport/lib/active_support/tagged_logging.rb b/activesupport/lib/active_support/tagged_logging.rb
index 6d1ecabcbd..8af6fa4ba7 100644
--- a/activesupport/lib/active_support/tagged_logging.rb
+++ b/activesupport/lib/active_support/tagged_logging.rb
@@ -20,7 +20,24 @@ module ActiveSupport
super(severity, timestamp, progname, "#{tags_text}#{msg}")
end
- def clear!
+ def tagged(*tags)
+ new_tags = push_tags *tags
+ yield self
+ ensure
+ pop_tags new_tags.size
+ end
+
+ def push_tags(*tags)
+ tags.flatten.reject(&:blank?).tap do |new_tags|
+ current_tags.concat new_tags
+ end
+ end
+
+ def pop_tags(size = 1)
+ current_tags.pop size
+ end
+
+ def clear_tags!
current_tags.clear
end
@@ -29,12 +46,12 @@ module ActiveSupport
end
private
- def tags_text
- tags = current_tags
- if tags.any?
- tags.collect { |tag| "[#{tag}] " }.join
+ def tags_text
+ tags = current_tags
+ if tags.any?
+ tags.collect { |tag| "[#{tag}] " }.join
+ end
end
- end
end
def self.new(logger)
@@ -42,17 +59,14 @@ module ActiveSupport
logger.extend(self)
end
- def tagged(*new_tags)
- tags = formatter.current_tags
- new_tags = new_tags.flatten.reject(&:blank?)
- tags.concat new_tags
- yield self
- ensure
- tags.pop(new_tags.size)
+ delegate :push_tags, :pop_tags, :clear_tags!, to: :formatter
+
+ def tagged(*tags)
+ formatter.tagged(*tags) { yield self }
end
def flush
- formatter.clear!
+ clear_tags!
super if defined?(super)
end
end
diff --git a/activesupport/test/tagged_logging_test.rb b/activesupport/test/tagged_logging_test.rb
index 43cf1a8e4f..fa045baa28 100644
--- a/activesupport/test/tagged_logging_test.rb
+++ b/activesupport/test/tagged_logging_test.rb
@@ -29,6 +29,23 @@ class TaggedLoggingTest < ActiveSupport::TestCase
assert_equal "[BCX] [Jason] [New] Funky time\n", @output.string
end
+ test "tagged are flattened" do
+ @logger.tagged("BCX", %w(Jason New)) { @logger.info "Funky time" }
+ assert_equal "[BCX] [Jason] [New] Funky time\n", @output.string
+ end
+
+ test "push and pop tags directly" do
+ assert_equal %w(A B C), @logger.push_tags('A', ['B', ' ', ['C']])
+ @logger.info 'a'
+ assert_equal %w(C), @logger.pop_tags
+ @logger.info 'b'
+ assert_equal %w(B), @logger.pop_tags(1)
+ @logger.info 'c'
+ assert_equal [], @logger.clear_tags!
+ @logger.info 'd'
+ assert_equal "[A] [B] [C] a\n[A] [B] b\n[A] c\nd\n", @output.string
+ end
+
test "does not strip message content" do
@logger.info " Hello"
assert_equal " Hello\n", @output.string