开发者

puts doesn't print stuff to console

i'm using POW for local rails development. i don't know why, but i can't print or puts information to my development.log. i want to puts the content of variables to console /开发者_C百科 log from my controller. any advice?

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

thanks!


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


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::


Another option is to use the rails tagging logger, 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. 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:

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

Hope this helps someone.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜