Most of the time, when you are interested in any log output,
- you see the logs directly on your console
- or you tail / grep some logfile in a separater terminal window
In rare cases it's helpful, to redirect the Logger output temporary to e.g. STDOUT
.
Rails.logger = Logger.new(STDOUT)
ActiveRecord::Base.logger = Logger.new(STDOUT)
User.save!
#=> D, [2025-09-08T11:12:26.683106 #1094157] DEBUG -- : User Load (1.1ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT $1 [["LIMIT", 1]]
Many frameworks in Rails have there own logger configuration and are set on boot. We set ActiveRecord::Base.logger
in the example, but logger exists also for e.g. ActiveStorage.logger
and so on.
Consider to reset the logger settings afterwards in a non temporary context:
rails_logger_was = Rails.logger
active_record_logger_was = ActiveRecord::Base.logger
begin
Rails.logger = Logger.new(STDOUT)
ActiveRecord::Base.logger = Logger.new(STDOUT)
User.save!
ensure
Rails.logger = rails_logger_was
ActiveRecord::Base.logger = active_record_logger_was
end
Posted by Emanuel to makandra dev (2025-09-08 08:25)