mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
TaggedLogging to return a new logger instance
This commit is contained in:
parent
4cc1c14493
commit
62fba74932
3 changed files with 34 additions and 10 deletions
|
@ -1,3 +1,7 @@
|
||||||
|
* Changed `ActiveSupport::TaggedLogging.new` to return a new logger instance instead of mutating the one received as parameter.
|
||||||
|
|
||||||
|
*Thierry Joyal*
|
||||||
|
|
||||||
* Add `ActiveSupport::Duration#before` and `#after` as aliases for `#until` and `#since`
|
* Add `ActiveSupport::Duration#before` and `#after` as aliases for `#until` and `#since`
|
||||||
|
|
||||||
These read more like English and require less mental gymnastics to read and write.
|
These read more like English and require less mental gymnastics to read and write.
|
||||||
|
|
|
@ -57,8 +57,15 @@ module ActiveSupport
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.new(logger)
|
def self.new(logger)
|
||||||
|
logger = logger.dup
|
||||||
|
|
||||||
|
if logger.formatter
|
||||||
|
logger.formatter = logger.formatter.dup
|
||||||
|
else
|
||||||
# Ensure we set a default formatter so we aren't extending nil!
|
# Ensure we set a default formatter so we aren't extending nil!
|
||||||
logger.formatter ||= ActiveSupport::Logger::SimpleFormatter.new
|
logger.formatter = ActiveSupport::Logger::SimpleFormatter.new
|
||||||
|
end
|
||||||
|
|
||||||
logger.formatter.extend Formatter
|
logger.formatter.extend Formatter
|
||||||
logger.extend(self)
|
logger.extend(self)
|
||||||
end
|
end
|
||||||
|
|
|
@ -17,9 +17,10 @@ class TaggedLoggingTest < ActiveSupport::TestCase
|
||||||
test "sets logger.formatter if missing and extends it with a tagging API" do
|
test "sets logger.formatter if missing and extends it with a tagging API" do
|
||||||
logger = Logger.new(StringIO.new)
|
logger = Logger.new(StringIO.new)
|
||||||
assert_nil logger.formatter
|
assert_nil logger.formatter
|
||||||
ActiveSupport::TaggedLogging.new(logger)
|
|
||||||
assert_not_nil logger.formatter
|
other_logger = ActiveSupport::TaggedLogging.new(logger)
|
||||||
assert logger.formatter.respond_to?(:tagged)
|
assert_not_nil other_logger.formatter
|
||||||
|
assert other_logger.formatter.respond_to?(:tagged)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "tagged once" do
|
test "tagged once" do
|
||||||
|
@ -80,16 +81,28 @@ class TaggedLoggingTest < ActiveSupport::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
test "keeps each tag in their own instance" do
|
test "keeps each tag in their own instance" do
|
||||||
@other_output = StringIO.new
|
other_output = StringIO.new
|
||||||
@other_logger = ActiveSupport::TaggedLogging.new(MyLogger.new(@other_output))
|
other_logger = ActiveSupport::TaggedLogging.new(MyLogger.new(other_output))
|
||||||
@logger.tagged("OMG") do
|
@logger.tagged("OMG") do
|
||||||
@other_logger.tagged("BCX") do
|
other_logger.tagged("BCX") do
|
||||||
@logger.info "Cool story"
|
@logger.info "Cool story"
|
||||||
@other_logger.info "Funky time"
|
other_logger.info "Funky time"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
assert_equal "[OMG] Cool story\n", @output.string
|
assert_equal "[OMG] Cool story\n", @output.string
|
||||||
assert_equal "[BCX] Funky time\n", @other_output.string
|
assert_equal "[BCX] Funky time\n", other_output.string
|
||||||
|
end
|
||||||
|
|
||||||
|
test "does not share the same formatter instance of the original logger" do
|
||||||
|
other_logger = ActiveSupport::TaggedLogging.new(@logger)
|
||||||
|
|
||||||
|
@logger.tagged("OMG") do
|
||||||
|
other_logger.tagged("BCX") do
|
||||||
|
@logger.info "Cool story"
|
||||||
|
other_logger.info "Funky time"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
assert_equal "[OMG] Cool story\n[BCX] Funky time\n", @output.string
|
||||||
end
|
end
|
||||||
|
|
||||||
test "cleans up the taggings on flush" do
|
test "cleans up the taggings on flush" do
|
||||||
|
|
Loading…
Reference in a new issue