Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8831049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:13:38+00:00 2026-06-14T08:13:38+00:00

As a follow up from this post : i am trying to log all

  • 0

As a follow up from this post: i am trying to log all queries that are being generated. I have tried placing this in an initializer, and also just straight up running it in console and execute never seems to get executed.

I have tried the following:

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
    raise "THROW AN ERROR"
    begin
        file = File.open(RAILS_ROOT + "/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

And then run

Person.last

And I still get no error.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T08:13:39+00:00Added an answer on June 14, 2026 at 8:13 am

    You don’t need to monkey-patch ActiveRecord to get logging (which you’re doing incorrectly here anyway). I assume you want this in production, since in development all queries are already logged. Simply set this in config/environments/production.rb:

    config.log_level = :debug
    

    Or if you want to restrict this to SQL queries only, without the other verbosity, try this:

    ActiveRecord::Base.logger.level = Logger::DEBUG
    

    Or if your goal is to write the SQL queries to a dedicated logfile, check out this question: Rails3 SQL logging output in a separate file

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to follow the example in this post . Since I
This is a follow-on from this question, in which I was trying to suppress
This is a follow up from my previous question I have this code basically
This is a follow up from a question of mine that was just answered
I am trying to follow this example (from p137 of Rob Pickering's Foundations of
I'm trying to follow along this tutorial from Twilio -->http://www.twilio.com/docs/howto/search-and-buy But, it being written
Following help from this site I have now got my log in script largely
I've been trying to follow this Railscast: http://railscasts.com/episodes/213-calendars I've implemented the controls and all
I'm trying to follow the instructions laid out in this post , but I'm
This post will be lengthy and I apologize on beforehand for that. I'm trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.