### # Extemds ActionController::Base and aliases the process method, to wrap it with the standard logworm request cycle # # By default it will automatically log web requests (in a short format) into web_log # Can also log in long format if specified. # # Configuration # In ApplicationController, add # logs_requests :short, :long # logs_requests :long # logs_requests :short, :long ### if defined?(ActionController) require 'benchmark' ActionController::Base.class_eval do @@log_requests_short = true @@log_requests_long = false def self.logs_requests(*options) @@log_requests_short = (options.include?(:short) or options.include?("short")) @@log_requests_long = (options.include?(:long) or options.include?("long")) end def process_with_logworm_log(request, response, method = :perform_action, *arguments) return process_without_logworm_log(request, response, method, *arguments) unless RAILS_ENV == 'production' startTime = Time.now Logworm::Logger.start_cycle begin response = process_without_logworm_log(request, response, method, *arguments) appTime = (Time.now - startTime) ensure env = request.env status = response.status[0..2] Logworm::Logger.log(:web_log_long, {:summary => "#{env['REQUEST_METHOD']} #{env['REQUEST_URI']} - #{status} #{appTime}", :request => env_to_log(env).merge({:input => request.request_parameters}), :response => {:status => status, :headers => response.headers}, :profiling => appTime, :queue_size => env['HTTP_X_HEROKU_QUEUE_DEPTH'] || '-1'}) if @@log_requests_long Logworm::Logger.log(:web_log, {:summary => "#{env['REQUEST_METHOD']} #{env['REQUEST_URI']} - #{status} #{appTime}", :request_path => env['REQUEST_URI'], :request_ip => env['REMOTE_ADDR'], :request_method => env['REQUEST_METHOD'], :input => request.request_parameters, :response_status => status, :profiling => appTime}) if @@log_requests_short begin Timeout::timeout(1) { sent = 0 ts = Benchmark.realtime {sent = Logworm::Logger.flush} # Flushes only if there are any entries. Times out after a second logger.info("\t *** logworm - logs sent in #{ts} seconds") if sent > 0 } rescue Exception => e # Ignore --nothing we can do # The list of logs may (and most likely will) be preserved for the next request logger.error("logworm call failed: #{e}") if logger end return response end end def env_to_log(env) to_log = {} env.each do |k,v| to_log[k.to_s.downcase.to_sym] = v unless (k.to_s =~ /rack/i or k.to_s =~ /\./i) end end alias_method_chain :process, :logworm_log end end