#!/usr/bin/ruby require File.dirname(__FILE__) + '/../lib/request_log_analyzer' require File.dirname(__FILE__) + '/../lib/cli/command_line_arguments' def terminal_width(default = 81) IO.popen('stty -a') do |pipe| column_line = pipe.detect { |line| /(\d+) columns/ =~ line } width = column_line ? $1.to_i : default end rescue default end # Parse the arguments given via commandline begin arguments = CommandLine::Arguments.parse do |command_line| command_line.command(:install) do |install| install.parameters = 1 end command_line.command(:strip) do |strip| strip.minimum_parameters = 1 strip.option(:format, :alias => :f, :default => 'rails') strip.option(:output, :alias => :o) strip.switch(:discard_teaser_lines, :t) strip.switch(:keep_junk_lines, :j) end command_line.command(:anonymize) do |anonymize| anonymize.minimum_parameters = 1 anonymize.option(:format, :alias => :f, :default => 'rails') anonymize.option(:output, :alias => :o) anonymize.switch(:discard_teaser_lines, :t) anonymize.switch(:keep_junk_lines, :j) end command_line.option(:format, :alias => :f, :default => 'rails') command_line.option(:file, :alias => :e) command_line.switch(:single_lines, :s) command_line.switch(:assume_correct_order) command_line.option(:aggregator, :alias => :a, :multiple => true) command_line.option(:database, :alias => :d) # filtering options command_line.option(:select, :multiple => true, :parameters => 2) command_line.option(:reject, :multiple => true, :parameters => 2) command_line.option(:after) command_line.option(:before) command_line.switch(:boring, :b) command_line.option(:report_width, :default => terminal_width - 1) command_line.switch(:debug) command_line.minimum_parameters = 1 end rescue CommandLine::Error => e puts "ARGUMENT ERROR: " + e.message if e.message puts puts "Usage: request-log-analyzer [LOGFILES*] " puts puts "Input options:" puts " --format , -f: Uses the specified log file format. Defaults to rails." puts " --after Only consider requests from or later." puts " --before Only consider requests before ." puts " --select Only consider requests where matches ." puts " --reject Only consider requests where does not match ." puts puts "Output options:" puts " --boring, -b Output reports without ASCII colors." puts " --database , -d: Creates an SQLite3 database of all the parsed request information." puts " --debug Print debug information while parsing." puts " --file Output to file." puts puts "Examples:" puts " request-log-analyzer development.log" puts " request-log-analyzer -b mongrel.0.log mongrel.1.log mongrel.2.log " puts " request-log-analyzer --format merb -d requests.db production.log" puts puts "To install rake tasks in your Rails application, " puts "run the following command in your application's root directory:" puts puts " request-log-analyzer install rails" exit(0) end def install_rake_tasks(install_type) if install_type == 'rails' require 'ftools' if File.directory?('./lib/tasks/') File.copy(File.dirname(__FILE__) + '/../tasks/request_log_analyzer.rake', './lib/tasks/request_log_analyze.rake') puts "Installed rake tasks." puts "To use, run: rake log:analyze" else puts "Cannot find /lib/tasks folder. Are you in your Rails directory?" puts "Installation aborted." end else raise "Cannot perform this install type! (#{install_type})" end end case arguments.command when :install install_rake_tasks(arguments.parameters[0]) when :strip require File.dirname(__FILE__) + '/../lib/request_log_analyzer/log_processor' RequestLogAnalyzer::LogProcessor.build(:strip, arguments).run! when :anonymize require File.dirname(__FILE__) + '/../lib/request_log_analyzer/log_processor' RequestLogAnalyzer::LogProcessor.build(:anonymize, arguments).run! else puts "Request log analyzer, by Willem van Bergen and Bart ten Brinke - Version 1.0\n\n" # Run the request_log_analyzer! RequestLogAnalyzer::Controller.build(arguments, terminal_width).run! puts puts "Thanks for using request-log-analyzer" end