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
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