Difference between Logger and ActiveSupport::Logger in Rails 6

Viewed 1369

Rails (specifically, Rails 6) defines ActiveSupport::Logger, which seems to be a wrapper of (one of Ruby's standard libraries) Logger according to the docs and source.

However, in the current official Rails reference as of October 2020 for Rails 6.0, the standard Logger is used in a config file. (It used to be ActiveSupport::Logger, maybe?)

Now, I would like to specify a different filename of the logfile from the default (log/development.log in the development environment) in the config file (e.g., config/environments/development.rb). Does

config.logger = Logger.new('log/my_log_file.log')

suffice? Or, is it better to use ActiveSupport::Logger? If so, what is the difference?

Note that in the default config/environments/development.rb no setting to follow or modify is defined about the logfile name. Still, log/development.log is created automatically in the development environment, so it must be defined somewhere obscure.

[EDIT] This answer suggests the way I put. I confirm it seems to work in my environment. But there is no reasoning why so… ActiveSupport::Logger is wrong?

1 Answers

As you explained in your question, ActiveSupport::Logger is indeed a wrapper for the standard Logger (https://github.com/rails/rails/blob/v6.1.4.1/activesupport/lib/active_support/logger.rb), and a quite thin one.

The main differences are that

  • AS::Logger configures a default log formatter and
  • provides a silence method to temporarily raise the log level and thus "silence" the logging below that level for the duration of a block. It also seems to contain a harness for chaining multiple loggers (the broadcast class method).

Rails uses AS::Logger unless anything else is specified in the config. They currently mention the class in the docs:

Rails makes use of the ActiveSupport::Logger class to write log information.

Later they go on to use the standard Logger in a code example. Probably, just for illustrative purposes.

To use one or another depends on whether you need the default formatter and the silencing. If you don't, Logger will work just fine and also save a tiny bit of overhead. Or you can prefer AS::Logger, because it's more universal.

Related