0

Is it possible in Ruby on Rails to log only the SQL commands generated and save it on a separate file?

I'm still in development mode and looking to save all the SQL commands on a seperate log.

I've checked the development.log files but they also save data like the

"Started GET "/assets/application.css" for 127.0.0.1 at 2014-02-17 16:41:47 +0800, etc.

I am looking to either create a separate log file only for SQL or remove these other lines leaving only the SQL commands.

Is this possible? Thank you.

1

1 Answer 1

1

If you use active record, you can overwrite the active record logger :

ActiveRecord::Base.logger = Logger.new("#{Rails.root}/log/sql_log.log")
Sign up to request clarification or add additional context in comments.

4 Comments

I'm not sure how to implement that. Can you give specifics on where I would need to add this line?
Oh, I added it to the development.rb and it works! thank you Intrepidd!
Regarding the sql logging, It appears the SQL log was transferred to the sql_log.log but how do you include for example the 'Started GET "/controller/index" for 127.0.0.1 at 2014-02-17 08:50:45 +0800 Connecting to database specified by database.yml' which is just before the SQL commands because this would include the date and time the SQL commands where executed.
I suggest you create your own logger class (make it inherit from Logger) and override the add method to include the time. More info : ruby-doc.org/stdlib-1.9.3/libdoc/logger/rdoc/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.