Sha256: 34a301f925a9cc94a683d10813adf0f3b9dff100134941fa08df47564cb2f4ed

Contents?: true

Size: 1.02 KB

Versions: 3

Compression:

Stored size: 1.02 KB

Contents

module God
  
  class Logger < ::Logger
    attr_accessor :logs
    
    def initialize
      super(STDOUT)
      self.logs = {}
      @mutex = Mutex.new
    end
    
    def log(watch, level, text)
      # initialize watch log if necessary
      self.logs[watch.name] ||= Timeline.new(God::LOG_BUFFER_SIZE_DEFAULT)
      
      # push onto timeline for the given watch
      buf = StringIO.new
      templog = ::Logger.new(buf)
      templog.send(level, text)
      @mutex.synchronize do
        self.logs[watch.name] << [Time.now, buf.string]
      end
      templog.close
      
      # send to regular logger
      self.send(level, text)
    end
    
    def watch_log_since(watch_name, since)
      # initialize watch log if necessary
      self.logs[watch_name] ||= Timeline.new(God::LOG_BUFFER_SIZE_DEFAULT)
      
      # get and join lines since given time
      @mutex.synchronize do
        self.logs[watch_name].select do |x|
          x.first > since
        end.map do |x|
          x[1]
        end.join
      end
    end
  end
  
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
god-0.4.0 lib/god/logger.rb
god-0.4.1 lib/god/logger.rb
god-0.4.3 lib/god/logger.rb