redislog 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/bin/redislog +67 -0
- data/lib/redislog.rb +75 -0
- metadata +69 -0
data/bin/redislog
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
require 'redislog'
|
|
3
|
+
require 'choice'
|
|
4
|
+
|
|
5
|
+
PROGRAM_VERSION = "1.0.0"
|
|
6
|
+
Choice.options do
|
|
7
|
+
header ''
|
|
8
|
+
separator "Mandatory Arguments:"
|
|
9
|
+
|
|
10
|
+
option :path, :required => true do
|
|
11
|
+
short "-d"
|
|
12
|
+
long "--directory=DIRECTORY"
|
|
13
|
+
desc "Path of the log files"
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
separator "Optional Arguments:"
|
|
17
|
+
|
|
18
|
+
option :host do
|
|
19
|
+
short "-h"
|
|
20
|
+
long "--host=HOST"
|
|
21
|
+
desc "Redis server ip"
|
|
22
|
+
default "localhost"
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
option :port do
|
|
26
|
+
short "-p"
|
|
27
|
+
long "--port=PORT"
|
|
28
|
+
desc "Redis server port"
|
|
29
|
+
default "6379"
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
option :db do
|
|
33
|
+
short "-D"
|
|
34
|
+
long "--DB=DB"
|
|
35
|
+
desc "Redis database"
|
|
36
|
+
default "0"
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
option :filename do
|
|
40
|
+
short "-f"
|
|
41
|
+
long "--filename=FILENAME"
|
|
42
|
+
desc "Pattern of the logfile name"
|
|
43
|
+
default "Redislog"
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
separator 'Common Arguments:'
|
|
47
|
+
option :help do
|
|
48
|
+
short '-h'
|
|
49
|
+
long '--help'
|
|
50
|
+
desc 'Show this message.'
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
option :version do
|
|
54
|
+
short '-v'
|
|
55
|
+
long '--version'
|
|
56
|
+
desc 'Show version'
|
|
57
|
+
action do
|
|
58
|
+
puts PROGRAM_VERSION
|
|
59
|
+
exit
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
logger = Redislog.new(:host => Choice.choices[:host],
|
|
65
|
+
:port => Choice.choices[:port].to_i,
|
|
66
|
+
:db => Choice.choices[:db])
|
|
67
|
+
logger.write_log(Choice.choices[:path], Choice.choices[:filename])
|
data/lib/redislog.rb
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
require 'redis'
|
|
2
|
+
|
|
3
|
+
class Redislog
|
|
4
|
+
|
|
5
|
+
@@levels = {
|
|
6
|
+
:debug => 0,
|
|
7
|
+
:info => 1,
|
|
8
|
+
:error => 2,
|
|
9
|
+
:critical => 3
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
attr_accessor :logger_level, :prefix
|
|
13
|
+
|
|
14
|
+
def initialize(params = {})
|
|
15
|
+
@logger_level = params[:logger_level] || :debug
|
|
16
|
+
host = params[:host] || 'localhost'
|
|
17
|
+
port = params[:port] || 6379
|
|
18
|
+
db = params[:db] || 0
|
|
19
|
+
@prefix = params[:prefix] || '0'
|
|
20
|
+
@client = Redis.new(:host => host, :port => port, :db => db)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def debug(msg)
|
|
24
|
+
log(msg, :debug)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def info(msg)
|
|
28
|
+
log(msg, :info)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def error(msg)
|
|
32
|
+
log(msg, :error)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def critical(msg)
|
|
36
|
+
log(msg, :critical)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def log(msg, level = :debug)
|
|
40
|
+
if @@levels[level] >= @@levels[@logger_level]
|
|
41
|
+
current_time = Time.now
|
|
42
|
+
key = if @prefix.nil? then current_time else "#{@prefix}:#{current_time.to_f}" end
|
|
43
|
+
log_msg = "#{@prefix} | #{current_time.to_s} | #{level} | #{msg}"
|
|
44
|
+
@client.set(key, log_msg)
|
|
45
|
+
@client.multi do
|
|
46
|
+
score = @client.zcard("logger_keys") || 0
|
|
47
|
+
@client.zadd("logger_keys", score+1, key)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def write_log(path=".", file_prefix = "Redislog")
|
|
54
|
+
count = @client.zcard("logger_keys")
|
|
55
|
+
keys = @client.zrange("logger_keys", 0, count)
|
|
56
|
+
|
|
57
|
+
#Export from redis to log file
|
|
58
|
+
if keys.length > 0
|
|
59
|
+
file = File.new("#{path}/#{file_prefix}_#{Time.now.to_i}.log", "w")
|
|
60
|
+
|
|
61
|
+
values = @client.mget(*keys)
|
|
62
|
+
(0 .. keys.length).each do |counter|
|
|
63
|
+
file.syswrite(values[counter].to_s)
|
|
64
|
+
file.syswrite("\n")
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
file.close
|
|
68
|
+
|
|
69
|
+
#delete the entries of logger_keys sorted set and also
|
|
70
|
+
#delete log messages
|
|
71
|
+
@client.zremrangebyrank("logger_keys", 0, count)
|
|
72
|
+
@client.del(*keys)
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: redislog
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Aravind
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2012-01-14 00:00:00.000000000Z
|
|
13
|
+
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
|
+
name: redis
|
|
16
|
+
requirement: &81034940 !ruby/object:Gem::Requirement
|
|
17
|
+
none: false
|
|
18
|
+
requirements:
|
|
19
|
+
- - ! '>='
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '0'
|
|
22
|
+
type: :runtime
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: *81034940
|
|
25
|
+
- !ruby/object:Gem::Dependency
|
|
26
|
+
name: choice
|
|
27
|
+
requirement: &81034720 !ruby/object:Gem::Requirement
|
|
28
|
+
none: false
|
|
29
|
+
requirements:
|
|
30
|
+
- - ! '>='
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '0'
|
|
33
|
+
type: :runtime
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: *81034720
|
|
36
|
+
description: Redislog is an application logger with redis as backend instead of file.
|
|
37
|
+
email: aravindkumar.leo@gmail.com
|
|
38
|
+
executables:
|
|
39
|
+
- redislog
|
|
40
|
+
extensions: []
|
|
41
|
+
extra_rdoc_files: []
|
|
42
|
+
files:
|
|
43
|
+
- lib/redislog.rb
|
|
44
|
+
- bin/redislog
|
|
45
|
+
homepage: https://github.com/aravindj/Redislog
|
|
46
|
+
licenses: []
|
|
47
|
+
post_install_message:
|
|
48
|
+
rdoc_options: []
|
|
49
|
+
require_paths:
|
|
50
|
+
- lib
|
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
52
|
+
none: false
|
|
53
|
+
requirements:
|
|
54
|
+
- - ! '>='
|
|
55
|
+
- !ruby/object:Gem::Version
|
|
56
|
+
version: '0'
|
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
58
|
+
none: false
|
|
59
|
+
requirements:
|
|
60
|
+
- - ! '>='
|
|
61
|
+
- !ruby/object:Gem::Version
|
|
62
|
+
version: '0'
|
|
63
|
+
requirements: []
|
|
64
|
+
rubyforge_project:
|
|
65
|
+
rubygems_version: 1.8.15
|
|
66
|
+
signing_key:
|
|
67
|
+
specification_version: 3
|
|
68
|
+
summary: Redis based application logger.
|
|
69
|
+
test_files: []
|