Class: Cabin::Outputs::EM::StdlibLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/cabin/outputs/em/stdlib-logger.rb

Overview

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.

Instance Method Summary collapse

Constructor Details

#initialize(logger) ⇒ StdlibLogger

Returns a new instance of StdlibLogger.



11
12
13
14
15
16
# File 'lib/cabin/outputs/em/stdlib-logger.rb', line 11

def initialize(logger)
  @logger_queue = EM::Queue.new
  @logger = logger
  # Consume log lines from a queue and send them with logger
  consumer
end

Instance Method Details

#<<(data) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/cabin/outputs/em/stdlib-logger.rb', line 33

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

#consumerObject



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/cabin/outputs/em/stdlib-logger.rb', line 18

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