Ruby on Rails - logging to database
Want to log every HTTP request in your database? It’s easy with rails.
Following solution intercepts log_processing call of ActionController::Base class.
Put this code at the end of config/environment.rb :
class ActionController::Base
private
alias :original_log_processing :log_processing if not respond_to?(:original_log_processing)
def log_processing
original_log_processing
pr = PageRequest.new
pr.session_id = @_session.session_id if @_session and @_session.respond_to?(:session_id)
pr.ip = request.remote_ip
pr.method = request.method.to_s
pr.domain = request.domain
pr.path = request.path
pr.parameters = YAML::dump(request.parameters)
pr.rails_env = ENV['RAILS_ENV']
pr.save
end
end
page_request table looks like this:
kanji=> \d page_requests
Table "public.page_requests"
Column | Type | Modifiers
------------+-----------------------------+---------------------------------------------------------------
id | bigint | not null default nextval('public.page_requests_id_seq'::text)
session_id | character varying |
created_at | timestamp without time zone |
ip | character varying |
method | character varying |
domain | character varying |
path | character varying |
parameters | character varying |
rails_env | character varying |
Indexes:
"page_requests_pkey" PRIMARY KEY, btree (id)
