Ruby-on-rails 运行 rspec 时如何将 Rails.logger 打印到控制台/标准输出?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11770552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get Rails.logger printing to the console/stdout when running rspec?
提问by s12chung
Same as title: How to get Rails.loggerprinting to the console/stdout when running rspec? Eg.
与标题相同:如何Rails.logger在运行 rspec 时打印到控制台/标准输出?例如。
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.loggerto go to log/test.logtoo.
我还是希望Rails.logger去log/test.log了。
采纳答案by Phil Calvin
For Rails 4, see this answer.
对于 Rails 4,请参阅此答案。
For Rails 3.x, configure a logger in config/environments/test.rb:
对于 Rails 3.x,在 中配置记录器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.
这会将测试期间记录的任何错误交错到 STDOUT。您可能希望将输出路由到 STDERR 或使用不同的日志级别。
Sending these messages to both the console and a log file requires something more robust than Ruby's built-in Loggerclass. The logginggem will do what you want. Add it to your Gemfile, then set up two appenders in config/environments/test.rb:
将这些消息同时发送到控制台和日志文件需要比 Ruby 的内置Logger类更健壮的东西。该记录的宝石会做你想要什么。将它添加到您的Gemfile,然后在 中设置两个附加程序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
回答by Thomas B Homburg
For Rails 4.x the log level is configured a bit different than in Rails 3.x
对于 Rails 4.x,日志级别的配置与 Rails 3.x 略有不同
Add this to config/environment/test.rb
将此添加到 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_levelat: https://github.com/rails/rails/blob/v4.2.4/railties/lib/rails/application/bootstrap.rb#L70
记录器级别在记录器实例config.log_level上设置: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:
然后从 shell 运行测试:
# Log level :INFO (the value is uppercased in bootstrap.rb)
$ LOG_LEVEL=info rake test
# Log level :ERROR
$ rake test
回答by Jared Beck
Tail the log as a background job(&) and it will interleave with rspec output.
将日志作为后台作业(&) 拖尾,它将与 rspec 输出交错。
tail -f log/test.log &
bundle exec rspec
回答by Shyam Habarakada
A solution that I like, because it keeps rspec output separate from actual rails log output, is to do the following:
我喜欢的一个解决方案是执行以下操作,因为它将 rspec 输出与实际的 rails 日志输出分开:
- Open a second terminal window or tab, and arrange it so that you can see both the main terminal you're running rspec on as well as the new one.
- Run a tail command in the second window so you see the rails log in the test environment. By default this can be like
$ tail -f $RAILS_APP_DIR/logs/test.logortail -f $RAILS_APP_DIR\logs\test.logfor Window users - Run your rspec suites
- 打开第二个终端窗口或选项卡,并对其进行排列,以便您可以看到正在运行 rspec 的主终端和新终端。
- 在第二个窗口中运行 tail 命令,以便您在测试环境中看到 rails 日志。默认情况下,这可以像
$ tail -f $RAILS_APP_DIR/logs/test.log或tail -f $RAILS_APP_DIR\logs\test.log适用于 Window 用户 - 运行你的 rspec 套件
If you are running a multi-pane terminal like iTerm, this becomes even more fun and you have rspecand the test.logoutput side by side.
如果你正在运行像的iTerm多窗格终端,这变得更加有趣,你有rspec和test.log并排输出侧。
回答by manglewood
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:
您可以在 spec_helper.rb 中定义一个方法,该方法将消息发送到 Rails.logger.info 并放置并使用它进行调试:
def log_test(message)
Rails.logger.info(message)
puts message
end

