Sha256: ec3805c4aeb04583876893236941630f1877db6447fbb926f985aa18195af45c

Contents?: true

Size: 1.31 KB

Versions: 14

Compression:

Stored size: 1.31 KB

Contents

require "cabin"
require "json"
require "eventmachine"

# Wrap Ruby stdlib's logger and make it EventMachine friendly. This
# allows you to output to a normal ruby logger with Cabin. Since
# Ruby's Logger has a love for strings alone, this wrapper will
# convert the data/event to json before sending it to Logger.
class Cabin::Outputs::EM::StdlibLogger
  public
  def initialize(logger)
    @logger_queue = EM::Queue.new
    @logger = logger
    # Consume log lines from a queue and send them with logger
    consumer
  end

  def consumer
    line_sender = Proc.new do |line|
      # This will call @logger.info(data) or something similar
      @logger.send(line[:method], line[:message])
      EM::next_tick do
        # Pop another line off the queue and do it again
        @logger_queue.pop(&line_sender)
      end
    end
    # Pop a line off the queue and send it with logger
    @logger_queue.pop(&line_sender)
  end

  # Receive an event
  public
  def <<(data)
    line = Hash.new
    line[:method] = data[:level] || "info"
    line[:message] = "#{data[:message]} #{data.to_json}"
    if EM::reactor_running?
      # Push line onto queue for later sending
      @logger_queue.push(line)
    else
      # This will call @logger.info(data) or something similar
      @logger.send(line[:method], line[:message])
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
cabin-0.4.4 lib/cabin/outputs/em/stdlib-logger.rb
cabin-0.4.3 lib/cabin/outputs/em/stdlib-logger.rb
cabin-0.4.2 lib/cabin/outputs/em/stdlib-logger.rb
cabin-0.4.1 lib/cabin/outputs/em/stdlib-logger.rb
cabin-0.3.8 lib/cabin/outputs/em/stdlib-logger.rb
cabin-0.3.7 lib/cabin/outputs/em/stdlib-logger.rb
cabin-0.3.6 lib/cabin/outputs/em/stdlib-logger.rb
cabin-0.3.2 lib/cabin/outputs/em/stdlib-logger.rb
cabin-0.3.1 lib/cabin/outputs/em/stdlib-logger.rb
cabin-0.3.0 lib/cabin/outputs/em/stdlib-logger.rb
cabin-0.2.3 lib/cabin/outputs/em/stdlib-logger.rb
cabin-0.2.2 lib/cabin/outputs/em/stdlib-logger.rb
cabin-0.2.1 lib/cabin/outputs/em/stdlib-logger.rb
cabin-0.1.8 lib/cabin/outputs/em/stdlib-logger.rb