简体   繁体   English

使用Librato记录对模型的更改

[英]Log changes to a model with Librato

I have a Devise model: 我有一个Devise模型:

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. 我想查看在Librato中创建和登录Account的次数。

Using Librato's log stream parsing you can record the events with a $stdout.puts . 使用Librato的日志流解析,您可以使用$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 : 我们可以在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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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