诗歌rails之 Logger

关键字: Rails Logger
Rails在controller里自带了logger,我们可以用来做debug:
ruby代码
  1. def show  
  2.   @cart = current_cart  
  3.   logger.debug "Hello world! #{@cart.to_yaml}"  
  4.   # debug, info, warn, error, fatal  
  5. end  
我们可以在environment.rb里配置Logger的消息格式:
ruby代码
  1. class Logger  
  2.   def format_message(level, time, progname, msg)  
  3.     "#{time.to_s(:db)} #{level} -- #{msg}\n"  
  4.   end  
  5. end  
我们还可以在environments/production.rb里配置log_level
ruby代码
  1. config.log_level = :debug  
使用rake log:clear可以清空旧日志
在.irbrc里也可以设置Logger:
ruby代码
  1. if ENV.include?('RAILS_ENV') && !Object.const_defined?('RAILS_DEFAULT_LOGGER')  
  2.   require 'logger'  
  3.   Object.const_set('RAILS_DEFAULT_LOGGER', Logger.new(STDOUT))  
  4. end  
这样在script/console里的Model操作就会直接in place显示在console里
posted @ 2009-07-10 09:54  麦飞  阅读(2350)  评论(0编辑  收藏  举报