BigBinary Blog

We write about Ruby on Rails, React.js, React Native, remote work, open source, engineering and design.

Ruby 2.4 has added additional parameters for Logger#new

This blog is part of our Ruby 2.4 series.

The Logger class in Ruby provides a simple but sophisticated logging utility.

After creating the logger object we need to set its level.

Ruby 2.3

2require 'logger'
3logger =
4logger.level = Logger::INFO

If we are working with ActiveRecord::Base.logger, then same code would look something like this.

2require 'logger'
3ActiveRecord::Base.logger =
4ActiveRecord::Base.logger.level = Logger::INFO

As we can see in the both the cases we need to set the level separately after instantiating the object.

Ruby 2.4

In Ruby 2.4, level can now be specified in the constructor.

2#ruby 2.4
3require 'logger'
4logger =, level: Logger::INFO)
6# let's verify it
7logger.level      #=> 1

Similarly, other options such as progname, formatter and datetime_format, which prior to Ruby 2.4 had to be explicitly set, can now be set during the instantiation.

1#ruby 2.3
2require 'logger'
3logger =
4logger.level = Logger::INFO
5logger.progname = 'bigbinary'
6logger.datetime_format = '%Y-%m-%d %H:%M:%S'
7logger.formatter = proc do |severity, datetime, progname, msg|
8  "#{severity} #{datetime} ==> App: #{progname}, Message: #{msg}\n"
10"Program started...")
12#=> INFO 2017-03-16 18:43:58 +0530 ==> App: bigbinary, Message: Program started...

Here is same stuff in Ruby 2.4.

1#ruby 2.4
2require 'logger'
3logger =,
4  level: Logger::INFO,
5  progname: 'bigbinary',
6  datetime_format: '%Y-%m-%d %H:%M:%S',
7  formatter: proc do |severity, datetime, progname, msg|
8    "#{severity} #{datetime} ==> App: #{progname}, Message: #{msg}\n"
9  end
11"Program started...")
13#=> INFO 2017-03-16 18:47:39 +0530 ==> App: bigbinary, Message: Program started...
Chirag Shah in Ruby 2.4
April 10, 2017

Subscribe to our newsletter