Class File::Tail::Logfile
In: lib/file/tail.rb
Parent: File

This is an easy to use Logfile class that includes the File::Tail module.

Usage

The unix command "tail -10f filename" can be emulated like that:

 File::Tail::Logfile.open(filename, :backward => 10) do |log|
   log.tail { |line| puts line }
 end

Or a bit shorter:

 File::Tail::Logfile.tail(filename, :backward => 10) do |line|
   puts line
 end

To skip the first 10 lines of the file do that:

 File::Tail::Logfile.open(filename, :forward => 10) do |log|
   log.tail { |line| puts line }
 end

The unix command "head -10 filename" can be emulated like that:

 File::Tail::Logfile.open(filename, :return_if_eof => true) do |log|
   log.tail(10) { |line| puts line }
 end

Methods

open   tail  

Included Modules

File::Tail

Public Class methods

This method creates an File::Tail::Logfile object and yields to it, and closes it, if a block is given, otherwise it just returns it. The opts hash takes an option like

  • :backward => 10 to go backwards
  • :forward => 10 to go forwards

in the logfile for 10 lines at the start. The buffersize for going backwards can be set with the

  • :bufsiz => 8192 option.

To define a callback, that will be called after a reopening occurs, use:

  • :after_reopen => lambda { |file| p file }

Every attribute of File::Tail can be set with a :attributename => value option.

Like open, but yields to every new line encountered in the logfile in block.

[Validate]