log4r 1.0.5 的DateFileOutPutter有个bug

当指定了输出文件后log日志并没有按照追加的形式记录,而是不断的被覆盖掉了,而他的父类FileOutputter反而能按追加的形式产生文件。
看了一下源码,少了对已有文件的处理,例如日志的路径、名称等

1.0.5
                                                                      
   
require "log4r/outputter/fileoutputter"                                   
require "log4r/staticlogger"                                              
                                                                          
module Log4r                                                              
                                                                          
  # Additional hash arguments are:                                        
  #                                                                       
  # [<tt>:dirname</tt>]         Directory of the log file                 
  # [<tt>:date_pattern</tt>]    Time.strftime format string (default is "%
                                                                          
  class DateFileOutputter < FileOutputter                                 
    DEFAULT_DATE_FMT = "%Y-%m-%d"                                         
                                                                          
    def initialize(_name, hash={})                                        
      @DatePattern = (hash[:date_pattern] or hash['date_pattern'] or      
                      DEFAULT_DATE_FMT)                                   
      @DateStamp = Time.now.strftime( @DatePattern);                      
      _dirname = (hash[:dirname] or hash['dirname'])                      
      # hash[:dirname] masks hash[:filename]                              
      if _dirname                                                         
        if not FileTest.directory?( _dirname)                             
          raise StandardError, "'#{_dirname}' must be a valid directory", 
        end                                                               
        @filebase = File.basename( $0, '.rb') + ".log"                    
      else                                                                
        @filebase = File.basename((hash[:filename] or hash['filename'] or 
      end                                                                 
      hash['filename'] = File.join(_dirname,                              
                    @filebase.sub(/(\.\w*)$/, "_#{@DateStamp}" + '\1'))   
      super(_name, hash)                                                  
    end                                                                   
                                                                          
    #######                                                               
    private                                                               
    #######                                                               
                                                                          
    # perform the write                                                   
    def write(data)                                                       
      change if requiresChange                                            
      super                                                               
    end                                                                   
                                                                          
    # construct a new filename from the DateStamp                         
    def makeNewFilename                                                   
        @DateStamp = Time.now.strftime( @DatePattern);                    
        @filename = File.join(File.dirname(@filename),                    
                    @filebase.sub(/(\.\w*)$/, "_#{@DateStamp}" + '\1'))   
    end                                                                   
                                                                          
    # does the file require a change?                                     
    def requiresChange                                                    
      _DateStamp = Time.now.strftime( @DatePattern);                      
      if not _DateStamp == @DateStamp                                     
        @DateStamp = _DateStamp                                           
        return true                                                       
      end                                                                 
      false                                                               
    end                                                                   
                                                                          
    # change the file                                                     
    def change                                                            
      begin                                                               
        @out.close                                                        
      rescue                                                              
        Logger.log_internal {                                             
          "DateFileOutputter '#{@name}' could not close #{@filename}"     
        }                                                                 
      end                                                                 
      makeNewFilename                                                     
      @out = File.new(@filename, (@trunc ? "w" : "a"))                    
      Logger.log_internal {                                               
        "DateFileOutputter '#{@name}' now writing to #{@filename}"        
      }                                                                   
    end                                                                   
  end                                                                     
                                                                          
end                                                                   


1.1.7

                                                                                     
require "log4r/outputter/fileoutputter"                                              
require "log4r/staticlogger"                                                         
                                                                                     
module Log4r                                                                         
                                                                                     
  # Additional hash arguments are:                                                   
  #                                                                                  
  # [<tt>:dirname</tt>]         Directory of the log file                            
  # [<tt>:date_pattern</tt>]    Time.strftime format string (default is "%Y-%m-%d")  
                                                                                     
  class DateFileOutputter < FileOutputter                                            
    DEFAULT_DATE_FMT = "%Y-%m-%d"                                                    
                                                                                     
    def initialize(_name, hash={})                                                   
      @DatePattern = (hash[:date_pattern] or hash['date_pattern'] or                 
                      DEFAULT_DATE_FMT)                                              
      @DateStamp = Time.now.strftime( @DatePattern);                                 
      _dirname = (hash[:dirname] or hash['dirname'])                                 
      # hash[:dirname] masks hash[:filename]                                         
      if _dirname                                                                    
        if not FileTest.directory?( _dirname)                                        
          raise StandardError, "'#{_dirname}' must be a valid directory", caller     
        end                                                                          
      end                                                                            
                                                                                     
      _filename = (hash[:filename] or hash['filename'])                              
      if _filename.nil?                                                              
        @filebase = File.basename( $0, '.rb') + ".log"                               
      else                                                                           
        @filebase = File.basename((hash[:filename] or hash['filename'] or ""))       
      end                                                                            
                                                                                     
      # Get rid of the 'nil' in the path                                             
      path = [_dirname, @filebase.sub(/(\.\w*)$/, "_#{@DateStamp}" + '\1')].compact  
      hash[:filename] = hash['filename'] = File.join(path)                           
                                                                                     
      super(_name, hash)                                                             
    end                                                                              
                                                                                     
    #######                                                                          
    private                                                                          
    #######                                                                          
                                                                                     
    # perform the write                                                              
    def write(data)                                                                  
      change if requiresChange                                                       
      super                                                                          
    end                                                                              
                                                                                     
    # construct a new filename from the DateStamp                                    
    def makeNewFilename                                                              
        @DateStamp = Time.now.strftime( @DatePattern);                               
        @filename = File.join(File.dirname(@filename),                               
                    @filebase.sub(/(\.\w*)$/, "_#{@DateStamp}" + '\1'))              
    end                                                                              
                                                                                     
    # does the file require a change?                                                
    def requiresChange                                                               
      _DateStamp = Time.now.strftime( @DatePattern);                                 
      if not _DateStamp == @DateStamp                                                
        @DateStamp = _DateStamp                                                      
        return true                                                                  
      end                                                                            
      false                                                                          
    end                                                                              
                                                                                     
    # change the file                                                                
    def change                                                                       
      begin                                                                          
        @out.close                                                                   
      rescue                                                                         
        Logger.log_internal {                                                        
          "DateFileOutputter '#{@name}' could not close #{@filename}"                
        }                                                                            
      end                                                                            
      makeNewFilename                                                                
      @out = File.new(@filename, (@trunc ? "w" : "a"))                               
      Logger.log_internal {                                                          
        "DateFileOutputter '#{@name}' now writing to #{@filename}"                   
      }                                                                              
    end                                                                              
  end                                                                                
                                                                                     
end                                                                                  

                                           

你可能感兴趣的:(Ruby)