OSDN Git Service

* [shogi-server]
authorbeatles <beatles@b8c68f68-1e22-0410-b08e-880e1f8202b4>
Sat, 13 Dec 2008 13:37:45 +0000 (13:37 +0000)
committerbeatles <beatles@b8c68f68-1e22-0410-b08e-880e1f8202b4>
Sat, 13 Dec 2008 13:37:45 +0000 (13:37 +0000)
  - A lotated log file is moved to $topdir/YYYY/MM/DD.
    (Closes: #14245)

changelog
shogi-server
shogi_server.rb

index 376c771..9a4f330 100644 (file)
--- a/changelog
+++ b/changelog
@@ -4,6 +4,8 @@
          - Improved an existance check and etc. of directories specified
            by command line options, expecially in case of the daemon mode. 
            (Closes: #14244)
+         - A lotated log file is moved to $topdir/YYYY/MM/DD.
+           (Closes: #14245)
 
 2008-11-27 Daigo Moriwaki <daigo at debian dot org>
 
index 8979a16..0533040 100755 (executable)
@@ -277,7 +277,7 @@ def login_loop(client)
 end
 
 def setup_logger(log_file)
-  logger = Logger.new(log_file, 'daily')
+  logger = ShogiServer::Logger.new(log_file, 'daily')
   logger.formatter = ShogiServer::Formatter.new
   logger.level = $DEBUG ? Logger::DEBUG : Logger::INFO  
   logger.datetime_format = "%Y-%m-%d %H:%M:%S"
index 157ebe5..4632c64 100644 (file)
@@ -62,6 +62,37 @@ def reload
 end
 module_function :reload
 
+class Logger < ::Logger
+  def initialize(logdev, shift_age = 0, shift_size = 1048576)
+    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
+        unless FileTest.directory?(File.dirname(age_file))
+          begin
+            FileUtils.mkdir_p File.dirname(age_file)
+          rescue
+            raise RuntimeError.new("Could not create a directory: %s" % [File.dirname(age_file)])
+          end
+        end
+        @dev.close
+        File.rename("#{@filename}", age_file)
+        @dev = create_logfile(@filename)
+        return true
+      end
+    end
+  end
+end
+
 class Formatter < ::Logger::Formatter
   def initialize
     super