2017-08-14 13:08:09 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2006-02-28 17:40:19 -05:00
|
|
|
namespace :log do
|
2016-08-06 13:55:02 -04:00
|
|
|
##
|
2015-12-09 14:46:38 -05:00
|
|
|
# Truncates all/specified log files
|
2016-08-06 13:55:02 -04:00
|
|
|
# ENV['LOGS']
|
2016-09-09 21:43:09 -04:00
|
|
|
# - defaults to all environments log files i.e. 'development,test,production'
|
2015-12-09 14:46:38 -05:00
|
|
|
# - ENV['LOGS']=all truncates all files i.e. log/*.log
|
|
|
|
# - ENV['LOGS']='test,development' truncates only specified files
|
|
|
|
desc "Truncates all/specified *.log files in log/ to zero bytes (specify which logs with LOGS=test,development)"
|
2006-02-28 11:28:11 -05:00
|
|
|
task :clear do
|
2013-01-16 13:27:06 -05:00
|
|
|
log_files.each do |file|
|
|
|
|
clear_log_file(file)
|
2006-02-28 11:28:11 -05:00
|
|
|
end
|
|
|
|
end
|
2013-01-16 13:27:06 -05:00
|
|
|
|
|
|
|
def log_files
|
2016-08-06 13:15:47 -04:00
|
|
|
if ENV["LOGS"] == "all"
|
2013-01-16 13:27:06 -05:00
|
|
|
FileList["log/*.log"]
|
2016-08-06 13:15:47 -04:00
|
|
|
elsif ENV["LOGS"]
|
|
|
|
log_files_to_truncate(ENV["LOGS"])
|
2015-12-09 14:46:38 -05:00
|
|
|
else
|
2016-09-09 21:43:09 -04:00
|
|
|
log_files_to_truncate(all_environments.join(","))
|
2013-01-16 13:27:06 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-09 14:46:38 -05:00
|
|
|
def log_files_to_truncate(envs)
|
2016-08-06 13:15:47 -04:00
|
|
|
envs.split(",")
|
2015-12-09 14:46:38 -05:00
|
|
|
.map { |file| "log/#{file.strip}.log" }
|
|
|
|
.select { |file| File.exist?(file) }
|
|
|
|
end
|
2016-08-06 13:55:02 -04:00
|
|
|
|
2013-01-16 13:27:06 -05:00
|
|
|
def clear_log_file(file)
|
|
|
|
f = File.open(file, "w")
|
|
|
|
f.close
|
|
|
|
end
|
2016-09-09 21:43:09 -04:00
|
|
|
|
|
|
|
def all_environments
|
|
|
|
Dir["config/environments/*.rb"].map { |fname| File.basename(fname, ".*") }
|
|
|
|
end
|
2006-02-28 11:28:11 -05:00
|
|
|
end
|