Class: Ledis::Logger::LogDevice
- Inherits:
-
Object
- Object
- Ledis::Logger::LogDevice
- Defined in:
- lib/ledis.rb
Instance Attribute Summary collapse
-
#cap ⇒ Object
Returns the value of attribute cap.
-
#config ⇒ Object
Returns the value of attribute config.
-
#cycle ⇒ Object
Returns the value of attribute cycle.
-
#list ⇒ Object
Returns the value of attribute list.
-
#step ⇒ Object
Returns the value of attribute step.
Instance Method Summary collapse
- #__write__ ⇒ Object
- #close ⇒ Object
- #configure(config = {}, &block) ⇒ Object
-
#initialize(*args, &block) ⇒ LogDevice
constructor
A new instance of LogDevice.
- #redis ⇒ Object
- #redis=(redis) ⇒ Object
- #size ⇒ Object
- #tail(n = 1024) ⇒ Object
- #truncate(size) ⇒ Object
- #write(message, &block) ⇒ Object
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.(args) config[:redis] ||= args.shift configure(config, &block) end |
Instance Attribute Details
#cap ⇒ Object
Returns the value of attribute cap.
191 192 193 |
# File 'lib/ledis.rb', line 191 def cap @cap end |
#config ⇒ Object
Returns the value of attribute config.
190 191 192 |
# File 'lib/ledis.rb', line 190 def config @config end |
#cycle ⇒ Object
Returns the value of attribute cycle.
193 194 195 |
# File 'lib/ledis.rb', line 193 def cycle @cycle end |
#list ⇒ Object
Returns the value of attribute list.
194 195 196 |
# File 'lib/ledis.rb', line 194 def list @list end |
#step ⇒ Object
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() begin redis.lpush(list, ) rescue Object => e error = "#{ e. } (#{ e.class })\n#{ Array(e.backtrace).join(10.chr) }" STDERR.puts(error) STDERR.puts() end ensure if (@step % @cycle).zero? truncate(@cap) rescue nil end @step = (@step + 1) % @cycle end |
#close ⇒ Object
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 |
#redis ⇒ Object
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 |
#size ⇒ Object
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() begin redis.lpush(list, ) rescue Object => e error = "#{ e. } (#{ e.class })\n#{ Array(e.backtrace).join(10.chr) }" STDERR.puts(error) STDERR.puts() end ensure if (@step % @cycle).zero? truncate(@cap) rescue nil end @step = (@step + 1) % @cycle end |