Same as title: How to get Rails.logger
printing to the console/stdout when running rspec? Eg.
Rails.logger.info "I WANT this to go to console/stdout when rspec is running"
puts "Like how the puts function works"
I still want Rails.logger
to go to log/test.log
too.
For Rails 4, see this answer.
For Rails 3.x, configure a logger in
config/environments/test.rb
:This will interleave any errors that are logged during testing to STDOUT. You may wish to route the output to STDERR or use a different log level instead.
Sending these messages to both the console and a log file requires something more robust than Ruby's built-in Logger class. The logging gem will do what you want. Add it to your
Gemfile
, then set up two appenders inconfig/environments/test.rb
:Tail the log as a background job (&) and it will interleave with rspec output.
You can define a method in spec_helper.rb that sends a message both to Rails.logger.info and to puts and use that for debugging:
A solution that I like, because it keeps rspec output separate from actual rails log output, is to do the following:
$ tail -f $RAILS_APP_DIR/logs/test.log
ortail -f $RAILS_APP_DIR\logs\test.log
for Window usersIf you are running a multi-pane terminal like iTerm, this becomes even more fun and you have
rspec
and thetest.log
output side by side.For Rails 4.x the log level is configured a bit different than in Rails 3.x
Add this to
config/environment/test.rb
The logger level is set on the logger instance from
config.log_level
at: https://github.com/rails/rails/blob/v4.2.4/railties/lib/rails/application/bootstrap.rb#L70Environment variable
As a bonus, you can allow overwriting the log level using an environment variable with a default value like so:
And then running tests from shell: