Search code examples
ruby-on-railsloggingrspecconsolestdout

How to get Rails.logger printing to the console/stdout when running rspec?


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.


Solution

  • For Rails 4, see this answer.

    For Rails 3.x, configure a logger in config/environments/test.rb:

    config.logger = Logger.new(STDOUT)
    config.logger.level = Logger::ERROR
    

    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 in config/environments/test.rb:

    logger = Logging.logger['test']
    logger.add_appenders(
        Logging.appenders.stdout,
        Logging.appenders.file('example.log')
    )
    logger.level = :info
    config.logger = logger