super
class << @logdev
def shift_log_period(now)
- postfix = previous_period_end(now).strftime("%Y%m%d") # YYYYMMDD
- age_file = File.join(
- File.dirname(@filename),
- postfix[0..3], # YYYY
- postfix[4..5], # MM
- postfix[6..7], # DD
- File.basename(@filename))
- if FileTest.exist?(age_file)
- raise RuntimeError.new("'#{ age_file }' already exists.")
- end
+ age_file = age_file_name(now)
+ move_age_file_in_the_way(age_file)
+
unless FileTest.directory?(File.dirname(age_file))
begin
FileUtils.mkdir_p File.dirname(age_file)
rescue
+ @dev.write("[ERROR] Could not create a directory: %s\n" % [File.dirname(age_file)])
raise RuntimeError.new("Could not create a directory: %s" % [File.dirname(age_file)])
end
end
@dev.close
- File.rename("#{@filename}", age_file)
+ rename_file(@filename, age_file)
@dev = create_logfile(@filename)
return true
end
+
+ def age_file_name(time)
+ postfix = previous_period_end(time).strftime("%Y%m%d") # YYYYMMDD
+ age_file = File.join(
+ File.dirname(@filename),
+ postfix[0..3], # YYYY
+ postfix[4..5], # MM
+ postfix[6..7], # DD
+ File.basename(@filename))
+ return age_file
+ end
+
+ def age_file_exists?(age_file)
+ return FileTest.exist?(age_file)
+ end
+
+ def rename_file(old_file, new_file)
+ File.rename(old_file, new_file)
+ end
+
+ def move_age_file_in_the_way(age_file)
+ return unless age_file_exists?(age_file)
+
+ now = Time.now
+ new_file = "%s.%s%06d" % [age_file, now.strftime("%Y%m%d%H%M%S"), now.usec]
+ @dev.write("[WARN] An existing '#{age_file}' is beeing moved to '#{new_file}'\n")
+ rename_file(age_file, new_file)
+ end
end
end
-end
+
+end # class Logger
class Formatter < ::Logger::Formatter
def initialize
--- /dev/null
+$:.unshift File.join(File.dirname(__FILE__), "..")
+$topdir = File.expand_path File.dirname(__FILE__)
+require 'test/unit'
+require 'shogi_server'
+
+class TestableLogger < ShogiServer::Logger
+ def initialize(logdev, shift_age = 0, shift_size = 1048576)
+ super
+ class << @logdev
+ attr_accessor :return_age_file_exists
+ def age_file_exists?(age_file)
+ return @return_age_file_exists || false
+ end
+
+ attr_reader :result_rename_file
+ def rename_file(old_file, new_file)
+ @result_rename_file ||= []
+ @result_rename_file << [old_file, new_file]
+ end
+ end
+ end
+ attr_reader :logdev
+
+end
+
+class TestLogger < Test::Unit::TestCase
+ def setup
+ filename = File.join($topdir, "TC_logger_test.log")
+ @logger = TestableLogger.new(filename, "daily")
+ @logger.formatter = ShogiServer::Formatter.new
+ @logger.level = TestableLogger::DEBUG
+ @logger.datetime_format = "%Y-%m-%d %H:%M:%S"
+ end
+
+ def test_dummy
+ assert true
+ end
+
+ def test_age_file_name
+ time = Time.mktime(2010, 7, 25, 23, 59, 59)
+ assert_equal File.expand_path(File.join($topdir, "2010", "07", "24", "TC_logger_test.log")),
+ @logger.logdev.age_file_name(time)
+ end
+
+ def test_age_file_exists
+ assert !@logger.logdev.age_file_exists?(nil)
+ @logger.logdev.return_age_file_exists = true
+ assert @logger.logdev.age_file_exists?(nil)
+ end
+
+ def test_rename_file
+ @logger.logdev.rename_file("old", "new")
+ assert_equal [["old", "new"]], @logger.logdev.result_rename_file
+ end
+
+ def test_move_age_file_in_the_way
+ assert !@logger.logdev.age_file_exists?(nil)
+ @logger.logdev.move_age_file_in_the_way("hoge.log")
+ assert_nil @logger.logdev.result_rename_file
+
+ @logger.logdev.return_age_file_exists = true
+ @logger.logdev.move_age_file_in_the_way("hoge.log")
+ assert_equal 1, @logger.logdev.result_rename_file.size
+ assert_equal "hoge.log", @logger.logdev.result_rename_file.first.first
+ end
+
+ def test_log_info
+ @logger.info("test_log_info")
+ assert true
+ end
+
+ def test_shift_log_period
+ @logger.info("test_shift_log_period")
+ now = Time.mktime(2010, 7, 25, 23, 59, 59)
+ @logger.logdev.shift_log_period(now)
+ assert_equal [["/home/daigo/rubyprojects/shogi-server/test/TC_logger_test.log",
+ "/home/daigo/rubyprojects/shogi-server/test/2010/07/24/TC_logger_test.log"]],
+ @logger.logdev.result_rename_file
+ end
+end