强大的 ActiveSupport::Notifications

 ActiveSupport::Notifications   是Rails3提供的 Rails系统的事件提醒通知机制, 非常强大。 我们可以非常容易的捕捉到 系统运行的各个状态时的参数,时间等。

一个简单的可以查看其功能的例子
新建/config/initializers/notifications.rb
    ActiveSupport::Notifications.subscribe do |name, start, finish, id, payload|  
      Rails.logger.debug(["notification:", name, start, finish, id, payload].join(" "))  
    end  

运行 rails s ,访问首页,可以收到如下请求

Started GET "/" for 127.0.0.1 at 2012-09-26 14:40:43 +0800
Processing by WelcomeController#index as HTML
notification: start_processing.action_controller 2012-09-26 14:40:43 +0800 2012-09-26 14:40:43 +0800 7b8c2a131277e455e268 {:controller=>"WelcomeController", :action=>"index", :params=>{"controller"=>"welcome", "action"=>"index"}, :format=>:html, :method=>"GET", :path=>"/"}
  User Load (0.1ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
notification: sql.active_record 2012-09-26 14:40:43 +0800 2012-09-26 14:40:43 +0800 7b8c2a131277e455e268 {:sql=>"SELECT  `users`.* FROM `users`  WHERE `users`.`id` = 1 LIMIT 1", :name=>"User Load", :connection_id=>97717390, :binds=>[]}
Read fragment views/127.0.0.1:3000/index (0.2ms)
notification: read_fragment.action_controller 2012-09-26 14:40:43 +0800 2012-09-26 14:40:43 +0800 7b8c2a131277e455e268 {:key=>"views/127.0.0.1:3000/index"}
Completed 200 OK in 3ms (ActiveRecord: 0.1ms)
notification: process_action.action_controller 2012-09-26 14:40:43 +0800 2012-09-26 14:40:43 +0800 7b8c2a131277e455e268 {:controller=>"WelcomeController", :action=>"index", :params=>{"controller"=>"welcome", "action"=>"index"}, :format=>:html, :method=>"GET", :path=>"/", :status=>200, :view_runtime=>nil, :db_runtime=>0.143454}

其中各个参数的意思
start_processing.action_controller 2012-09-26 14:40:43 +0800 2012-09-26 14:40:43 +0800 7b8c2a131277e455e268 {:controller=>"WelcomeController", :action=>"index", :params=>{"controller"=>"welcome", "action"=>"index"}, :format=>:html, :method=>"GET", :path=>"/"}

name 是 notifications的名称, start , finish 分别对应 开始时间,结束时间, id 是系统分配的ID号码, payload 是 一个传递的hash值


Rails默认的事件有
start_processing.action_controller
notification: sql.active_record
notification: sql.active_record
notification: sql.active_record
notification: !render_template.action_view
notification: !render_template.action_view
notification: render_template.action_view
notification: process_action.action_controller


我们也可以自己定义 notification
    class Product < ActiveRecord::Base  
      def self.search(search)  
        if search  
          ActiveSupport::Notifications.instrument("products.search", :search => search)  
          where('name LIKE ?', "%#{search}%")  
        else  
          scoped  
        end  
      end  
    end  


然后可以在任何地方使用如下代码抓取 这个通知
    ActiveSupport::Notifications.subscribe "products.search"  do |name, start, finish, id, payload|  
      Rails.logger.debug "SEARCH: #{payload[:search]}"  
    end  






参考文档:
http://www.taobaotesting.com/blogs/qa?bid=15025
http://asciicasts.com/episodes/249-notifications-in-rails-3
http://api.rubyonrails.org/classes/ActiveSupport/Notifications.html


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值