Created
December 23, 2011 20:01
-
-
Save schneems/1515221 to your computer and use it in GitHub Desktop.
delayed log
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # DelayedLog Goals: decouple logging and sending logs, keep controllers Dry | |
| # Example | |
| # ======= | |
| ## Define how LogWrapper writes to logs | |
| # DelayedLog.on_send do |options| | |
| # | |
| # end | |
| ## Build a log to send | |
| # log = DelayedLog.new(:user => user.to_json) | |
| # log.build(:friend_count => 10) | |
| ## Send a log | |
| # log.send # executes contents of on_send block | |
| # gives you the ability to set defaults using an arround_filter and lets you add-to or modify those values | |
| # lets you send queue up multiple logs by calling | |
| # TODO | |
| # ====== | |
| # - rescue | |
| # TODO ? | |
| # ======== | |
| # - Drink scotch | |
| class DelayedLogs | |
| attr_accessor :logs, :defaults | |
| class << self; attr_accessor :on_send_block end | |
| def initialize(options = {}) | |
| self.defaults = options | |
| self.logs = [] | |
| end | |
| def logs_or_default | |
| @logs.presence || [DelayedLogs::Log.new(defaults)] | |
| end | |
| def self.on_send(&block) | |
| self.on_send_block = block | |
| end | |
| def on_send_block | |
| self.class.on_send_block | |
| end | |
| def build(event) | |
| logs << DelayedLogs::Log.new(defaults.merge(event)) | |
| return self | |
| end | |
| def send_log | |
| Rails.logger.error "ERROR: Set DelayedLogs.on_send plz" and return false if on_send_block.blank? | |
| logs_or_default.each {|log| on_send_block.call(log.event)} | |
| end | |
| class Log | |
| attr_accessor :event | |
| def initialize(options) | |
| self.event = options | |
| end | |
| end | |
| end | |
| ## Configure the on send action of DelayedLog | |
| # Don't know why but this only works correctly for the first call if put in the initializers | |
| DelayedLogs.on_send do |options| | |
| #=== Slop ===# | |
| Gowalla::Slop.log(options) | |
| #=== Statsd ===# | |
| # TODO: Statsd.increment("") | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment