Class: File::Tail::Logfile
- Includes:
- File::Tail
- Defined in:
- lib/file/tail/logfile.rb
Overview
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
Constant Summary
Constants included from File::Tail
VERSION, VERSION_ARRAY, VERSION_BUILD, VERSION_MAJOR, VERSION_MINOR
Instance Attribute Summary
Attributes included from File::Tail
#break_if_eof, #default_bufsize, #interval, #line_separator, #max_interval, #reopen_deleted, #reopen_suspicious, #return_if_eof, #suspicious_interval
Class Method Summary collapse
-
.open(filename, opts = {}, &block) ⇒ Object
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.
-
.tail(filename, opts = {}, &block) ⇒ Object
Like open, but yields to every new line encountered in the logfile in
block.
Methods included from File::Tail
#after_reopen, #backward, #debug?, #forward, #output_debug_information, #preset_attributes, #read_line, #reopen_file, #restat, #sleep_interval, #tail
Class Method Details
.open(filename, opts = {}, &block) ⇒ Object
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 => 10to go backwards -
:forward => 10to go forwards
in the logfile for 10 lines at the start. The buffersize for going backwards can be set with the
-
:bufsiz => 8192option.
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.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/file/tail/logfile.rb', line 42 def self.open(filename, opts = {}, &block) # :yields: file file = new filename opts.each do |o, v| writer = o.to_s + "=" file.__send__(writer, v) if file.respond_to? writer end if opts.key?(:wind) or opts.key?(:rewind) warn ":wind and :rewind options are deprecated, "\ "use :forward and :backward instead!" end if backward = opts[:backward] || opts[:rewind] (args = []) << backward args << opts[:bufsiz] if opts[:bufsiz] file.backward(*args) elsif forward = opts[:forward] || opts[:wind] file.forward(forward) end if opts[:after_reopen] file.after_reopen(&opts[:after_reopen]) end if block_given? begin block.call file ensure file.close end else file end end |
.tail(filename, opts = {}, &block) ⇒ Object
Like open, but yields to every new line encountered in the logfile in block.
75 76 77 78 79 80 81 82 |
# File 'lib/file/tail/logfile.rb', line 75 def self.tail(filename, opts = {}, &block) if ([ :forward, :backward ] & opts.keys).empty? opts[:backward] = 0 end open(filename, opts) do |log| log.tail { |line| block.call line } end end |