Created
January 27, 2009 19:19
-
-
Save jstewart/53487 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
# Rotate rails logs with more flexibility than system logrotate | |
# requires logrotate gem and written for use with passenger | |
# can easily be adapted to mongrel | |
require 'fileutils' | |
require 'rubygems' | |
require 'logrotate' | |
RAILS_SITES = [ | |
'/path/to/rails_root' | |
] | |
LOGROTATE_OPTIONS = { | |
:count => 2, | |
:gzip => true | |
} | |
def logs_for(rails_root) | |
Dir.glob(File.join(rails_root, 'log', '*.log')) | |
end | |
def should_rotate?(logfile) | |
# Rotate if size > 50M, or add other checks here | |
(File.size(logfile) / 1048576).round >= 50 | |
end | |
def restart_rails(rails_root) | |
FileUtils.touch File.join(rails_root, 'tmp', 'restart.txt') | |
end | |
for site in RAILS_SITES | |
restart_needed = false | |
unless File.exist?(site) | |
$stderr.puts "Rails site #{site} does not exist or bad permissions, skipping" | |
next | |
end | |
logs = logs_for(site) | |
logs.each do |log| | |
if should_rotate?(log) | |
LogRotate.rotate_file(log, LOGROTATE_OPTIONS) | |
restart_needed = true | |
end | |
end | |
restart_rails(site) if restart_needed | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment