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 inteneded to be a scalable logging solution, but it can be useful as an additional logging tool to expose recent logs.
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 ⇒ Object
- #datetime_format=(format) ⇒ Object
-
#initialize(name:, redis:, limit: 10_000, ttl: nil) ⇒ RedisDevice
constructor
Create a device.
-
#read(count = limit) ⇒ Object
Read a set number of entries from the list.
- #redis ⇒ Object
-
#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 entris 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.
21 22 23 24 25 26 |
# File 'lib/lumberjack_redis_device.rb', line 21 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.
13 14 15 |
# File 'lib/lumberjack_redis_device.rb', line 13 def limit @limit end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
13 14 15 |
# File 'lib/lumberjack_redis_device.rb', line 13 def name @name end |
#ttl ⇒ Object (readonly)
Returns the value of attribute ttl.
13 14 15 |
# File 'lib/lumberjack_redis_device.rb', line 13 def ttl @ttl end |
Instance Method Details
#datetime_format ⇒ Object
46 47 48 |
# File 'lib/lumberjack_redis_device.rb', line 46 def datetime_format @time_formatter.format if @time_formatter end |
#datetime_format=(format) ⇒ Object
50 51 52 |
# File 'lib/lumberjack_redis_device.rb', line 50 def datetime_format=(format) @time_formatter = Lumberjack::Formatter::DateTimeFormatter.new(format) end |
#read(count = limit) ⇒ Object
Read a set number of entries from the list. The result will be an array of Lumberjack::LogEntry objects.
41 42 43 44 |
# File 'lib/lumberjack_redis_device.rb', line 41 def read(count = limit) docs = redis.lrange(name, 0, count - 1) docs.collect { |json| entry_from_json(json) } end |
#redis ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/lumberjack_redis_device.rb', line 54 def redis if @redis.is_a?(Proc) @redis.call else @redis end end |
#write(entry) ⇒ Object
Write an entry to the list in redis
29 30 31 32 33 34 35 36 37 |
# File 'lib/lumberjack_redis_device.rb', line 29 def write(entry) data = entry_as_json(entry) json = MultiJson.dump(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 |