新手问题 rails 调试时如何将结果输出到 console?[已解决]

siyang1982 · 2012年02月22日 · 最后由 siyang1982 回复于 2012年05月27日 · 7710 次阅读

居然没有搜到,催悲的使用 raise 中。。。

把默认的 logger 改成 Logger.new(STDOUT),比如

ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveResource::Base.logger = Logger.new(STDOUT)
ActionController::Base.logger = Logger.new(STDOUT)
logger.debug(string)

#3 楼 @jonny 在我项目中我是这样做的: @@log = Logger.new("xx/xx/xx/xx.log") 然后把这日志写到这个文件中用"tail"的方式浏览!

多谢各位。

#1 楼 @vkill 看了下 pry,好像很复杂,收了慢慢看。

#2 楼 @zhangyuan #3 楼 @jonny #4 楼 @lb563 logger 果然很好用

#5 楼 @jinleileiking puts 果然也行,比 logger 还简单

再给你的输出加点颜色吧

# colorize for console output
class String
  def colorize(color, options = {})
    background = options[:background] || options[:bg] || false
    style = options[:style]
    offsets = ["gray","red", "green", "yellow", "blue", "magenta", "cyan","white"]
    styles = ["normal","bold","dark","italic","underline","xx","xx","underline","xx","strikethrough"]
    start = background ? 40 : 30
    color_code = start + (offsets.index(color) || 8)
    style_code = styles.index(style) || 0
    "\e[#{style_code};#{color_code}m#{self}\e[0m"
  end
end

#7 楼 @jonny 也可以用 gem 包"colored" 给个效果图:

看到一个更简洁的,直接pp 'xxx'

需要 登录 后方可回复, 如果你还没有账号请 注册新账号