Class: RequestLogAnalyzer::LogProcessor
- Inherits:
-
Object
- Object
- RequestLogAnalyzer::LogProcessor
- Defined in:
- lib/request_log_analyzer/log_processor.rb
Overview
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/.
Instance Attribute Summary collapse
-
#file_format ⇒ Object
readonly
Returns the value of attribute file_format.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#output_file ⇒ Object
Returns the value of attribute output_file.
-
#sources ⇒ Object
readonly
Returns the value of attribute sources.
Class Method Summary collapse
-
.build(command, arguments) ⇒ Object
Builds a logprocessor instance from the arguments given on the command line
command
The command hat was used to start the log processor.
Instance Method Summary collapse
-
#initialize(format, mode, options = {}) ⇒ LogProcessor
constructor
Initializes a new LogProcessor instance.
-
#process_file(file) ⇒ Object
Processes input files by opening it and sending the filestream to
process_io
, in which the actual processing is performed. -
#process_io(io) ⇒ Object
Processes an input stream by iteration over each line and processing it according to the current operation mode
io
The IO instance to process. -
#run! ⇒ Object
Runs the log processing by setting up the output stream and iterating over all the input sources.
-
#strip_line(line) ⇒ Object
Returns the line itself if the string matches any of the line definitions.
Constructor Details
#initialize(format, mode, options = {}) ⇒ LogProcessor
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
43 44 45 46 47 48 49 |
# File 'lib/request_log_analyzer/log_processor.rb', line 43 def initialize(format, mode, = {}) @options = @mode = mode @sources = [] @file_format = format $output_file = nil end |
Instance Attribute Details
#file_format ⇒ Object (readonly)
Returns the value of attribute file_format.
15 16 17 |
# File 'lib/request_log_analyzer/log_processor.rb', line 15 def file_format @file_format end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
15 16 17 |
# File 'lib/request_log_analyzer/log_processor.rb', line 15 def mode @mode end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
15 16 17 |
# File 'lib/request_log_analyzer/log_processor.rb', line 15 def @options end |
#output_file ⇒ Object
Returns the value of attribute output_file.
16 17 18 |
# File 'lib/request_log_analyzer/log_processor.rb', line 16 def output_file @output_file end |
#sources ⇒ Object (readonly)
Returns the value of attribute sources.
15 16 17 |
# File 'lib/request_log_analyzer/log_processor.rb', line 15 def sources @sources end |
Class Method Details
.build(command, arguments) ⇒ Object
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)
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/request_log_analyzer/log_processor.rb', line 22 def self.build(command, arguments) = { :discard_teaser_lines => arguments[:discard_teaser_lines], :keep_junk_lines => arguments[:keep_junk_lines], } log_processor = RequestLogAnalyzer::LogProcessor.new(arguments[:format].to_sym, command, ) log_processor.output_file = arguments[:output] if arguments[:output] arguments.parameters.each do |input| log_processor.sources << input end return log_processor end |
Instance Method Details
#process_file(file) ⇒ Object
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
54 55 56 |
# File 'lib/request_log_analyzer/log_processor.rb', line 54 def process_file(file) File.open(file, 'r') { |file| process_io(file) } end |
#process_io(io) ⇒ Object
Processes an input stream by iteration over each line and processing it according to the current operation mode io
The IO instance to process.
61 62 63 64 65 |
# File 'lib/request_log_analyzer/log_processor.rb', line 61 def process_io(io) case mode when :strip; io.each_line { |line| @output << strip_line(line) } end end |
#run! ⇒ Object
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.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/request_log_analyzer/log_processor.rb', line 77 def run! if @output_file.nil? @output = $stdout else @output = File.new(@output_file, 'a') end @sources.each do |source| if source.kind_of?(String) && File.exist?(source) process_file(source) elsif source.kind_of?(IO) process_io(source) elsif ['-', 'STDIN'].include?(source) process_io($stdin) end end ensure @output.close if @output.kind_of?(File) end |
#strip_line(line) ⇒ Object
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
70 71 72 |
# File 'lib/request_log_analyzer/log_processor.rb', line 70 def strip_line(line) file_format.line_definitions.any? { |name, definition| definition =~ line } ? line : "" end |