I am taking over administration for a server which has a custom made 3rd party Rails app. The app developer informed me that the ruby log files are getting large and pointed me to the following link:
– https://stackoverflow.com/questions/4883891/ruby-on-rails-production-log-rotation
The logs are located in /root/production/app/log. This folder contains several files which end in .log. The current log files are about 2GB in size and the folder also has archived log files with the format of "logname.log.2013_01_18.bz2".
I tried searching the ssh command history to see if I can see which command was used to create the archive files, but the commands don't go back that far. I also ran "cat /var/lib/logrotate/status" but it doesn't look like logrotate did not rotate any logs from the above folder.
Basically:
- I would like to be able to run a command to rotate the logs manually and/or have the logs rotate automatically every Sunday night.
- If I need to shutdown my rails app to rotate the logs, I have no problem with this as I have a maintenance window every Sunday night to perform this.
- Finally, I am not sure if I should compress the log files weekly into bz2 or only compress the logs on a monthly basis for any rotated logs related to the previous month. This is mainly because I am not sure exactly how this app utilizes the logs and I have never utilized rails apps nor have I had to configure log rotation manually.
At this moment I do need to keep all log files and not discard any. Any information on concerns related to rotating logs, such as backing up logs before attempting to rotate are welcome.
Best Answer
logrotate
is used by the system to rotate logs so you have 2 choices. You can either incorporate the rotation of these app logs into the systems rotations or setup your own and either run them manually or from the root user's crontab (Assuming the Rails app is run as root given it's directory is/root/...
).System rotation
To setup a logrotation within the system's pre-existing ones simply add a new file to the directory
/etc/logrotate.d
. Call itrailsapp.conf
. I'd use the other examples there to construct it. Also confrere with thelogrotate
man page.User rotation
If you want to run your own instance of
logrotate
you only have to provide it with command line switches to do so./etc/logrotate.conf
/root/rails_logrotate.conf
Run it
If things look OK you can re-run these commands without the
-d
switch. This is for debugging purposes only and won't actually do any of the tasks, merely show you what it WOULD do.You could also use the
-v
switch to make it verbose, similar to the output seen when using the-d
switch.Example
Start with this log file.
Now run
logrotate
Check the results
Weekly Cron
To make this run every Sunday you could create the following crontab entry for the root user.
Add the following lines:
Then save the above.
You can also use these types of shortcuts instead of specifying the actual days, mins, secs, etc.
References