Last Updated: February 25, 2016
·
4.967K
· sorens

Custom Log Location in Rails

I use Dropbox to "backup" my workspace as well as provide seamless machine switching. This means, all of my Rails projects are on Dropbox. However, I discovered, that the logging is really burdensome to have Dropbox continuously syncing.

The solution, obviously, re-locate where Rails logs to. However, I didn't want to inflict this requirement on my co-developers.

I wrote a simple class to redirect the logging...

lib/config/log_to_user_dir.rb

class LogToUserDir
  LOGS_DIR    = "~/Library/Logs"

  def self.load
    log_dir = File.expand_path( File.join( LOGS_DIR, 
        Rails.application.class.parent_name ) )
    FileUtils.mkdir_p( log_dir )
    path = File.join( log_dir, "#{Rails.env}.log" )
    logfile = File.open( path, 'a' )
    logfile.sync = true
    Rails.logger = Logger.new( logfile )
  end
end

Using my override mechanism which doesn't impact my co-developers (see http://coderwall.com/p/eekfuw), I added this to my config/overrides/development.rb

MyApp::Application.configure do
  # log to our user's log directory
  LogToUserDir.load
end

If you prefer something more configurable, modify the log class to accept a parameter which sets the logging directory location (note: might want to change it's name at that point, as well).