7

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?

4
  • 1
    Does this answer your question? Rails: How to change the logger filename?
    – Eyeslandic
    Oct 19, 2020 at 21:24
  • 2
    @Eyeslandic Thank you for your comment. Yes, but partially. The suggested way is what I have been trying in Rails 6 if rather blindly and so far it seems to be working. I would like to understand why. Is ActiveSupport::Logger just an internal-use class working behind the scene, and Rails-6 app developers do not have to care? If so, that would answer my question. In older versions of Rails, I saw people using ActiveSupport::Logger, I think. Oct 20, 2020 at 1:22
  • 1
    ActiveSupport::Logger adds some conveniences on top of Logger. For example, temporary silencing of the logger or broadcasting to multiple loggers. If you don't need these features, I think the standard Logger should work just fine and without the overhead.
    – RocketR
    Oct 28, 2021 at 11:14
  • 2
    @RocketR I see, so ActiveSupport::Logger is indeed a wrapper. Would you write it as an answer, perhaps with relevant references, maybe? Oct 28, 2021 at 17:45

1 Answer 1

4

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.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.