# Make RoR log to database in addition to production.log

**URL:** <https://www.ruby-forum.com/t/make-ror-log-to-database-in-addition-to-production-log/86377>\
**Category:** Rails\
**Created:** [February 21, 2007, 8:44pm UTC](https://www.ruby-forum.com/t/make-ror-log-to-database-in-addition-to-production-log/86377 "2007-02-21T20:44:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jszobody](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea666f/32.png) [@jszobody](https://www.ruby-forum.com/u/jszobody)\
**Post date:** [February 21, 2007, 8:44pm UTC](https://www.ruby-forum.com/t/make-ror-log-to-database-in-addition-to-production-log/86377/1 "2007-02-21T20:44:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jszobody](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea666f/32.png) [@jszobody](https://www.ruby-forum.com/u/jszobody)\
**Post date:** [February 21, 2007, 8:52pm UTC](https://www.ruby-forum.com/t/make-ror-log-to-database-in-addition-to-production-log/86377/2 "2007-02-21T20:52:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jszobody](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea666f/32.png) [@jszobody](https://www.ruby-forum.com/u/jszobody)\
**Post date:** [February 22, 2007, 6:41pm UTC](https://www.ruby-forum.com/t/make-ror-log-to-database-in-addition-to-production-log/86377/3 "2007-02-22T18:41:26Z")

</div>

Have you thought of using something like, google analytics, splunk or  
the rails log analyzer?

---

<div class="post-metadata">

**Author:** ![jszobody](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea666f/32.png) [@jszobody](https://www.ruby-forum.com/u/jszobody)\
**Post date:** [February 22, 2007, 6:44pm UTC](https://www.ruby-forum.com/t/make-ror-log-to-database-in-addition-to-production-log/86377/4 "2007-02-22T18:44:26Z")

</div>

Have you thought of using something like, google analytics, splunk or  
the rails log analyzer?

---

<div class="post-metadata">

**Author:** ![jszobody](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ea666f/32.png) [@jszobody](https://www.ruby-forum.com/u/jszobody)\
**Post date:** [February 23, 2007, 8:13am UTC](https://www.ruby-forum.com/t/make-ror-log-to-database-in-addition-to-production-log/86377/5 "2007-02-23T08:13:42Z")

</div>

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.
