# $Id: test_logging.rb 34 2007-02-28 21:42:41Z tim_pease $ require 'test/setup.rb' require 'fileutils' module TestLogging class TestLogging < Test::Unit::TestCase include LoggingTestCase TMP = 'tmp' def setup super @levels = ::Logging::LEVELS @lnames = ::Logging::LNAMES FileUtils.rm_rf TMP FileUtils.mkdir(TMP) @fn = File.join(TMP, 'test.log') @glob = File.join(TMP, '*.log') end def teardown h = ::Logging::Repository.instance.instance_variable_get :@h h.values.each {|l| l.close if l.respond_to? :close} FileUtils.rm_rf TMP end def test_configure assert_raise(ArgumentError) {::Logging.configure 'blah.txt'} ::Logging.configure 'examples/logging.yaml' names = { 0 => 'DEB', 1 => 'INF', 2 => 'PRT', 3 => 'WRN', 4 => 'ERR', 5 => 'FAT' } assert_equal names, ::Logging::LNAMES assert_equal :inspect, ::Logging::OBJ_FORMAT assert_equal 3, ::Logging::Logger.root.level # verify the appenders h = ::Logging::Appender.instance_variable_get :@appenders assert_equal ['logfile', 'stderr'], h.keys.sort # start with the File appender logfile = ::Logging::Appender['logfile'] assert_instance_of ::Logging::Appenders::File, logfile assert_equal 0, logfile.level assert_equal 'temp.log', logfile.instance_variable_get(:@fn) layout = logfile.layout assert_instance_of ::Logging::Layouts::Pattern, layout assert_equal '[%d] %l %c : %m\\n', layout.pattern assert_equal 'to_s', layout.date_method assert_nil layout.date_pattern # and now the Stderr appender stderr = ::Logging::Appender['stderr'] assert_instance_of ::Logging::Appenders::Stderr, stderr assert_equal 0, stderr.level layout = stderr.layout assert_instance_of ::Logging::Layouts::Basic, layout # verify the loggers h = ::Logging::Repository.instance.instance_variable_get :@h assert_equal 3, h.length # mylogger mylogger = ::Logging::Logger['mylogger'] assert_equal 0, mylogger.level assert_equal false, mylogger.additive assert_equal false, mylogger.trace appenders = mylogger.instance_variable_get :@appenders assert_equal 2, appenders.length assert_equal ['logfile', 'stderr'], appenders.map {|a| a.name}.sort # yourlogger yourlogger = ::Logging::Logger['yourlogger'] assert_equal 1, yourlogger.level assert_equal true, yourlogger.additive assert_equal false, yourlogger.trace appenders = yourlogger.instance_variable_get :@appenders assert_equal 2, appenders.length assert_equal ['logfile', 'stderr'], appenders.map {|a| a.name}.sort # cleanup File.delete('temp.log') if File.exist?('temp.log') end def test_logger assert_raise(TypeError) {::Logging.logger []} logger = ::Logging.logger STDOUT assert_match %r/\A-?\d+\z/, logger.name assert_same logger, ::Logging.logger(STDOUT) logger.close assert !STDOUT.closed? assert !File.exist?(@fn) fd = File.new @fn, 'w' logger = ::Logging.logger fd, 2, 100 assert_equal @fn, logger.name logger.debug 'this is a debug message' logger.warn 'this is a warning message' logger.error 'and now we should have over 100 bytes of data ' + 'in the log file' logger.info 'but the log file should not roll since we provided ' + 'a file descriptor -- not a file name' logger.close assert fd.closed? assert File.exist?(@fn) assert_equal 1, Dir.glob(@glob).length FileUtils.rm_f @fn assert !File.exist?(@fn) logger = ::Logging.logger @fn, 2, 100 assert File.exist?(@fn) assert_equal @fn, logger.name logger.debug 'this is a debug message' logger.warn 'this is a warning message' logger.error 'and now we should have over 100 bytes of data ' + 'in the log file' logger.info 'but the log file should not roll since we provided ' + 'a file descriptor -- not a file name' logger.close assert_equal 3, Dir.glob(@glob).length end def test_define_levels_default empty = {} assert_equal empty, @levels assert_equal empty, @lnames assert_same false, ::Logging.const_defined?(:MAX_LEVEL_LENGTH) ::Logging::Repository.instance assert_equal 5, @levels.length assert_equal 5, @lnames.length assert_equal 5, ::Logging::MAX_LEVEL_LENGTH assert_equal 0, @levels['debug'] assert_equal 1, @levels['info'] assert_equal 2, @levels['warn'] assert_equal 3, @levels['error'] assert_equal 4, @levels['fatal'] assert_equal 'DEBUG', @lnames[0] assert_equal 'INFO', @lnames[1] assert_equal 'WARN', @lnames[2] assert_equal 'ERROR', @lnames[3] assert_equal 'FATAL', @lnames[4] end def test_define_levels_special empty = {} assert_equal empty, @levels assert_equal empty, @lnames assert_same false, ::Logging.const_defined?(:MAX_LEVEL_LENGTH) assert_raise(ArgumentError) {::Logging.define_levels(1, 2, 3, 4)} ::Logging.define_levels :one, 'two', :THREE, 'FoUr', :sIx assert_equal 5, @levels.length assert_equal 5, @lnames.length assert_equal 5, ::Logging::MAX_LEVEL_LENGTH assert_equal 0, @levels['one'] assert_equal 1, @levels['two'] assert_equal 2, @levels['three'] assert_equal 3, @levels['four'] assert_equal 4, @levels['six'] assert_equal 'ONE', @lnames[0] assert_equal 'TWO', @lnames[1] assert_equal 'THREE', @lnames[2] assert_equal 'FOUR', @lnames[3] assert_equal 'SIX', @lnames[4] end def test_define_levels_all_off empty = {} assert_equal empty, @levels assert_equal empty, @lnames assert_same false, ::Logging.const_defined?(:MAX_LEVEL_LENGTH) ::Logging.define_levels %w(a b all c off d) assert_equal 4, @levels.length assert_equal 4, @lnames.length assert_equal 1, ::Logging::MAX_LEVEL_LENGTH assert_equal 0, @levels['a'] assert_equal 1, @levels['b'] assert_equal 2, @levels['c'] assert_equal 3, @levels['d'] assert_equal 'A', @lnames[0] assert_equal 'B', @lnames[1] assert_equal 'C', @lnames[2] assert_equal 'D', @lnames[3] end def test_format_as assert_equal false, ::Logging.const_defined?('OBJ_FORMAT') assert_raises(ArgumentError) {::Logging.format_as 'bob'} assert_raises(ArgumentError) {::Logging.format_as String} assert_raises(ArgumentError) {::Logging.format_as :what?} remove_const = lambda do |const| ::Logging.class_eval {remove_const const if const_defined? const} end ::Logging.format_as :string assert ::Logging.const_defined?('OBJ_FORMAT') assert_equal :string, ::Logging::OBJ_FORMAT remove_const[:OBJ_FORMAT] ::Logging.format_as :inspect assert ::Logging.const_defined?('OBJ_FORMAT') assert_equal :inspect, ::Logging::OBJ_FORMAT remove_const[:OBJ_FORMAT] ::Logging.format_as :yaml assert ::Logging.const_defined?('OBJ_FORMAT') assert_equal :yaml, ::Logging::OBJ_FORMAT remove_const[:OBJ_FORMAT] ::Logging.format_as 'string' assert ::Logging.const_defined?('OBJ_FORMAT') assert_equal :string, ::Logging::OBJ_FORMAT remove_const[:OBJ_FORMAT] ::Logging.format_as 'inspect' assert ::Logging.const_defined?('OBJ_FORMAT') assert_equal :inspect, ::Logging::OBJ_FORMAT remove_const[:OBJ_FORMAT] ::Logging.format_as 'yaml' assert ::Logging.const_defined?('OBJ_FORMAT') assert_equal :yaml, ::Logging::OBJ_FORMAT remove_const[:OBJ_FORMAT] end end # class TestLogging end # module TestLogging # EOF