Created
February 19, 2015 20:23
-
-
Save tlux/ed339c6d71eef001ac51 to your computer and use it in GitHub Desktop.
Simple Log Rotate for Rails Applications
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace :log do | |
| desc 'Compresses the current application log for the environment' | |
| task :rotate do | |
| store_path = "#{Rails.root}/log" | |
| logfiles = Dir[File.join(store_path, '*.log')] | |
| logfiles.delete_if { |l| File.size(l).zero? } | |
| if logfiles.any? | |
| log_filenames = logfiles.map { |l| File.basename(l) }.join(' ') | |
| archive_filename = "logs_#{Time.now.strftime('%s')}.tgz" | |
| `cd #{store_path} && tar -zcvf #{archive_filename} #{log_filenames}` | |
| logfiles.each do |logfile| | |
| File.open(logfile, 'w') { |file| file.truncate(0) } | |
| end | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment