class RequestLogAnalyzer::LogProcessor

The Logprocessor class is used to perform simple processing actions over log files. It will go over the log file/stream line by line, pass the line to a processor and write the result back to the output file or stream. The processor can alter the contents of the line, remain it intact or remove it altogether, based on the current file format

Currently, one processors is supported:

* :strip will remove all irrelevent lines (according to the file format) from the
  sources. A compact, information packed log will remain/.

Attributes

file_format[R]
mode[R]
options[R]
output_file[RW]
sources[R]

Public Class Methods

build(command, arguments) click to toggle source

Builds a logprocessor instance from the arguments given on the command line command The command hat was used to start the log processor. This will set the

processing mode. Currently, only :strip is supported.

arguments The parsed command line arguments (a CommandLine::Arguments instance)

   # File lib/request_log_analyzer/log_processor.rb
20 def self.build(command, arguments)
21   options = {
22     discard_teaser_lines: arguments[:discard_teaser_lines],
23     keep_junk_lines: arguments[:keep_junk_lines]
24   }
25 
26   log_processor = RequestLogAnalyzer::LogProcessor.new(arguments[:format].to_sym, command, options)
27   log_processor.output_file = arguments[:output] if arguments[:output]
28 
29   arguments.parameters.each do |input|
30     log_processor.sources << input
31   end
32 
33   log_processor
34 end
new(format, mode, options = {}) click to toggle source

Initializes a new LogProcessor instance. format The file format to use (e.g. :rails). mode The processing mode options A hash with options to take into account

   # File lib/request_log_analyzer/log_processor.rb
40 def initialize(format, mode, options = {})
41   @options     = options
42   @mode        = mode
43   @sources     = []
44   @file_format = format
45   $output_file = nil
46 end

Public Instance Methods

process_file(file) click to toggle source

Processes input files by opening it and sending the filestream to process_io, in which the actual processing is performed. file The file to process

   # File lib/request_log_analyzer/log_processor.rb
51 def process_file(file)
52   File.open(file, 'r') { |io| process_io(io) }
53 end
process_io(io) click to toggle source

Processes an input stream by iteration over each line and processing it according to the current operation mode io The IO instance to process.

   # File lib/request_log_analyzer/log_processor.rb
58 def process_io(io)
59   case mode
60     when :strip then     io.each_line { |line| @output << strip_line(line) }
61   end
62 end
run!() click to toggle source

Runs the log processing by setting up the output stream and iterating over all the input sources. Input sources can either be filenames (String instances) or IO streams (IO instances). The strings “-” and “STDIN” will be substituted for the $stdin variable.

   # File lib/request_log_analyzer/log_processor.rb
74 def run!
75   if @output_file.nil?
76     @output = $stdout
77   else
78     @output = File.new(@output_file, 'a')
79   end
80 
81   @sources.each do |source|
82     if source.is_a?(String) && File.exist?(source)
83       process_file(source)
84     elsif source.is_a?(IO)
85       process_io(source)
86     elsif ['-', 'STDIN'].include?(source)
87       process_io($stdin)
88     end
89   end
90 
91 ensure
92   @output.close if @output.is_a?(File)
93 end
strip_line(line) click to toggle source

Returns the line itself if the string matches any of the line definitions. If no match is found, an empty line is returned, which will strip the line from the output. line The line to strip

   # File lib/request_log_analyzer/log_processor.rb
67 def strip_line(line)
68   file_format.line_definitions.any? { |_name, definition| definition =~ line } ? line : ''
69 end