Class: SemanticLogger::Appender::Rabbitmq

Inherits:
Subscriber show all
Defined in:
lib/semantic_logger/appender/rabbitmq.rb

Instance Attribute Summary

Attributes inherited from Subscriber

#application, #formatter, #host, #logger, #metrics

Attributes inherited from Base

#filter, #name

Instance Method Summary collapse

Methods inherited from Subscriber

#level, #should_log?

Methods inherited from Base

#backtrace, #fast_tag, #level, #level=, #measure, #named_tags, #payload, #pop_tags, #push_tags, #should_log?, #silence, #tagged, #tags, #with_payload

Constructor Details

#initialize(queue_name: 'semantic_logger', rabbitmq_host: nil, metrics: false, **args, &block) ⇒ Rabbitmq

Create RabbitMQ appender using Bunny gem

Parameters:

queue_name: [String]
  Name of RabbitMQ queue where to stream logs to.
  This will be a queue bound to AMQP Default exchange
  Default: semantic_logger

level: [:trace | :debug | :info | :warn | :error | :fatal]
  Override the log level for this appender.
  Default: SemanticLogger.default_level

formatter: [Object|Proc|Symbol|Hash]
  An instance of a class that implements #call, or a Proc to be used to format
  the output from this appender
  Default: :json (See: #call)

filter: [Regexp|Proc]
  RegExp: Only include log messages where the class name matches the supplied.
  regular expression. All other messages will be ignored.
  Proc: Only include log messages where the supplied Proc returns true
        The Proc must return true or false.

host: [String]
  Name of this host to appear in log messages.
  Default: SemanticLogger.host

application: [String]
  Name of this application to appear in log messages.
  Default: SemanticLogger.application

RabbitMQ Parameters:

rabbitmq_host: [String]
  Host for AMQP connection. in Bunny this is called :host but here it has 
  been remapped to avoid conflicting with SemanticLogger's :host param.
  Default: localhost

username: [String]
  Username for AMQP connection
  Default: nil

password: [String]
  Password for AMQP connection
  Default: nil

more parameters supported by Bunny: http://rubybunny.info/articles/connecting.html


79
80
81
82
83
84
85
86
87
# File 'lib/semantic_logger/appender/rabbitmq.rb', line 79

def initialize(queue_name: 'semantic_logger', rabbitmq_host: nil, metrics: false, **args, &block)
  @queue_name             = queue_name
  @rabbitmq_args          = args.dup
  @rabbitmq_args[:host]   = rabbitmq_host
  @rabbitmq_args[:logger] = logger

  super(level: level, formatter: formatter, filter: filter, application: application, host: host, metrics: metrics, &block)
  reopen
end

Instance Method Details

#closeObject



95
96
97
98
99
100
# File 'lib/semantic_logger/appender/rabbitmq.rb', line 95

def close
  @channel&.close
  @channel = nil
  @connection&.close
  @connection = nil
end

#default_formatterObject

Use JSON Formatter by default.



111
112
113
# File 'lib/semantic_logger/appender/rabbitmq.rb', line 111

def default_formatter
  SemanticLogger::Formatters::Json.new
end

#flushObject



106
107
108
# File 'lib/semantic_logger/appender/rabbitmq.rb', line 106

def flush
  # NOOP
end

#log(log) ⇒ Object



102
103
104
# File 'lib/semantic_logger/appender/rabbitmq.rb', line 102

def log(log)
  queue.publish(formatter.call(log, self))
end

#queueObject



115
116
117
# File 'lib/semantic_logger/appender/rabbitmq.rb', line 115

def queue
  @queue ||= @channel.queue(@queue_name)
end

#reopenObject



89
90
91
92
93
# File 'lib/semantic_logger/appender/rabbitmq.rb', line 89

def reopen
  @connection = Bunny.new(@rabbitmq_args)
  @connection.start
  @channel = @connection.create_channel
end