Sunday, December 21, 2008

Logging user agent in log file using mixin

Create actioncontroller_base.rb:

module ActionController #:nodoc:
class Base
private
def log_processing_for_request_id
request_id = "\n\nProcessing #{self.class.name}\##{action_name} "
request_id << "to #{params[:format]} " if params[:format]
request_id << "(for #{request_origin}) [#{request.method.to_s.upcase}]\n[#{request.user_agent.downcase}]"

logger.info(request_id)
end
end
end


In environment.rb, add:

require "actioncontroller_base"


When a request is logged, it will be:

Processing SitesController#index (for 127.0.0.1 at 2008-12-21 11:49:38) [GET]
[mozilla/5.0 (windows; u; windows nt 6.0; en-us; rv:1.9.0.5) gecko/2008120122 firefox/3.0.1;megaupload 1.0 (.net clr 3.5.30729)]
Rendering template within layouts/application
Rendering sites/index
Completed in 53ms (View: 50, DB: 0) | 200 OK [http://localhost/site/index]

No comments: