Class: Semlog::RabbitNotifier
- Inherits:
-
Object
- Object
- Semlog::RabbitNotifier
- Defined in:
- lib/semlog/rabbit_notifier.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
Returns the value of attribute connection.
-
#default_options ⇒ Object
Returns the value of attribute default_options.
-
#exhange_name ⇒ Object
Returns the value of attribute exhange_name.
-
#host ⇒ Object
Returns the value of attribute host.
-
#port ⇒ Object
Returns the value of attribute port.
-
#pw ⇒ Object
Returns the value of attribute pw.
-
#user ⇒ Object
Returns the value of attribute user.
-
#vhost ⇒ Object
Returns the value of attribute vhost.
Instance Method Summary collapse
- #channel ⇒ Object
- #connect ⇒ Object
- #data_to_json(data) ⇒ Object
- #exchange ⇒ Object
- #gelfify(data) ⇒ Object
-
#initialize(host:, port:, vhost:, exchange_name:, user:, pw:) ⇒ RabbitNotifier
constructor
A new instance of RabbitNotifier.
- #notify!(data) ⇒ Object
Constructor Details
#initialize(host:, port:, vhost:, exchange_name:, user:, pw:) ⇒ RabbitNotifier
Returns a new instance of RabbitNotifier.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/semlog/rabbit_notifier.rb', line 14 def initialize host:, port:, vhost:, exchange_name:, user:, pw: @connection = nil @host = host @port = port @vhost = vhost @exhange_name = exchange_name @user = user @pw = pw $logger.debug "RabbitNotifier>host: #{@host} port #{@port} vhost #{@vhost} " = {} ['version'] = "1.1" ['host'] ||= Socket.gethostname ['level'] ||= Semlog::UNKNOWN ['facility'] ||= 'RabbitNotifier' end |
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection.
12 13 14 |
# File 'lib/semlog/rabbit_notifier.rb', line 12 def connection @connection end |
#default_options ⇒ Object
Returns the value of attribute default_options.
12 13 14 |
# File 'lib/semlog/rabbit_notifier.rb', line 12 def end |
#exhange_name ⇒ Object
Returns the value of attribute exhange_name.
12 13 14 |
# File 'lib/semlog/rabbit_notifier.rb', line 12 def exhange_name @exhange_name end |
#host ⇒ Object
Returns the value of attribute host.
12 13 14 |
# File 'lib/semlog/rabbit_notifier.rb', line 12 def host @host end |
#port ⇒ Object
Returns the value of attribute port.
12 13 14 |
# File 'lib/semlog/rabbit_notifier.rb', line 12 def port @port end |
#pw ⇒ Object
Returns the value of attribute pw.
12 13 14 |
# File 'lib/semlog/rabbit_notifier.rb', line 12 def pw @pw end |
#user ⇒ Object
Returns the value of attribute user.
12 13 14 |
# File 'lib/semlog/rabbit_notifier.rb', line 12 def user @user end |
#vhost ⇒ Object
Returns the value of attribute vhost.
12 13 14 |
# File 'lib/semlog/rabbit_notifier.rb', line 12 def vhost @vhost end |
Instance Method Details
#channel ⇒ Object
68 69 70 71 |
# File 'lib/semlog/rabbit_notifier.rb', line 68 def channel connect @channel ||= @connection.create_channel end |
#connect ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/semlog/rabbit_notifier.rb', line 33 def connect unless @connection @connection = Bunny.new(:host => @host, :vhost => @vhost, :user => @user, :password => @pw) @connection.start end @connection end |
#data_to_json(data) ⇒ Object
60 61 62 |
# File 'lib/semlog/rabbit_notifier.rb', line 60 def data_to_json(data) Oj.dump(gelfify(data)) end |
#exchange ⇒ Object
73 74 75 |
# File 'lib/semlog/rabbit_notifier.rb', line 73 def exchange @exchange ||= channel.fanout(exhange_name, durable: true) end |
#gelfify(data) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/semlog/rabbit_notifier.rb', line 41 def gelfify(data) gdata = .dup data.keys.each do |key| value, key_s = data[key], key.to_s if ['host', 'level', 'version', 'short_message', 'full_message', 'timestamp', 'facility', 'line', 'file'].index(key_s) gdata[key_s] = value elsif key_s == 'action' gdata["_application_action"] = value elsif key_s == 'id' gdata["_application_id"] = value elsif key_s[0] != '_' gdata["_#{key_s}"] = value else gdata[key_s] = value end end gdata end |
#notify!(data) ⇒ Object
64 65 66 |
# File 'lib/semlog/rabbit_notifier.rb', line 64 def notify!(data) exchange.publish(data_to_json(data)) end |