Ruby-on-rails puts 不打印东西到控制台

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5673584/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:56:27  来源:igfitidea点击:

puts doesn't print stuff to console

ruby-on-railsrubyprintingconsolelogging

提问by trnc

i'm using POWfor local rails development. i don't know why, but i can't printor putsinformation to my development.log. i want to puts the content of variables to console / log from my controller. any advice?

我正在使用POW进行本地 Rails 开发。我不知道为什么,但我无法打印信息放入我的 development.log。我想将变量的内容从我的控制器放到控制台/日志中。有什么建议吗?

i read my logs with tail -f logs/development.log

我读了我的日志 tail -f logs/development.log

thanks!

谢谢!

回答by sarnold

Instead of puts, try logger.info(). Logging in Railsis very flexible, but it does mean that you might not be able to use the simplest tools sometimes.

而不是puts,尝试logger.info()在 Rails 中登录非常灵活,但这确实意味着有时您可能无法使用最简单的工具。

回答by opsb

If you're doing debugging and only want to see some messages in the logs you can do the following:

如果您正在进行调试并且只想查看日志中的一些消息,您可以执行以下操作:

Rails.logger.debug("debug::" + person.name)

and

$ pow logs | grep debug::

now you'll only see logging messages that start with debug::

现在您将只看到以 debug:: 开头的日志消息:

回答by opsb

Another option is to use the rails tagging logger, http://api.rubyonrails.org/classes/ActiveSupport/TaggedLogging.html.

另一种选择是使用 rails 标记记录器,http://api.rubyonrails.org/classes/ActiveSupport/TaggedLogging.html

logger = ActiveSupport::TaggedLogging.new(Logger.new(STDOUT))
logger.tagged('BCX') { logger.info 'Stuff' }                            # Logs "[BCX] Stuff"

$ pow logs | grep BCX

回答by Zack Morris

For anyone who still can't get it to work, remember that Ruby doesn't use semicolons. They are only used to chain commands. I was adding them at the end due to muscle memory (coming from PHP), so the ruby console thought I was still entering commands:

对于仍然无法使用它的任何人,请记住 Ruby 不使用分号。它们仅用于链接命令。由于肌肉记忆(来自 PHP),我在最后添加了它们,所以 ruby​​ 控制台认为我仍在输入命令:

irb(main):001:0> puts "hi";
irb(main):002:0* puts "hi"
hi
hi
=> nil

Hope this helps someone.

希望这可以帮助某人。