简体   繁体   English

Rails投放到控制台

[英]Rails puts to console

In my Rails app, there are some cases where code is being outputted with the puts command (for debugging purposes). 在我的Rails应用程序中,有些情况下使用puts命令输出代码(用于调试)。 Is there anyway to follow this output in the rails console (with the rails c command)? 无论如何,是否可以在rails控制台中跟踪此输出(使用rails c命令)? Or is there any other way to debug/view logs in the rails console? 还是有其他方法可以在Rails控制台中调试/查看日志?

Thanks! 谢谢!

For debugging, use Rails.logger instead of puts . 要进行调试,请使用Rails.logger而不是puts For example: 例如:

Rails.logger.info "Some debugging info"

This will be logged to a log file in rails_app_root/log directory. 这将被记录到rails_app_root/log目录中的日志文件中。 If you are running in development environment locally, it will be logged to rails_app/log/development.log file. 如果您在本地开发环境中运行,它将记录到rails_app/log/development.log文件中。

Now, to see the log as they come in you can use tail command, like this: 现在,要查看进入的日志,可以使用tail命令,如下所示:

tail -f log/development.log

Hope it helps. 希望能帮助到你。

Rails.logger is the best solution,one more think i want to add,if you want separate log file you could use like this Rails.logger是最好的解决方案,还有一个我想添加,如果您想要单独的日志文件,可以这样使用

def read(args)    
 unless args.blank?
   cache_key= self.get_cache_key(args)
 end
 logger = Logger.new("#{Rails.root}/log/cache_read.log")
 logger.error("cache read scope == #{cache_key.to_s}")
end

so cache_read.log file having only this method log only. 因此只具有此方法的cache_read.log文件仅记录日志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM