tags:

views:

151

answers:

2

How can I log all executed SQL statements to the conosle/stdout when using Ruby MySQL?

Like the log/development.log output from rails.

A: 

Rails uses ORMs like ActiveRecord which has it's logger associated to it. I don't think MySQL gem has a logger...

khelll
+1  A: 

If you mean Ruby/MySQL, that provides a debug() function which performs the same function as mysql_debug() -- if your client library has been compiled with debugging, you can get DBUG to trace things for you. This might give what you're after (with a bit of clean-up.)

Another approach would be to capture the MySQL packets using tcpdump and decode them with maatkit.

A third approach would be to alias Mysql.query and Mysql.real_query with your own functions that do logging. Something like this (untested! trivial example! doesn't handle blocks!):

class Mysql
  alias_method :old_query, :query
  def query(sql)
    $stderr.puts "SQL: #{sql}"
    old_query(sql)
  end
end
rjp