-
-
Save jdfdesign/4046716 to your computer and use it in GitHub Desktop.
easy cheasy log rotation in your rails' apps
This file contains 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
log/* |
This file contains 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
# file: config/initializers/logrotate.rb | |
# | |
# we don't really need to run locally from 'rails server' or 'rails console' | |
# | |
# but when we're fired up under passenger, unicorn, or whatever we'll rotate | |
# our logs on startup and periodically while running (bout every 42 minutes) | |
# | |
# | |
unless defined?(Rails::Server) or defined?(Rails::Console) | |
Thread.new do | |
Thread.current.abort_on_exception = true | |
sleep(rand(16)) | |
loop do | |
`#{ Rails.root }/script/logrotate` | |
sleep(rand(60 * 42)) | |
end | |
end | |
end |
This file contains 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
# file: ./script/logrotate | |
#! /usr/bin/env ruby | |
# encoding: utf-8 | |
# | |
# this script generates a simple and sane logrotate config file and runs | |
# logrotate using it. you can run it whenever using | |
# | |
# ./script/logrotate | |
# | |
# it's run automatically using | |
# | |
# config/initializers/logrotate.rb | |
# | |
# so you can deploy and not have to think about log rotation | |
# | |
exit(42) unless DATA.flock(File::LOCK_EX | File::LOCK_NB).zero? | |
config = DATA.read | |
script_dir = File.expand_path(File.dirname(__FILE__)) | |
rails_root = File.dirname(script_dir) | |
cmd = "logrotate #{ ARGV.join(' ') } -v -s ./log/logrotate.status ./log/logrotate.conf" | |
Dir.chdir(rails_root) do | |
open('./log/logrotate.conf', 'w') do |fd| | |
fd.write(config % rails_root) | |
end | |
puts "### #{ cmd }\n\n" | |
exec(cmd) | |
end | |
__END__ | |
"%s/log/*.log" { | |
weekly | |
missingok | |
rotate 4 | |
compress | |
delaycompress | |
notifempty | |
copytruncate | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment