2008-01-05 08:31:04 -05:00
|
|
|
require 'abstract_unit'
|
2005-10-13 15:05:32 -04:00
|
|
|
require 'stringio'
|
2009-03-29 02:56:14 -04:00
|
|
|
require 'active_support/core_ext/logger'
|
2005-10-13 15:05:32 -04:00
|
|
|
|
|
|
|
class CleanLoggerTest < Test::Unit::TestCase
|
|
|
|
def setup
|
|
|
|
@out = StringIO.new
|
|
|
|
@logger = Logger.new(@out)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_format_message
|
|
|
|
@logger.error 'error'
|
|
|
|
assert_equal "error\n", @out.string
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_silence
|
|
|
|
# Without yielding self.
|
|
|
|
@logger.silence do
|
|
|
|
@logger.debug 'debug'
|
|
|
|
@logger.info 'info'
|
|
|
|
@logger.warn 'warn'
|
|
|
|
@logger.error 'error'
|
|
|
|
@logger.fatal 'fatal'
|
|
|
|
end
|
|
|
|
|
|
|
|
# Yielding self.
|
|
|
|
@logger.silence do |logger|
|
|
|
|
logger.debug 'debug'
|
|
|
|
logger.info 'info'
|
|
|
|
logger.warn 'warn'
|
|
|
|
logger.error 'error'
|
|
|
|
logger.fatal 'fatal'
|
|
|
|
end
|
|
|
|
|
2005-10-23 16:00:05 -04:00
|
|
|
# Silencer off.
|
|
|
|
Logger.silencer = false
|
|
|
|
@logger.silence do |logger|
|
|
|
|
logger.warn 'unsilenced'
|
|
|
|
end
|
|
|
|
Logger.silencer = true
|
|
|
|
|
|
|
|
assert_equal "error\nfatal\nerror\nfatal\nunsilenced\n", @out.string
|
2005-10-13 15:05:32 -04:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2007-02-24 19:07:54 -05:00
|
|
|
def test_datetime_format
|
|
|
|
@logger.formatter = Logger::Formatter.new
|
|
|
|
@logger.datetime_format = "%Y-%m-%d"
|
|
|
|
@logger.debug 'debug'
|
2007-06-25 14:22:31 -04:00
|
|
|
assert_equal "%Y-%m-%d", @logger.datetime_format
|
2007-02-24 19:07:54 -05:00
|
|
|
assert_match(/D, \[\d\d\d\d-\d\d-\d\d#\d+\] DEBUG -- : debug/, @out.string)
|
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2007-09-22 14:25:30 -04:00
|
|
|
def test_nonstring_formatting
|
|
|
|
an_object = [1, 2, 3, 4, 5]
|
|
|
|
@logger.debug an_object
|
|
|
|
assert_equal("#{an_object.inspect}\n", @out.string)
|
|
|
|
end
|
2005-10-13 15:05:32 -04:00
|
|
|
end
|