It is possible to do logrotate without restarting nginx (just send USR1 signal to nginx it will do the job). I wonder it is possible for my rails app (nginx passenger). It's not worth to restart my rails apps just to do a logrotate.
2 Answers
logrotate configuration is pretty simple to get this down
/path/to/rails_apps/*/shared/log/*.log {
daily
missingok
rotate 30
compress
delaycompress
copytruncate
}
the copytruncate
basically copies the content to new file and truncates the old file. this eliminates the need for restart.
-
I wonder why there is no postrotate script to tell. Any problems with this?– ChamnapMay 4, 2011 at 3:44
-
I have my production servers with this configuration.. and everything's working perfectly.– AddyMay 7, 2011 at 4:21
-
thank you guys, if you need to specify the size of log file use
size 100M
– IljaSep 9, 2016 at 16:25
If you are talking about rails application log rotation, you can do that by putting
config.logger = Logger.new(config.log_path, 10, 1024**2)
in your environment file. The 2nd argument is the number of .log files you’d like to keep, and the 3rd being the size in bytes that the files are allowed to reach before they’ll be rotated. This configuration means 10 files of 1 megabyte. May not be quite as configurable as logrotate perhaps (no support for compression etc.), but it lets you keep all your log files within your app. This usually works for me.
Also found this if want to stick to log rotation via nginx.
-
1
-
1I have personally found the rails log rotation to be hit or miss. sometimes the rails logger gets stuck on logging to old files after rotation.– AddyApr 12, 2011 at 3:44
-
3Be careful with this... if you are running more than one instance of Rails through Passenger (ie. PassengerMinInstances), and you also have something like PassengerMaxRequests set in your Passenger config, then be aware that each new spawn of a Rails process will create a new file. We had our setup like this: "config.logger = Logger.new(config.log_path, 14, 200000000)" and it only ever kept 1 day's worth of logs, since 14 instances would spin up and down throughout the day based on load, creating a new file each time. They hardly ever hit the max size of 200000000. Go with syslog.– HarmonApr 25, 2012 at 23:56
-
Please mark Addy's answer as the right one. His answer is better than the one I posted. Feb 16, 2016 at 10:13