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.

1
2daily_logger = Logger.new('foo.log', 'daily')
3
4weekly_logger = Logger.new('foo.log', 'weekly')
5
6monthly_logger = Logger.new('foo.log', 'monthly')
7

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

1
2foo.log.20170615
3

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.

1
2# Ruby 2.4
3
4logger = Logger.new('foo.log', 'weekly', shift_period_suffix: '%d-%m-%Y')
5

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

1
2foo.log.15-06-2017
3

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.