強大的 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


發佈了198 篇原創文章 · 獲贊 8 · 訪問量 27萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章