Class: RedisStreamLogger::LogDevice

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_stream_logger/log_device.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conn = nil, stream: 'rails-log') ⇒ LogDevice

Creates a new LogDevice that can be used as a sink for Ruby Logger

Parameters:

  • conn (Redis) (defaults to: nil)

    connection to Redis

  • stream (String) (defaults to: 'rails-log')

    name of key to write to

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/redis_stream_logger/log_device.rb', line 12

def initialize(conn = nil, stream: 'rails-log')
    @config = Config.new
    @closed = false
    # Just in case a whole new config is passed in like in the Railtie
    new_conf = yield @config if block_given?
    @config = new_conf if new_conf.is_a?(Config)
    @config.connection ||= conn
    @config.stream_name ||= stream
    raise ArgumentError, 'must provide connection' if @config.connection.nil?

    @q = Queue.new
    start
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



5
6
7
# File 'lib/redis_stream_logger/log_device.rb', line 5

def config
  @config
end

Instance Method Details

#closeObject



36
37
38
39
40
41
42
43
# File 'lib/redis_stream_logger/log_device.rb', line 36

def close
    return if @closed
    @q.push :exit
    @ticker.exit
    @writer.join
    @config.connection.close
    @closed = true
end

#reopen(log = nil) ⇒ Object



30
31
32
33
34
# File 'lib/redis_stream_logger/log_device.rb', line 30

def reopen(log = nil)
    close
    @config.connection._client.connect
    start
end

#write(msg) ⇒ Object



26
27
28
# File 'lib/redis_stream_logger/log_device.rb', line 26

def write(msg)
    @q.push msg
end