File: stdlib-logger.rb

package info (click to toggle)
ruby-cabin 0.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 272 kB
  • sloc: ruby: 1,306; makefile: 12
file content (42 lines) | stat: -rw-r--r-- 1,190 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
require "cabin"
require "eventmachine"

# Wrap Ruby stdlib's logger and make it EventMachine friendly. This
# allows you to output to a normal ruby logger with Cabin.
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.inspect}"
    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