Class: LogStashLogger::Device::Redis

Inherits:
Connectable show all
Defined in:
lib/logstash-logger/device/redis.rb

Constant Summary collapse

DEFAULT_LIST =
'logstash'

Instance Attribute Summary collapse

Attributes inherited from Connectable

#buffer_logger

Attributes inherited from Base

#error_logger, #io, #sync

Instance Method Summary collapse

Methods inherited from Connectable

#close, #connected?, #flush, #on_full_buffer_receive, #to_io, #write

Methods included from Buffer

#buffer_flush, #buffer_full?, #buffer_initialize, #buffer_receive, #reset_buffer

Methods inherited from Base

#close, #flush, #to_io, #unrecoverable_error?, #write

Constructor Details

#initialize(opts) ⇒ Redis



10
11
12
13
14
15
16
17
18
# File 'lib/logstash-logger/device/redis.rb', line 10

def initialize(opts)
  super
  @list = opts.delete(:list) || DEFAULT_LIST
  @buffer_group = @list

  normalize_path(opts)

  @redis_options = opts
end

Instance Attribute Details

#listObject

Returns the value of attribute list



8
9
10
# File 'lib/logstash-logger/device/redis.rb', line 8

def list
  @list
end

Instance Method Details

#close!Object



53
54
55
# File 'lib/logstash-logger/device/redis.rb', line 53

def close!
  @io && @io.quit
end

#connectObject



20
21
22
# File 'lib/logstash-logger/device/redis.rb', line 20

def connect
  @io = ::Redis.new(@redis_options)
end

#reconnectObject



24
25
26
27
28
# File 'lib/logstash-logger/device/redis.rb', line 24

def reconnect
  @io.client.reconnect
rescue => e
  log_error(e)
end

#with_connectionObject



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/logstash-logger/device/redis.rb', line 30

def with_connection
  connect unless connected?
  yield
rescue ::Redis::InheritedError
  reconnect
  retry
rescue => e
  log_error(e)
  close(flush: false)
  raise
end

#write_batch(messages, list = nil) ⇒ Object



42
43
44
45
46
47
# File 'lib/logstash-logger/device/redis.rb', line 42

def write_batch(messages, list = nil)
  list ||= @list
  with_connection do
    @io.rpush(list, messages)
  end
end

#write_one(message, list = nil) ⇒ Object



49
50
51
# File 'lib/logstash-logger/device/redis.rb', line 49

def write_one(message, list = nil)
  write_batch(message, list)
end