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

前端 未结 5 1549
温柔的废话
温柔的废话 2020-12-04 15:08

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/s         


        
5条回答
  •  盖世英雄少女心
    2020-12-04 15:33

    For Rails 4.x the log level is configured a bit different than in Rails 3.x

    Add this to config/environment/test.rb

    # Enable stdout logger
    config.logger = Logger.new(STDOUT)
    
    # Set log level
    config.log_level = :ERROR
    

    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#L70

    Environment variable

    As a bonus, you can allow overwriting the log level using an environment variable with a default value like so:

    # default :ERROR
    config.log_level = ENV.fetch("LOG_LEVEL", "ERROR")
    

    And then running tests from shell:

    # Log level :INFO (the value is uppercased in bootstrap.rb)
    $ LOG_LEVEL=info rake test
    
    # Log level :ERROR
    $ rake test
    

提交回复
热议问题