Project

General

Profile

Actions

Bug #7303

closed

Logger fails on log rotation in Windows (log shifting failed. Permission denied)

Added by pink-ink (Herman Munster) about 12 years ago. Updated over 5 years ago.

Status:
Closed
Target version:
-
ruby -v:
ruby 1.9.3p0 (2011-10-30) [i386-mingw32]
Backport:
[ruby-core:49036]

Description

I have the problem that the logger fails in rotating the log file (daily) with Ruby 1.9.3 in Windows 7.

The cause is, that the log file is open and shall be renamed.
This results in an error "log shifting failed. Permission denied".

Log rotating will work, if the log file is copied to the age_file and then the logfile is cleaned by using logdev.truncate(0).

My changes to logger.rb are (remove 3 lines, add 2 lines):

    def shift_log_period(period_end)
      postfix = period_end.strftime("%Y%m%d")# YYYYMMDD
      age_file = "#{@filename}.#{postfix}"
      if FileTest.exist?(age_file)
        # try to avoid filename crash caused by Timestamp change.
        idx = 0
        # .99 can be overridden; avoid too much file search with 'loop do'
        while idx < 100
          idx += 1
          age_file = "#{@filename}.#{postfix}.#{idx}"
          break unless FileTest.exist?(age_file)
        end
      end
-     @dev.close rescue nil
-     File.rename("#{@filename}", age_file)
-     @dev = create_logfile(@filename)
+     FileUtils.cp(@filename, age_file)
+     reset_logfile(@dev)  # see below for this new method
      return true

I've added a new method to clean the open logfile and add a log header:

    def reset_logfile(logdev)
      logdev.truncate( 0 )
      logdev.sync = true
      add_log_header(logdev)
    end

The change above (copy/clean instead of rename) should also be
applied to the method "shift_log_age", same problem here.

I don't know how to bring this code into standard ruby sources.
Please somebody change the code, so the bug will be fixed.

(I found this bug also described in the old RubyForge archive: http://rubyforge.org/tracker/?group_id=426&atid=1698&func=detail&aid=19709)

Updated by pink-ink (Herman Munster) about 12 years ago

Addition to my previous post:
require 'fileutils' is needed at the beginning of logger.rb, if using FileUtils.cp().

Updated by usa (Usaku NAKAMURA) about 12 years ago

  • Status changed from Open to Assigned

Updated by hsbt (Hiroshi SHIBATA) over 5 years ago

  • Description updated (diff)
  • Assignee changed from nahi (Hiroshi Nakamura) to sonots (Naotoshi Seo)

Updated by sonots (Naotoshi Seo) over 5 years ago

It looks the codes around specified are fairly changed from 1.9.3 now. This problem should already have been fixed.
Let me close once. If anyone finds same issue with the latest ruby, please let me know.

Actions #5

Updated by sonots (Naotoshi Seo) over 5 years ago

  • Status changed from Assigned to Closed
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0