Class: SemanticLogger::Appender::Rabbitmq

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

Instance Attribute Summary

Attributes inherited from Subscriber

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

Instance Method Summary collapse

Methods inherited from Subscriber

#batch_by_default?, #console_output?, #console_stream, #level, #should_log?

Constructor Details

#initialize(queue_name: "semantic_logger", rabbitmq_host: nil, level: nil, formatter: nil, filter: nil, application: nil, environment: nil, host: nil, metrics: true, **args) ⇒ 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

metrics: [Boolean]
Also send metrics only events to rabbitmq.
Default: true

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


83
84
85
86
87
88
89
90
91
92
93
# File 'lib/semantic_logger/appender/rabbitmq.rb', line 83

def initialize(queue_name: "semantic_logger", rabbitmq_host: nil,
               level: nil, formatter: nil, filter: nil, application: nil, environment: nil, host: nil, metrics: true,
               **args, &)
  @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, environment: environment, host: host, metrics: metrics, &)
  reopen
end

Instance Method Details

#closeObject



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

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

#default_formatterObject

Use JSON Formatter by default.



117
118
119
# File 'lib/semantic_logger/appender/rabbitmq.rb', line 117

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

#flushObject



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

def flush
  # NOOP
end

#log(log) ⇒ Object



108
109
110
# File 'lib/semantic_logger/appender/rabbitmq.rb', line 108

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

#queueObject



121
122
123
# File 'lib/semantic_logger/appender/rabbitmq.rb', line 121

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

#reopenObject



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

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