Class: Ccdk::LogFile

Inherits:
Object show all
Defined in:
lib/ccdk/log_file.rb

Overview

A class for reading Rails log files. Provides a way to step through the log by assuming the format of the output conforms to the Rails defaul.

Direct Known Subclasses

GzipLogFile

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ LogFile

Create a new LogFile

Parameters

  • name - The name of the file to open



38
39
40
# File 'lib/ccdk/log_file.rb', line 38

def initialize(name)
  @fh = File.open(name, 'r')
end

Class Method Details

.open(file_name) ⇒ Object

Open a LogFile with the provided name. If the file name ends in ‘.gz’ assume that the file is gzipped

Parameters

  • file_name - The name of the log file to open

Examples

# Open the log file 'log/production.log'
LogFile.open('log/production.log')

# Open the gzipped log file 'log/old/production.log.1.gz'
LogFile.open('log/old/production.log.1.gz')


24
25
26
27
28
29
30
# File 'lib/ccdk/log_file.rb', line 24

def open(file_name)
  if file_name =~ /\.gz$/
    GzipLogFile.new(file_name)
  else
    LogFile.new(file_name)
  end
end

Instance Method Details

#closeObject

Close the log file



74
75
76
# File 'lib/ccdk/log_file.rb', line 74

def close
  @fh.close
end

#each_blockObject

execute code on each block of the log file. A block is defined as starting with Processing and ending with a blank line

Examples

log.each_block{ |b| puts b if b =~ ApplicationController }


47
48
49
50
51
# File 'lib/ccdk/log_file.rb', line 47

def each_block
  while !@fh.eof?
    yield next_block
  end
end

#next_blockObject

Read the next block from the log file

TODO: please refactor me



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ccdk/log_file.rb', line 56

def next_block
  lines = []

  line = @fh.gets
  while !start_of_block?(line)
    line = @fh.gets
    return "" if @fh.eof?
  end

  while(!@fh.eof? && !end_of_block?(line))
    lines << line.chomp
    line = @fh.gets
  end

  lines.join("\n")
end