Skip to content

Instantly share code, notes, and snippets.

@rdavila
Created September 6, 2011 16:20
Show Gist options
  • Save rdavila/1198039 to your computer and use it in GitHub Desktop.
Save rdavila/1198039 to your computer and use it in GitHub Desktop.
Problem to log AR queries to DB
class ActiveRecord::ConnectionAdapters::MysqlAdapter
def execute(sql, name = nil, skip_logging = false)
log(sql, name) { @connection.query(sql) } unless skip_logging
rescue ActiveRecord::StatementInvalid => exception
if exception.message.split(":").first =~ /Packets out of order/
raise ActiveRecord::StatementInvalid, "'Packets out of order' error was received from the database. Please update your mysql bindings (gem install mysql) and read http://dev.mysql.com/doc/mysql/en/password-hashing.html for more information. If you're on Windows, use the Instant Rails installer to get the updated mysql bindings."
else
raise
end
end
end
ActiveRecord::ConnectionAdapters::AbstractAdapter.class_eval do
def log_with_syspanel_logger(sql, name, &block)
if Thread.current["syspanel.sql.logger.enabled"]
# When I comment the line below I don't get any error
SyspanelSqlLog.connection.execute("INSERT INTO syspanel_sql_logs (`sql`, `name`) VALUES('#{sql}', '#{name}')", nil, true)
end
log_without_syspanel_logger(sql, name, &block)
end
unless self.instance_methods.include?('log_without_syspanel_logger')
alias_method_chain :log, :syspanel_logger
end
end
/!\ FAILSAFE /!\ Tue Sep 06 11:31:43 -0500 2011
Status: 500 Internal Server Error
stack level too deep
/Users/rdavila/.rvm/gems/ruby-1.8.7-p302@rails2/gems/activerecord-2.3.2/lib/active_record/connection_adapters/mysql_adapter.rb:7:in `define_all_hashes_method!'
/Users/rdavila/.rvm/gems/ruby-1.8.7-p302@rails2/gems/activerecord-2.3.2/lib/active_record/connection_adapters/mysql_adapter.rb:71:in `mysql_connection'
/Users/rdavila/.rvm/gems/ruby-1.8.7-p302@rails2/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:223:in `send'
/Users/rdavila/.rvm/gems/ruby-1.8.7-p302@rails2/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:223:in `new_connection'
/Users/rdavila/.rvm/gems/ruby-1.8.7-p302@rails2/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:245:in `checkout_new_connection'
/Users/rdavila/.rvm/gems/ruby-1.8.7-p302@rails2/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:188:in `checkout'
/Users/rdavila/.rvm/gems/ruby-1.8.7-p302@rails2/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:184:in `loop'
/Users/rdavila/.rvm/gems/ruby-1.8.7-p302@rails2/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:184:in `checkout'
/Users/rdavila/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/monitor.rb:242:in `synchronize'
/Users/rdavila/.rvm/gems/ruby-1.8.7-p302@rails2/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:183:in `checkout'
/Users/rdavila/.rvm/gems/ruby-1.8.7-p302@rails2/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:98:in `connection'
/Users/rdavila/.rvm/gems/ruby-1.8.7-p302@rails2/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:326:in `retrieve_connection'
/Users/rdavila/.rvm/gems/ruby-1.8.7-p302@rails2/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/connection_specification.rb:123:in `retrieve_connection'
/Users/rdavila/.rvm/gems/ruby-1.8.7-p302@rails2/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/connection_specification.rb:115:in `connection'
/Users/rdavila/projects/rails/app/lib/syspanel_sql_logger.rb:4:in `log'
/Users/rdavila/projects/rails/app/config/initializers/ar_extensions.rb:6:in `execute'
/Users/rdavila/.rvm/gems/ruby-1.8.7-p302@rails2/gems/activerecord-2.3.2/lib/active_record/connection_adapters/mysql_adapter.rb:586:in `configure_connection'
/Users/rdavila/.rvm/gems/ruby-1.8.7-p302@rails2/gems/activerecord-2.3.2/lib/active_record/connection_adapters/mysql_adapter.rb:581:in `connect'
/Users/rdavila/.rvm/gems/ruby-1.8.7-p302@rails2/gems/activerecord-2.3.2/lib/active_record/connection_adapters/mysql_adapter.rb:204:in `initialize'
/Users/rdavila/.rvm/gems/ruby-1.8.7-p302@rails2/gems/activerecord-2.3.2/lib/active_record/connection_adapters/mysql_adapter.rb:76:in `new'
/Users/rdavila/.rvm/gems/ruby-1.8.7-p302@rails2/gems/activerecord-2.3.2/lib/active_record/connection_adapters/mysql_adapter.rb:76:in `mysql_connection'
/Users/rdavila/.rvm/gems/ruby-1.8.7-p302@rails2/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:223:in `send'
/Users/rdavila/.rvm/gems/ruby-1.8.7-p302@rails2/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:223:in `new_connection'
/Users/rdavila/.rvm/gems/ruby-1.8.7-p302@rails2/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:245:in `checkout_new_connection'
/Users/rdavila/.rvm/gems/ruby-1.8.7-p302@rails2/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:188:in `checkout'
/Users/rdavila/.rvm/gems/ruby-1.8.7-p302@rails2/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:184:in `loop'
/Users/rdavila/.rvm/gems/ruby-1.8.7-p302@rails2/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:184:in `checkout'
/Users/rdavila/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/monitor.rb:242:in `synchronize'
class SyspanelSqlLogger
def initialize(app)
@app = app
end
def call(env)
Thread.current["syspanel.sql.logger.enabled"] = (env['REQUEST_URI'] =~ /mypath/).present?
@app.call(env)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment