Class: Lumberjack::RedisDevice

Inherits:
Device
  • Object
show all
Defined in:
lib/lumberjack/redis_device.rb

Overview

This Lumberjack device logs output to a redis list. The redis list will automatically truncate to a given size to prevent running out of memory on the server. This is not intended to be a scalable logging solution, but it can be useful as an additional logging tool to expose recent logs.

Constant Summary collapse

VERSION =
::File.read(::File.join(__dir__, "..", "..", "VERSION")).strip.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, redis:, limit: 10_000, ttl: nil) ⇒ RedisDevice

Create a device. The name will be used as the key for the log entries in redis.

The redis object can either be a ‘Redis` instance or a block that yields a `Redis` instance.

You can also specify a time to live in seconds (ttl) and set the limit for the size of the list.



25
26
27
28
29
30
# File 'lib/lumberjack/redis_device.rb', line 25

def initialize(name:, redis:, limit: 10_000, ttl: nil)
  @name = name
  @redis = redis
  @ttl = ttl.to_i
  @limit = limit
end

Instance Attribute Details

#limitObject (readonly)

Returns the value of attribute limit.



17
18
19
# File 'lib/lumberjack/redis_device.rb', line 17

def limit
  @limit
end

#nameObject (readonly)

Returns the value of attribute name.



17
18
19
# File 'lib/lumberjack/redis_device.rb', line 17

def name
  @name
end

#ttlObject (readonly)

Returns the value of attribute ttl.



17
18
19
# File 'lib/lumberjack/redis_device.rb', line 17

def ttl
  @ttl
end

Instance Method Details

#datetime_formatString?

Get the datetime format used for formatting time values in log entries.

Returns:

  • (String, nil)

    the datetime format string, or nil if no custom format is set



68
69
70
# File 'lib/lumberjack/redis_device.rb', line 68

def datetime_format
  @time_formatter&.format
end

#datetime_format=(format) ⇒ Object

Set the datetime format for formatting time values in log entries.

Parameters:

  • format (String)

    the datetime format string to use for time formatting



74
75
76
# File 'lib/lumberjack/redis_device.rb', line 74

def datetime_format=(format)
  @time_formatter = Lumberjack::Formatter::DateTimeFormatter.new(format)
end

#exists?Boolean

Return true if the logs exist in redis. Will return false if the logs have expired.

Returns:

  • (Boolean)


52
53
54
55
56
57
58
# File 'lib/lumberjack/redis_device.rb', line 52

def exists?
  retval = redis.exists(name)
  if retval.is_a?(Integer)
    retval = (retval > 0)
  end
  retval
end

#last_written_atObject

Return the timestamp of the last log entry.



61
62
63
64
# File 'lib/lumberjack/redis_device.rb', line 61

def last_written_at
  entry = read(1).first
  entry&.time
end

#read(count = limit) ⇒ Object

Read a set number of entries from the list. The result will be an array of Lumberjack::LogEntry objects.



45
46
47
48
# File 'lib/lumberjack/redis_device.rb', line 45

def read(count = limit)
  docs = redis.lrange(name, 0, count - 1)
  docs.collect { |json| entry_from_json(json) }
end

#redisRedis

Get the Redis connection instance.

Returns:

  • (Redis)

    the Redis connection, either from the stored instance or by calling the proc



80
81
82
83
84
85
86
# File 'lib/lumberjack/redis_device.rb', line 80

def redis
  if @redis.is_a?(Proc)
    @redis.call
  else
    @redis
  end
end

#write(entry) ⇒ Object

Write an entry to the list in redis



33
34
35
36
37
38
39
40
41
# File 'lib/lumberjack/redis_device.rb', line 33

def write(entry)
  data = entry_as_json(entry)
  json = dump_json(data)
  redis.multi do |transaction|
    transaction.lpush(name, json)
    transaction.ltrim(name, 0, limit - 1)
    transaction.expire(name, ttl) if ttl && ttl > 0
  end
end