Log rotation in ruby
Jake wrote a great til about linux log rotation last year. Ruby also has a log rotation alternative built into the stdlib.
Generally, ruby logging looks like this:
require 'logger'
logger = Logger.new('app.log')
logger.info('starting log')
You can also pass a 'daily', 'weekly' or 'monthly' to the logger:
logger = Logger.new('week.log', 'weekly')
If I use this log today, Tue Jan 9, and next week on Wed Jan 17, then ls
will show two log files:
week.log
week.log.20180113
20180113
is Saturday the last day of the previous logging period, a new log is started every Sunday. NOTE: no files are deleted with this style of logging, you may potentially run out of space.
You can also choose to log with the number of files to keep and the maximum size of each file.
logger = Logger.new('size.log', 4, 1024000)
In this case after producing more than 4 MB of logging information ls
will show you.
size.log
size.log.1
size.log.2
size.log.3
Compared to linux's logrotate
this is missing features, compression for example, but for adding log rotation to your logs quickly and simply it works great.