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