Back to Blog

Ruby 2.4 allows custom suffix of rotated log files

on 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

You might also like

If you liked this blog post, check out similar ones from BigBinary