Log every SQL query to database in Rails

后端 未结 6 1512
梦毁少年i
梦毁少年i 2020-12-03 17:40

I want to save to a log file some SQL query rails performs, (namely the CREATE, UPDATE and DELETE ones) therefore I need to intercept all queries and then filter them maybe

相关标签:
6条回答
  • 2020-12-03 17:50

    As a note for followers, you can "log all queries" like Rails - See generated SQL queries in Log files and then grep the files for the ones you want, if desired.

    0 讨论(0)
  • 2020-12-03 17:52

    Here a simplified version of what c0r0ner linked to, to better show it:

    connection = ActiveRecord::Base.connection
    class << connection
      alias :original_exec :execute
      def execute(sql, *name)
        # try to log sql command but ignore any errors that occur in this block
        # we log before executing, in case the execution raises an error
        begin
            File.open(Rails.root.join("/log/sql.txt"),'a'){|f| f.puts Time.now.to_s+": "+sql}
        rescue Exception => e
          ;
        end
        # execute original statement
        original_exec(sql, *name)
      end
    end
    
    0 讨论(0)
  • 2020-12-03 17:52

    Slightly updated version of @luca's answer for at least Rails 4 (and probably Rails 5)

    Place this in config/initializers/sql_logger.rb:

    connection = ActiveRecord::Base.connection
    class << connection
      alias :original_exec :execute
      def execute(sql, *name)
        # try to log sql command but ignore any errors that occur in this block
        # we log before executing, in case the execution raises an error
        begin
          File.open(Rails.root.join("log/sql.log"), 'a') do |file|
            file.puts Time.now.to_s + ": " + sql
          end
        rescue Exception => e
          "Error logging SQL: #{e}"
        end
        # execute original statement
        original_exec(sql, *name)
      end
    end
    
    0 讨论(0)
  • 2020-12-03 18:00

    If you are using mysql I would look into mysqlbinlog . It is going to track everything that potentially updates data. you can grep out whatever you need from that log easily.

    http://dev.mysql.com/doc/refman/5.0/en/mysqlbinlog.html

    http://dev.mysql.com/doc/refman/5.0/en/binary-log.html

    0 讨论(0)
  • 2020-12-03 18:07

    SQL logging in rails - In brief - you need to override ActiveRecord execute method. There you can add any logic for logging.

    0 讨论(0)
  • 2020-12-03 18:11

    SQL Server? If so...

    Actually, I'd do this at the SQL end. You could set up a trace, and collect every query that comes through a connection with a particular Application Name. If you save it to a table, you can easily query that table later.

    0 讨论(0)
提交回复
热议问题