简体   繁体   English

puts不会打印到控制台

[英]puts doesn't print stuff to console

i'm using POW for local rails development. 我正在使用POW进行本地轨道开发。 i don't know why, but i can't print or puts information to my development.log. 我不知道为什么,但我不能打印信息放到我的development.log中。 i want to puts the content of variables to console / log from my controller. 我想将变量的内容从控制器放入控制台/日志。 any advice? 任何建议?

i read my logs with tail -f logs/development.log 我用tail -f logs/development.log读取我的日志

thanks! 谢谢!

Instead of puts , try logger.info() . 取而代之的puts ,尝试logger.info() Logging in Rails is very flexible, but it does mean that you might not be able to use the simplest tools sometimes. 登录Rails非常灵活,但它确实意味着您有时可能无法使用最简单的工具。

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 ::开头的日志消息

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

For anyone who still can't get it to work, remember that Ruby doesn't use semicolons. 对于仍然无法使用它的人,请记住Ruby不使用分号。 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: 由于肌肉记忆(来自PHP),我最后在最后添加它们,所以ruby控制台认为我还在输入命令:

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

Hope this helps someone. 希望这有助于某人。

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

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