# Logging sql calls

**URL:** <https://www.ruby-forum.com/t/logging-sql-calls/73392>\
**Category:** Rails\
**Created:** [October 24, 2006, 10:51am UTC](https://www.ruby-forum.com/t/logging-sql-calls/73392 "2006-10-24T10:51:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![chadster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/46a35a/32.png) [@chadster](https://www.ruby-forum.com/u/chadster)\
**Post date:** [October 24, 2006, 10:51am UTC](https://www.ruby-forum.com/t/logging-sql-calls/73392/1 "2006-10-24T10:51:27Z")

</div>

Is there an easy way to log every SQL call to the database? I’d like to  
log  
mysql queries and their respective execution times into a table so that  
I  
can closely monitor/report the performance of my application and  
identify  
potential bottlenecks.

Thanks,  
Chad

---

<div class="post-metadata">

**Author:** ![chadster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/46a35a/32.png) [@chadster](https://www.ruby-forum.com/u/chadster)\
**Post date:** [October 24, 2006, 11:00am UTC](https://www.ruby-forum.com/t/logging-sql-calls/73392/2 "2006-10-24T11:00:37Z")

</div>

Chad A. wrote:

> Is there an easy way to log every SQL call to the database? I’d like to  
> log  
> mysql queries and their respective execution times into a table so that  
> I  
> can closely monitor/report the performance of my application and  
> identify  
> potential bottlenecks.

If your config.log\_level is :debug (which it is for the development  
environment, production defaults to :info) then all your sql calls will  
be logged.

Fred

---

<div class="post-metadata">

**Author:** ![chadster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/46a35a/32.png) [@chadster](https://www.ruby-forum.com/u/chadster)\
**Post date:** [October 25, 2006, 1:09am UTC](https://www.ruby-forum.com/t/logging-sql-calls/73392/3 "2006-10-25T01:09:24Z")

</div>

I might have been a little unclear…

I’d like to log those sql calls TO a database table.

in example:

class SQLQueries \< ActiveRecord::Migration  
def self.up  
create\_table :sql\_queries do |t|  
t.column :query, :string  
t.column :execution\_time, :decimal  
t.column :date, :timestamp  
end  
end

thanks,  
Chad

---

<div class="post-metadata">

**Author:** ![chadster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/46a35a/32.png) [@chadster](https://www.ruby-forum.com/u/chadster)\
**Post date:** [October 25, 2006, 7:32pm UTC](https://www.ruby-forum.com/t/logging-sql-calls/73392/4 "2006-10-25T19:32:21Z")

</div>

anybody else? is it really that tough?

---

<div class="post-metadata">

**Author:** ![chadster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/46a35a/32.png) [@chadster](https://www.ruby-forum.com/u/chadster)\
**Post date:** [November 7, 2006, 10:54am UTC](https://www.ruby-forum.com/t/logging-sql-calls/73392/5 "2006-11-07T10:54:17Z")

</div>

In case anyone in the future cares, I found a pretty good solution for  
tracking db layer performance:

1. I implemented the Query Stats plugin:  
[Google Code Archive - Long-term storage for Google Code Project Hosting.](http://code.google.com/p/query-stats/)
2. set an after\_filter to insert the results of those calls to the  
database. see below:

after\_filter :logging\_dbstats

def logging\_dbstats  
# log sql stats here (pull from footer)  
sql = “insert into  
logging\_dbstats(controller,action,controller\_queries,view\_queries,execution\_time)  
values  
(‘#{params[:controller]}’,‘#{params[:action]}’,‘#{queries.count\_with\_label  
:controller}’, ‘#{queries.count\_with\_label :view}’,  
‘#{queries.total\_time}’)”  
ActiveRecord::Base.connection.execute(sql)  
end

cheers

---

<div class="post-metadata">

**Author:** ![chadster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/46a35a/32.png) [@chadster](https://www.ruby-forum.com/u/chadster)\
**Post date:** [October 25, 2006, 9:32pm UTC](https://www.ruby-forum.com/t/logging-sql-calls/73392/6 "2006-10-25T21:32:31Z")

</div>

Chad wrote:

> anybody else? is it really that tough?

Well you could write your implementation of Logger (eg there is one that  
logs to syslog) that stuck things in the database, but then you’d be  
getting all the logging statements, not just the ones about db queries.  
It might be easier to just patch the code for the DB driver you use (or  
possibly for the corresponding ActiveRecord db adaptor

Fred

---

<div class="post-metadata">

**Author:** ![chadster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/46a35a/32.png) [@chadster](https://www.ruby-forum.com/u/chadster)\
**Post date:** [November 7, 2006, 10:54am UTC](https://www.ruby-forum.com/t/logging-sql-calls/73392/7 "2006-11-07T10:54:17Z")

</div>

ignore the comment in the code
