-1

I have a couple of lines in my config/environments/production.rb environment file for a rails 2.2.2 app, which only do anything when i'm running on my local machine:

if ENV['HOME'] == "/home/max"
  #fall back to the default logger instead of hodel, locally
  config.logger = nil
  #put sql into a seperate log 
  config.active_record.logger = Logger.new("log/sql.log")
end

Having this in the app's production environment file feels pretty dirty. I've got another file, config/initializers/max_local/send_mails_from_max_gmail.rb, which configures outgoing emails to use a gmail account, and does a couple of other me-specific things on startup.

Moving my logger config into there seems like a better option than having it in the production.rb file: for a start, the send_mails_from_max_gmail.rb file is git-ignored so no-one else ever sees it.

But, i can't figure out how to get to the rails config in there. It doesn't have the config local variable, which existed in the block generated by Rails::Initializer.run do |config|. How do i get at this config?

thanks, Max

EDIT: detailed reply to @japed's answer below, which requires some formatting and so is nicer here than in a comment. I'm just going to focus on config.active_record.logger = Logger.new("log/sql.log"), which moves the sql output into log/sql.log instead of the standard log.

I've tried the following: changed production.rb as follows: note that the logger line is commented out:

puts "production.rb:#{__LINE__} : config.active_record.logger = #{config.active_record.logger.inspect}"
#config.active_record.logger = Logger.new("log/sql.log")

Added the following to config/initializers/max_local/send_mails_from_max_gmail.rb:

puts "send_mails_from_max_gmail.rb:#{__LINE__} : Rails.configuration.active_record.logger = #{Rails.configuration.active_record.logger.inspect}"
Rails.configuration.active_record.logger = Logger.new("log/sql.log")
puts "send_mails_from_max_gmail.rb:#{__LINE__} : Rails.configuration.active_record.logger = #{Rails.configuration.active_record.logger.inspect}"

Then, i've restarted my server. i can see this output:

production.rb:65 : config.active_record.logger = nil
send_mails_from_max_gmail.rb:42 : Rails.configuration.active_record.logger = nil
send_mails_from_max_gmail.rb:44 : Rails.configuration.active_record.logger = #<Logger:0x7fcbee0fedc8 @formatter=nil, @progname=nil, @level=0, @logdev=#<Logger::LogDevice:0x7fcbee0fed28 @dev=#<File:log/sql.log>, @filename="log/sql.log", @mutex=#<Logger::LogDevice::LogDeviceMutex:0x7fcbee0fecd8 @mon_waiting_queue=[], @mon_entering_queue=[], @mon_count=0, @mon_owner=nil>, @shift_size=1048576, @shift_age=0>, @default_formatter=#<Logger::Formatter:0x7fcbee0fed78 @datetime_format=nil>>

which looks like it's working, and the the override is happening after the main config runs. But, the sql isn't going into log/sql.log, it's going into the standard production.log

Max Williams
  • 32,435
  • 31
  • 130
  • 197

1 Answers1

0

Ok, diving into the docs and in rails 2 Rails.logger maps to the constant RAILS_DEFAULT_LOGGER docs

So you should be able to do

if ENV['HOME'] == "/home/max"
  RAILS_DEFAULT_LOGGER = Logger.new("log/sql.log")
end

This time properly tested in console

RAILS_DEFAULT_LOGGER = Logger.new("log/sql.log")
=> warning: already initialized constant RAILS_DEFAULT_LOGGER

>> Rails.logger.info "Blah"
=> true

And I get Blah in log/sql.log

j-dexx
  • 10,286
  • 3
  • 23
  • 36
  • I should point out I tried this in console, not in an initializer. I figure it *should* work. – j-dexx Oct 21 '15 at 08:53
  • This doesn't work for me in the console actually. Would you mind expanding on your answer with exactly what you did in the console and how you tested whether it had worked? (in case i'm missing something) thanks! – Max Williams Oct 21 '15 at 12:03
  • Yeah, I only looked into setting it then assumed it would work after that, sorry. I'm guessing it's setting the configuration but that's loaded once when Rails boots which is why it's not working. – j-dexx Oct 21 '15 at 12:57
  • @MaxWilliams updated my answer having had a bit more of a fiddle around. Actually viewing the output this works for me in console now. – j-dexx Oct 21 '15 at 13:10
  • That pushes all of the standard logging into log/sql.log, but what i was trying to do (and what the original code in production.rb does) is push the SQL into `log/sql.log` and leave the rest into the regular log. – Max Williams Oct 21 '15 at 13:17
  • I just tried this `ActiveRecord::Base.logger = Logger.new("log/sql.log") ` and that didn't work either. – Max Williams Oct 21 '15 at 13:17
  • That was going to be my next suggestion :( Doing that in console worked for me - in terms of logging the sql to sql.log. Does it still log it to development too? – j-dexx Oct 21 '15 at 13:23
  • Yep, sql and regular logging are just going to the main log. Are you trying this in a rails 2.2.2 console? – Max Williams Oct 21 '15 at 13:24
  • Rails 2.3, I don't have any apps on 2.2.2 I'm afraid – j-dexx Oct 21 '15 at 13:24
  • Hmm, maybe something changed between 2.2.2 and 2.3 then. – Max Williams Oct 21 '15 at 13:36
  • BTW - when i said " sql and regular logging are just going to the main log" that was after doing `ActiveRecord::Base.logger = Logger.new("log/sql.log")`, ie this didn't seem to do anything. When i did `RAILS_DEFAULT_LOGGER = Logger.new("log/sql.log")` then regular logging and sql all go to `log/sql.log` – Max Williams Oct 21 '15 at 13:38
  • I'm starting to think that it might be easier to leave it as it is, in production.rb. – j-dexx Oct 21 '15 at 13:42