Sha256: 78a054916f25209e0d4187e6e4330462d792190fdc2a899e6c6d5cd226602ff6

Contents?: true

Size: 1 KB

Versions: 3

Compression:

Stored size: 1 KB

Contents

module Monkeyshines
  module Monitor

    #
    # Emits a log line but only every +iter_interval+ calls or +time_interval+
    # lapse.
    #
    # Since the contents of the block aren't called until the criteria are met,
    # you can put relatively expensive operations in the log without killing
    # your iteration time.
    #
    class PeriodicLogger < PeriodicMonitor
      #
      # Call with a block that returns a string or array to log.
      # If you return
      #
      # Ex: log if it has been at least 5 minutes since last announcement:
      #
      #   periodic_logger = Monkeyshines::Monitor::PeriodicLogger.new(:time => 300)
      #   loop do
      #     # ... stuff ...
      #     periodic_logger.periodically{ [morbenfactor, crunkosity, exuberance] }
      #   end
      #
      def periodically &block
        super do
          result = [ "%10d"%iter, "%7.1f"%since, "%7.1f"%rate, (block ? block.call : nil) ].flatten.compact
          Log.info result.join("\t")
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
monkeyshines-0.2.1 lib/monkeyshines/monitor/periodic_logger.rb
monkeyshines-0.2.0 lib/monkeyshines/monitor/periodic_logger.rb
monkeyshines-0.0.2 lib/monkeyshines/monitor/periodic_logger.rb