简体   繁体   中英

Log changes to a model with Librato

I have a Devise model:

class Account < ActiveRecord::Base
  devise ::trackable

I'd like to see the number of times an Account is created and signed in to, in Librato.

Using Librato's log stream parsing you can record the events with a $stdout.puts .

I recommend extracting this log to a concern , and using model callbacks to monitor changes.

We can create a file in lib/librato/account.rb :

module Librato
  module Account
    extend ActiveSupport::Concern

    included do
      after_create do
        $stdout.puts 'count#account.create=1'
      end

      after_save if: :current_sign_in_at_changed? do
        $stdout.puts 'count#account.sign_in=1'
      end
    end
  end
end

And then include it in your model, thus:

class Account < ActiveRecord::Base
  include Librato::Account

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM