Sha256: 0443b6ff3dbc66845de8996b809c13e9e441a09708c39ee5c980bf2c49fe16aa

Contents?: true

Size: 1.05 KB

Versions: 34

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

namespace :log do
  ##
  # Truncates all/specified log files
  # ENV['LOGS']
  #   - defaults to all environments log files i.e. 'development,test,production'
  #   - ENV['LOGS']=all truncates all files i.e. log/*.log
  #   - ENV['LOGS']='test,development' truncates only specified files
  desc "Truncate all/specified *.log files in log/ to zero bytes (specify which logs with LOGS=test,development)"
  task :clear do
    log_files.each do |file|
      clear_log_file(file)
    end
  end

  def log_files
    if ENV["LOGS"] == "all"
      FileList["log/*.log"]
    elsif ENV["LOGS"]
      log_files_to_truncate(ENV["LOGS"])
    else
      log_files_to_truncate(all_environments.join(","))
    end
  end

  def log_files_to_truncate(envs)
    envs.split(",")
        .map    { |file| "log/#{file.strip}.log" }
        .select { |file| File.exist?(file) }
  end

  def clear_log_file(file)
    f = File.open(file, "w")
    f.close
  end

  def all_environments
    Dir["config/environments/*.rb"].map { |fname| File.basename(fname, ".*") }
  end
end

Version data entries

34 entries across 34 versions & 4 rubygems

Version Path
railties-8.0.0 lib/rails/tasks/log.rake
railties-7.2.2 lib/rails/tasks/log.rake
railties-7.1.5 lib/rails/tasks/log.rake
railties-8.0.0.rc2 lib/rails/tasks/log.rake
railties-7.2.1.2 lib/rails/tasks/log.rake
railties-7.1.4.2 lib/rails/tasks/log.rake
railties-8.0.0.rc1 lib/rails/tasks/log.rake
railties-7.2.1.1 lib/rails/tasks/log.rake
railties-7.1.4.1 lib/rails/tasks/log.rake
railties-8.0.0.beta1 lib/rails/tasks/log.rake
omg-railties-8.0.0.alpha9 lib/rails/tasks/log.rake
omg-railties-8.0.0.alpha8 lib/rails/tasks/log.rake
omg-railties-8.0.0.alpha7 lib/rails/tasks/log.rake
omg-railties-8.0.0.alpha4 lib/rails/tasks/log.rake
omg-railties-8.0.0.alpha3 lib/rails/tasks/log.rake
railties-7.1.4 lib/rails/tasks/log.rake
railties-7.2.1 lib/rails/tasks/log.rake
railties-7.2.0 lib/rails/tasks/log.rake
railties-7.2.0.rc1 lib/rails/tasks/log.rake
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/railties-7.1.3.4/lib/rails/tasks/log.rake