class Yell::Adapters::Datefile

The Datefile adapter is similar to the File adapter. However, it rotates the file at midnight (by default).

Constants

DefaultDatePattern

The default date pattern, e.g. “19820114” (14 Jan 1982)

Metadata

HeaderRegexp

Attributes

date_pattern[RW]

The pattern to be used for the files

@example

date_pattern = "%Y%m%d"       # default
date_pattern = "%Y-week-%V"
header[RW]

You can suppress the first line of the logfile that contains the metadata. This is important upon rollover, because on *nix systems, it is not possible to determine the creation time of a file, on the last access time. The header compensates this.

@example

header = false
keep[RW]

Set the amount of logfiles to keep when rolling over. By default, no files will be cleaned up.

@example Keep the last 5 logfiles

keep = 5
keep = '10'

@example Do not clean up any files

keep = 0

Private Instance Methods

cleanup!() click to toggle source

Removes old logfiles of the same date pattern.

By reading the header of the files that match the date pattern, the adapter determines whether to remove them or not. If no header is present, it makes the best guess by checking the last access time (which may result in false cleanups).

# File lib/yell/adapters/datefile.rb, line 119
def cleanup!
  files = Dir[ @original_filename.sub(/(\.\w+)?$/, ".*\\1") ].sort.select do |file|
    _, pattern = header_from(file)

    # Select if the date pattern is nil (no header info available within the file) or
    # when the pattern matches.
    pattern.nil? || pattern == self.date_pattern
  end

  ::File.unlink( *files[0..-keep-1] )
end
cleanup?() click to toggle source

Cleanup old logfiles?

@return [Boolean] true or false

# File lib/yell/adapters/datefile.rb, line 134
def cleanup?
  !!keep && keep.to_i > 0
end
close!() click to toggle source

@overload close!

Calls superclass method Yell::Adapters::Io#close!
# File lib/yell/adapters/datefile.rb, line 88
def close!
  @filename = filename_for(@date)

  super
end
close?() click to toggle source

Determine whether to close the file handle or not.

It is based on the `:date_pattern` (can be passed as option upon initialize). If the current time hits the pattern, it closes the file stream.

@return [Boolean] true or false

# File lib/yell/adapters/datefile.rb, line 100
def close?
  _date           = Time.now
  _date_strftime  = _date.strftime(date_pattern)

  if @stream.nil? or _date_strftime != @date_strftime
    @date, @date_strftime = _date, _date_strftime

    return true
  end

  false
end
filename_for( date ) click to toggle source

Sets the filename with the `:date_pattern` appended to it.

# File lib/yell/adapters/datefile.rb, line 167
def filename_for( date )
  @original_filename.sub(/(\.\w+)?$/, ".#{date.strftime(date_pattern)}\\1")
end
header!() click to toggle source

Write the header information into the file

# File lib/yell/adapters/datefile.rb, line 155
def header!
  stream.puts( Header.call(@date, date_pattern) )
end
header?() click to toggle source

Write header into the file?

@return [Boolean] true or false

# File lib/yell/adapters/datefile.rb, line 162
def header?
  !!header
end
header_from( file ) click to toggle source

Fetch the header form the file

# File lib/yell/adapters/datefile.rb, line 172
def header_from( file )
  if m = ::File.open(file, &:readline).match(HeaderRegexp)
    # in case there is a Header present, we can just read from it
    [ Time.at(m[2].to_f), m[3] ]
  else
    # In case there is no header: we need to take a good guess
    #
    # Since the pattern can not be determined, we will just return the Posix ctime.
    # That is NOT the creatint time, so the value will potentially be wrong!
    [::File.ctime(file), nil]
  end
end
inspectables() click to toggle source

@overload inspectables

Calls superclass method Yell::Adapters::Io#inspectables
# File lib/yell/adapters/datefile.rb, line 186
def inspectables
  super.concat [:date_pattern, :header, :keep, :symlink ]
end
setup!( options ) click to toggle source

@overload setup!( options )

Calls superclass method Yell::Adapters::File#setup!
# File lib/yell/adapters/datefile.rb, line 56
def setup!( options )
  self.header = Yell.__fetch__(options, :header, :default => true)
  self.date_pattern = Yell.__fetch__(options, :date_pattern, :default => DefaultDatePattern)
  self.keep = Yell.__fetch__(options, :keep, :default => false)
  self.symlink = Yell.__fetch__(options, :symlink, :default => true)

  @original_filename  = ::File.expand_path(Yell.__fetch__(options, :filename, :default => default_filename))
  options[:filename]  = @original_filename

  @date = Time.now
  @date_strftime = @date.strftime(date_pattern)

  super
end
write!( event ) click to toggle source

@overload write!( event )

Calls superclass method Yell::Adapters::Io#write!
# File lib/yell/adapters/datefile.rb, line 72
def write!( event )
  # do nothing when not closing
  return super unless close?
  close

  # exit when file ready present
  return super if ::File.exist?(@filename)

  header! if header?
  symlink! if symlink?
  cleanup! if cleanup?

  super
end