ActiveRecordã§ãããSQLãçºè¡ãããæã«ããã®SQLãå®è¡ãããDBã®ã³ãã¯ã·ã§ã³æ å ±ã調ã¹ããã¨ããããã
Hoge.find() ããã¨ãã«ã©ãããSQLãçºè¡ããã¦ããã®ãã¨ããã®ã¯developmentç°å¢ãªãããã©ã«ãã§development.logã«ã¯ã¨ãªãã°ãåºãã
ãã®ãã°å¦ç㯠active_record/connection_adapters/abstract_adapter.rb ã«æ¸ããã¦ãã
# active_record/connection_adapters/abstract_adapter.rb 198 def log(sql, name) 199 name ||= "SQL" 200 @instrumenter.instrument("sql.active_record", 201 :sql => sql, :name => name, :connection_id => object_id) do 202 yield 203 end 204 rescue Exception => e 205 message = "#{e.class.name}: #{e.message}: #{sql}" 206 @logger.debug message if @logger 207 raise translate_exception(e, message) 208 end 209 210 def translate_exception(e, message) 211 # override in derived class 212 ActiveRecord::StatementInvalid.new(message) 213 end
ãããããã¯ãã¦å¦çãæ¸ãã°ããæ°ããããã©ãã¯ã¨ãªãã°ããªãã«ãªã£ã¦ãç°å¢ã§ã¯ãã®ã¡ã½ããã¯å®è¡ãããªãã®ã§ã¡ãã£ã¨ä¸ä¾¿ã
ã ããããæ¸ããã¨ã«ããã*1
::ActiveRecord::ConnectionAdapters::Mysql2Adapter.class_eval do def execute_with_logging(sql, name = nil) result = execute_without_logging(sql, name) # ããã§è§£æå¦çãæ¸ãã # DBã®æ å ±ã¯ @config ã¨ããããã·ã¥ã«å ¥ã£ã¦ãã # ãã¨ãã° @config[:database] ã§DBåã @config[:username] ã§DBã®ã¦ã¼ã¶åãåå¾å¯è½ result end alias_method_chain :execute, :logging end
ActiveRecordãçºè¡ããSQLã¯å¿ ãDBã¢ããã¿ã®executeã¡ã½ããã§å®è¡ãããã®ã§ããããããã¯ãã¦ããã