Class: Ledis::Logger::LogDevice

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ LogDevice

Returns a new instance of LogDevice.



196
197
198
199
200
# File 'lib/ledis.rb', line 196

def initialize(*args, &block)
  config = Map.options_for!(args)
  config[:redis] ||= args.shift
  configure(config, &block)
end

Instance Attribute Details

#capObject

Returns the value of attribute cap.



191
192
193
# File 'lib/ledis.rb', line 191

def cap
  @cap
end

#configObject

Returns the value of attribute config.



190
191
192
# File 'lib/ledis.rb', line 190

def config
  @config
end

#cycleObject

Returns the value of attribute cycle.



193
194
195
# File 'lib/ledis.rb', line 193

def cycle
  @cycle
end

#listObject

Returns the value of attribute list.



194
195
196
# File 'lib/ledis.rb', line 194

def list
  @list
end

#stepObject

Returns the value of attribute step.



192
193
194
# File 'lib/ledis.rb', line 192

def step
  @step
end

Instance Method Details

#__write__Object



238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/ledis.rb', line 238

def write(message)
  begin
    redis.lpush(list, message)
  rescue Object => e
    error = "#{ e.message } (#{ e.class })\n#{ Array(e.backtrace).join(10.chr) }"
    STDERR.puts(error)
    STDERR.puts(message)
  end
ensure
  if (@step % @cycle).zero?
    truncate(@cap) rescue nil
  end
  @step = (@step + 1) % @cycle
end

#closeObject



246
247
248
# File 'lib/ledis.rb', line 246

def close
  redis.quit rescue nil
end

#configure(config = {}, &block) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
# File 'lib/ledis.rb', line 202

def configure(config = {}, &block)
  @config = Map.for(config)

  block.call(@config) if block

  @redis = @config[:redis] || @redis
  @cap   = @config[:cap]   || (2 ** 16)
  @step  = @config[:step]  || 0
  @cycle = @config[:cycle] || (2 ** 8)
  @list  = @config[:list]  || 'ledis:log'
end

#redisObject



214
215
216
# File 'lib/ledis.rb', line 214

def redis
  @redis ||= Redis.new
end

#redis=(redis) ⇒ Object



218
219
220
# File 'lib/ledis.rb', line 218

def redis=(redis)
  @redis = redis
end

#sizeObject



258
259
260
# File 'lib/ledis.rb', line 258

def size
  redis.llen(list)
end

#tail(n = 1024) ⇒ Object



250
251
252
# File 'lib/ledis.rb', line 250

def tail(n = 1024)
  redis.lrange(list, 0, n - 1).reverse
end

#truncate(size) ⇒ Object



254
255
256
# File 'lib/ledis.rb', line 254

def truncate(size)
  redis.ltrim(list, 0, size - 1)
end

#write(message, &block) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/ledis.rb', line 222

def write(message)
  begin
    redis.lpush(list, message)
  rescue Object => e
    error = "#{ e.message } (#{ e.class })\n#{ Array(e.backtrace).join(10.chr) }"
    STDERR.puts(error)
    STDERR.puts(message)
  end
ensure
  if (@step % @cycle).zero?
    truncate(@cap) rescue nil
  end
  @step = (@step + 1) % @cycle
end