Ruby 2.4 allows custom suffix of rotated log files

Sushant Mittal

Sushant Mittal

June 15, 2017

This blog is part of our  Ruby 2.4 series.

In Ruby, The Logger class can be used for rotating log files daily, weekly or monthly.


daily_logger = Logger.new('foo.log', 'daily')

weekly_logger = Logger.new('foo.log', 'weekly')

monthly_logger = Logger.new('foo.log', 'monthly')

At the end of the specified period, Ruby will change the file extension of the log file as follows:


foo.log.20170615

The format of the suffix for the rotated log file is %Y%m%d. In Ruby 2.3, there was no way to customize this suffix format.

Ruby 2.4 added the ability to customize the suffix format by passing an extra argument shift_period_suffix.


# Ruby 2.4

logger = Logger.new('foo.log', 'weekly', shift_period_suffix: '%d-%m-%Y')

Now, suffix of the rotated log file will use the custom date format which we passed.


foo.log.15-06-2017

If this blog was helpful, check out our full blog archive.

Stay up to date with our blogs.

Subscribe to receive email notifications for new blog posts.