Class: Log4r::RabbitOutputter

Inherits:
Outputter
  • Object
show all
Defined in:
lib/log4r/outputter/rabbitoutputter.rb

Overview

See log4r/logserver.rb

Instance Attribute Summary

Attributes inherited from Outputter

#formatter, #level, #name

Instance Method Summary collapse

Methods inherited from Outputter

[], []=, each, each_outputter, #flush, #only_at, stderr, stdout

Constructor Details

#initialize(_name, hash = {}) ⇒ RabbitOutputter

Returns a new instance of RabbitOutputter.



12
13
14
15
16
17
18
19
20
# File 'lib/log4r/outputter/rabbitoutputter.rb', line 12

def initialize(_name, hash={})
  # Configuration defaults
  super(_name, hash)
  stderr_log "Unable to find rabbit configuration file" unless load_config
  @config ||= {:host => "localhost"}
  @config.symbolize_keys!
  @queue_name = @config.delete(:queue) || ''
  start_bunny rescue nil
end

Instance Method Details

#create_channelObject



55
56
57
58
# File 'lib/log4r/outputter/rabbitoutputter.rb', line 55

def create_channel
  ch = @conn.create_channel
  @queue  = ch.queue(@queue_name, auto_delete: false, durable: true)
end

#load_configObject



29
30
31
32
33
34
35
# File 'lib/log4r/outputter/rabbitoutputter.rb', line 29

def load_config
  @config = if load_config_file("bunny.yml")
    @config[Rails.env]
  else
    load_config_file("rabbitmq.yml")
  end
end

#load_config_file(name) ⇒ Object



22
23
24
25
26
27
# File 'lib/log4r/outputter/rabbitoutputter.rb', line 22

def load_config_file(name)
  path = "#{Rails.root}/config/#{name}"
  if File.exist?(path)
    @config = YAML::load(IO.read(path)) 
  end
end

#start_bunnyObject



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/log4r/outputter/rabbitoutputter.rb', line 37

def start_bunny
  begin
    stderr_log "Starting Bunny Client"
    config = @config.clone
    config[:pass] &&= "**redacted**"
    stderr_log config
    @conn = Bunny.new @config
    @conn.start
    create_channel
  rescue Bunny::TCPConnectionFailed => e
    stderr_log "rescued from: #{e}. Unable to connect to Rabbit Server"
  end
end

#stderr_log(msg) ⇒ Object



51
52
53
# File 'lib/log4r/outputter/rabbitoutputter.rb', line 51

def stderr_log(msg)
  $stderr.puts "[#{Time.now.utc}] #{msg}"
end