Class: Lumberjack::RedisDevice
- Inherits:
-
Device
- Object
- Device
- Lumberjack::RedisDevice
- 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
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#ttl ⇒ Object
readonly
Returns the value of attribute ttl.
Instance Method Summary collapse
-
#datetime_format ⇒ String?
Get the datetime format used for formatting time values in log entries.
-
#datetime_format=(format) ⇒ Object
Set the datetime format for formatting time values in log entries.
-
#exists? ⇒ Boolean
Return true if the logs exist in redis.
-
#initialize(name:, redis:, limit: 10_000, ttl: nil) ⇒ RedisDevice
constructor
Create a device.
-
#last_written_at ⇒ Object
Return the timestamp of the last log entry.
-
#read(count = limit) ⇒ Object
Read a set number of entries from the list.
-
#redis ⇒ Redis
Get the Redis connection instance.
-
#write(entry) ⇒ Object
Write an entry to the list in redis.
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
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
17 18 19 |
# File 'lib/lumberjack/redis_device.rb', line 17 def limit @limit end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
17 18 19 |
# File 'lib/lumberjack/redis_device.rb', line 17 def name @name end |
#ttl ⇒ Object (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_format ⇒ String?
Get the datetime format used for formatting time values in log entries.
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.
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.
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_at ⇒ Object
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 |
#redis ⇒ Redis
Get the Redis connection instance.
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 |