summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authornahi <nahi@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2005-09-13 13:13:41 +0000
committernahi <nahi@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2005-09-13 13:13:41 +0000
commit5d0bf5623513d1610b51a479776a21359c82db58 (patch)
tree78956d6e5e778953e7c0d1661fb2c9bcb1895aeb /test
parent9a7b502846f473540f1229496d8d9bc80187e225 (diff)
* lib/logger.rb (Logger): added formatter accessor to logger for
dictating the way in which the logger should format the messages it displays. Thanks to Nicholas Seckar (cf. [ruby-talk:153391]) and Daniel Berger. * lib/logger.rb (Logger): added VERSION constant. * lib/logger.rb: removed document for LogDevice. It is an implementation detail and is not a public interface. * test/logger/test_logger.rb: added tests. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@9151 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test')
-rw-r--r--test/logger/test_logger.rb34
1 files changed, 31 insertions, 3 deletions
diff --git a/test/logger/test_logger.rb b/test/logger/test_logger.rb
index 7f11a30..6fba2bc 100644
--- a/test/logger/test_logger.rb
+++ b/test/logger/test_logger.rb
@@ -36,13 +36,17 @@ class TestLogger < Test::Unit::TestCase
end
def log(logger, msg_id, *arg, &block)
+ Log.new(log_raw(logger, msg_id, *arg, &block))
+ end
+
+ def log_raw(logger, msg_id, *arg, &block)
logdev = Tempfile.new(File.basename(__FILE__) + '.log')
logger.instance_eval { @logdev = Logger::LogDevice.new(logdev) }
logger.__send__(msg_id, *arg, &block)
logdev.open
msg = logdev.read
logdev.close
- Log.new(msg)
+ msg
end
def test_level
@@ -98,6 +102,32 @@ class TestLogger < Test::Unit::TestCase
assert_match(/^$/, log.datetime)
end
+ def test_formatter
+ dummy = STDERR
+ logger = Logger.new(dummy)
+ # default
+ log = log(logger, :info, "foo")
+ assert_equal("foo\n", log.msg)
+ # config
+ logger.formatter = proc { |severity, timestamp, progname, msg|
+ "#{severity}:#{msg}\n\n"
+ }
+ line = log_raw(logger, :info, "foo")
+ assert_equal("INFO:foo\n\n", line)
+ # recover
+ logger.formatter = nil
+ log = log(logger, :info, "foo")
+ assert_equal("foo\n", log.msg)
+ # again
+ o = Object.new
+ def o.call(severity, timestamp, progname, msg)
+ "<<#{severity}-#{msg}>>\n"
+ end
+ logger.formatter = o
+ line = log_raw(logger, :info, "foo")
+ assert_equal("<<INFO-foo>>\n", line)
+ end
+
def test_initialize
logger = Logger.new(STDERR)
assert_nil(logger.progname)
@@ -301,7 +331,6 @@ class TestLogDevice < Test::Unit::TestCase
assert(!File.exist?(logfile3))
logger.error("0" * 15)
assert(!File.exist?(logfile3))
- logger.close
File.unlink(logfile)
File.unlink(logfile0)
File.unlink(logfile1)
@@ -337,7 +366,6 @@ class TestLogDevice < Test::Unit::TestCase
assert(!File.exist?(logfile3))
logger.error("0" * 15)
assert(!File.exist?(logfile3))
- logger.close
File.unlink(logfile)
File.unlink(logfile0)
File.unlink(logfile1)