Class: Redislog
- Inherits:
-
Object
- Object
- Redislog
- Defined in:
- lib/redislog.rb
Constant Summary collapse
- @@levels =
{ :debug => 0, :info => 1, :error => 2, :critical => 3 }
Instance Attribute Summary collapse
-
#logger_level ⇒ Object
Returns the value of attribute logger_level.
-
#prefix ⇒ Object
Returns the value of attribute prefix.
Instance Method Summary collapse
- #critical(msg) ⇒ Object
- #debug(msg) ⇒ Object
- #error(msg) ⇒ Object
- #info(msg) ⇒ Object
-
#initialize(params = {}) ⇒ Redislog
constructor
A new instance of Redislog.
- #log(msg, level = :debug) ⇒ Object
- #write_log(path = ".", file_prefix = "Redislog") ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ Redislog
Returns a new instance of Redislog.
14 15 16 17 18 19 20 21 |
# File 'lib/redislog.rb', line 14 def initialize(params = {}) @logger_level = params[:logger_level] || :debug host = params[:host] || 'localhost' port = params[:port] || 6379 db = params[:db] || 0 @prefix = params[:prefix] || '0' @client = Redis.new(:host => host, :port => port, :db => db) end |
Instance Attribute Details
#logger_level ⇒ Object
Returns the value of attribute logger_level.
12 13 14 |
# File 'lib/redislog.rb', line 12 def logger_level @logger_level end |
#prefix ⇒ Object
Returns the value of attribute prefix.
12 13 14 |
# File 'lib/redislog.rb', line 12 def prefix @prefix end |
Instance Method Details
#critical(msg) ⇒ Object
35 36 37 |
# File 'lib/redislog.rb', line 35 def critical(msg) log(msg, :critical) end |
#debug(msg) ⇒ Object
23 24 25 |
# File 'lib/redislog.rb', line 23 def debug(msg) log(msg, :debug) end |
#error(msg) ⇒ Object
31 32 33 |
# File 'lib/redislog.rb', line 31 def error(msg) log(msg, :error) end |
#info(msg) ⇒ Object
27 28 29 |
# File 'lib/redislog.rb', line 27 def info(msg) log(msg, :info) end |
#log(msg, level = :debug) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/redislog.rb', line 39 def log(msg, level = :debug) if @@levels[level] >= @@levels[@logger_level] current_time = Time.now key = if @prefix.nil? then current_time else "#{@prefix}:#{current_time.to_f}" end log_msg = "#{@prefix} | #{current_time.to_s} | #{level} | #{msg}" @client.set(key, log_msg) @client.multi do score = @client.zcard("logger_keys") || 0 @client.zadd("logger_keys", score+1, key) end end end |
#write_log(path = ".", file_prefix = "Redislog") ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/redislog.rb', line 53 def write_log(path=".", file_prefix = "Redislog") count = @client.zcard("logger_keys") keys = @client.zrange("logger_keys", 0, count) #Export from redis to log file if keys.length > 0 file = File.new("#{path}/#{file_prefix}_#{Time.now.to_i}.log", "w") values = @client.mget(*keys) (0 .. keys.length).each do |counter| file.syswrite(values[counter].to_s) file.syswrite("\n") end file.close #delete the entries of logger_keys sorted set and also #delete log messages @client.zremrangebyrank("logger_keys", 0, count) @client.del(*keys) end end |