Folks,
Crawling through my production.log file is a pain. Is there any way I
can have rails log all this information to a database table, instead
of, or in addition to the production.log file? Ideally the DB table
would have separate columns for some of the log details, like IP
address, controller, action, errors, trace, etc. The idea is that I
could then filter the log data to quickly see hits on a particular
action, or from a particular IP address, etc.
I know I could parse production.log and throw it in the database, but
I’d prefer to have that done up front without the need for a log
parser.
Thanks
jszobody wrote:
Folks,
Crawling through my production.log file is a pain. Is there any way I
can have rails log all this information to a database table, instead
of, or in addition to the production.log file? Ideally the DB table
would have separate columns for some of the log details, like IP
address, controller, action, errors, trace, etc. The idea is that I
could then filter the log data to quickly see hits on a particular
action, or from a particular IP address, etc.
I know I could parse production.log and throw it in the database, but
I’d prefer to have that done up front without the need for a log
parser.
Thanks
There is no functionality like that currently in Rails, but it wouldn’t
be hard to write it yourself (and even release it as a plugin!)
Something with a general idea like:
class ApplicationController < ActionController::Base
after_filter :log_request_to_db
private
def log_request_to_db
LogEntry.create(
:uri => request.request_uri,
:date => Time.now,
# populate other useful attributes here
)
end
end
Have you thought of using something like, google analytics, splunk or
the rails log analyzer?
Have you thought of using something like, google analytics, splunk or
the rails log analyzer?
I’m doing this in one of my apps…
I have a method in my application helper that displays the number of
hits
(ya, i know, old school hit counter, but what client wants, client
gets).
So, anyway, in addition to keeping up with the current hit count, I log
the
header data to the db.
module ApplicationHelper
def hit_counter
log = Log.new
log.log_type = ‘hit’
log.datetime = Time.now
log.save
controller.request.env.each do |header|
log_detail = LogDetail.new
log_detail.log_id = log.id
log_detail.detail_key = header[0]
log_detail.detail_value = header[1]
log_detail.save
end
log.id + 34873
end
…
…
…
class CreateLogs < ActiveRecord::Migration
def self.up
create_table :logs do |t|
t.column :log_type, :string
t.column :misc, :string
t.column :datetime, :timestamp
end
create_table :log_details do |t|
t.column :log_id, :integer, :null => false
t.column :detail_key, :string
t.column :detail_value, :string
end
log = Log.new
log.log_type = 'debug'
log.misc = "Initial log creation"
log.save
end
def self.down
drop_table :logs
drop_table :log_details
end
end
…
add_index :log_details, [:detail_key, :detail_value]
I can’t comment on how performant this will be as this hasn’t been in
production for very long. If you find a better solution, I’d like to
hear
about it.
–
James M.