Search code examples
ruby-on-railsrubyloggingjrubywarbler

How to create a log folder before rails application loads the config.logger


  1. I have an rails app. I am using Jruby/Warble/Jetty to create the executable war file. Copy paste the war file into a separate folder.
  2. I want the application to log outside the war file. So i am using the solution from this LINK

3.

config.logger = ActiveSupport::BufferedLogger.new(File.join(ENV['RAILS_ENV'], "#{Rails.root}/logs/#{ENV['RAILS_ENV']}.log"))
  1. When the rails server boots up. It says "Automatic creation of logging directories has been deprecated in rails". SEE Kevin Bedell Answer

How do I create a folder when the rails application starts so that the folder is created when the application boots up and the logs can access the created folder.

Where should I add the code in which folder under \config so that it loads the code which creates a directory first and then uses the config? Thank you.


Solution

  • Copy Paste it config/environments/development.rb

    log_file_name = "#{Rails.root}/logs/#{ENV['RAILS_ENV']}.log"
    unless File.exist?(File.dirname(log_file_name))
      FileUtils.mkdir_p(File.dirname(log_file_name))
      File.new(log_file_name, 'a+')
    end
    
    config.logger = ActiveSupport::BufferedLogger.new(File.join("#{Rails.root}", "logs", "#{ENV['RAILS_ENV']}.log"))
    

    For 'a+' in File.new see this LINK

    You can also use the config/application.rb LINK