services 1.1.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/lib/services/logger/redis.rb +10 -6
- data/lib/services/version.rb +1 -1
- data/spec/services/logger/redis_spec.rb +67 -33
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5badc829e3bbaa1e7dfde3b0578580a95c590cdd
|
4
|
+
data.tar.gz: 245f341ba15aa65aebb1707fdf7301727ac45d0d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7ac343f2d578b4a90424d65dea68526d73e7383466f91c5be9160045e06791796537f80c584e5edeb30b045b4b07960f4a3e0b898853e24399cff134abdb8848
|
7
|
+
data.tar.gz: 32759e41c136880fe9aa566fca32035f9b5815a1e31c4bae45a5df556d74a172d1d8ed2223c2b450d1e779ad8010c687be6cd42fa62bde00565055894681d10e
|
data/CHANGELOG.md
CHANGED
@@ -20,18 +20,22 @@ module Services
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def fetch
|
23
|
-
@redis.lrange(@key, 0, -1).map
|
24
|
-
JSON.load json
|
25
|
-
end
|
23
|
+
@redis.lrange(@key, 0, -1).map(&method(:log_entry_from_json))
|
26
24
|
end
|
27
25
|
|
28
26
|
def clear
|
29
27
|
@redis.multi do
|
30
28
|
@redis.lrange @key, 0, -1
|
31
29
|
@redis.del @key
|
32
|
-
end.first.map
|
33
|
-
|
34
|
-
|
30
|
+
end.first.map(&method(:log_entry_from_json))
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def log_entry_from_json(json)
|
36
|
+
data = JSON.load(json)
|
37
|
+
data['time'] = Time.at(data['time'])
|
38
|
+
data
|
35
39
|
end
|
36
40
|
end
|
37
41
|
end
|
data/lib/services/version.rb
CHANGED
@@ -1,69 +1,103 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Services::Logger::Redis do
|
4
|
-
let(:
|
5
|
-
let(:
|
6
|
-
let(:
|
7
|
-
let(:
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
4
|
+
let(:key) { 'custom_log_key' }
|
5
|
+
let(:redis) { Redis.new }
|
6
|
+
let(:logger) { described_class.new(redis, key) }
|
7
|
+
let(:logs) {
|
8
|
+
[
|
9
|
+
{
|
10
|
+
time: Date.new(2014, 9, 15),
|
11
|
+
message: "One day baby we'll be old",
|
12
|
+
severity: :info,
|
13
|
+
meta: {
|
14
|
+
foo: 'bar',
|
15
|
+
class: Services::Base,
|
16
|
+
object: redis
|
17
|
+
}
|
18
|
+
}, {
|
19
|
+
time: Date.new(2014, 10, 10),
|
20
|
+
message: "Oh baby, we'll be old",
|
21
|
+
severity: :warning,
|
22
|
+
meta: {}
|
23
|
+
}, {
|
24
|
+
time: Date.new(2014, 11, 17),
|
25
|
+
message: 'And think of all the stories',
|
26
|
+
severity: :critical,
|
27
|
+
meta: {
|
28
|
+
one: 2,
|
29
|
+
three: 3.14
|
30
|
+
}
|
31
|
+
}, {
|
32
|
+
time: Date.new(2014, 11, 17),
|
33
|
+
message: 'That we could have told',
|
34
|
+
severity: :debug
|
35
|
+
}
|
36
|
+
]
|
37
|
+
}
|
38
|
+
let(:fetched_logs) {
|
39
|
+
logs.reverse.map do |log|
|
40
|
+
log[:time] = log[:time].to_time
|
41
|
+
%i(message severity).each do |k|
|
42
|
+
log[k] = log[k].try(:to_s) || ''
|
43
|
+
end
|
44
|
+
log[:meta] = if log.has_key?(:meta)
|
45
|
+
log[:meta].map { |k, v| [k.to_s, v.to_s] }.to_h
|
46
|
+
else
|
47
|
+
{}
|
48
|
+
end
|
49
|
+
log.stringify_keys
|
14
50
|
end
|
15
|
-
|
51
|
+
}
|
16
52
|
|
17
53
|
def create_logs
|
18
|
-
|
19
|
-
time
|
20
|
-
|
21
|
-
|
54
|
+
logs.each do |log|
|
55
|
+
Timecop.freeze log[:time] do
|
56
|
+
args = [log[:message]]
|
57
|
+
args.push log[:meta] || {}
|
58
|
+
args.push log[:severity] if log.has_key?(:severity)
|
59
|
+
logger.log *args
|
22
60
|
end
|
23
61
|
end
|
24
62
|
end
|
25
63
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
'message' => message,
|
32
|
-
'severity' => severity,
|
33
|
-
'meta' => meta.map { |k, v| [k.to_s, v.to_s] }.to_h
|
34
|
-
}
|
35
|
-
expect do
|
36
|
-
logger.log message, meta, severity
|
37
|
-
end.to change { log_entries }.from([]).to([payload])
|
38
|
-
end
|
64
|
+
def logs_in_db
|
65
|
+
redis.lrange(key, 0, -1).map do |json|
|
66
|
+
data = JSON.load(json)
|
67
|
+
data['time'] = Time.at(data['time'])
|
68
|
+
data
|
39
69
|
end
|
40
70
|
end
|
41
71
|
|
72
|
+
before do
|
73
|
+
redis.del key
|
74
|
+
end
|
75
|
+
|
42
76
|
context 'when logs are present' do
|
43
77
|
before do
|
44
78
|
create_logs
|
45
|
-
expect(
|
79
|
+
expect(logs_in_db.size).to eq(logs.size)
|
46
80
|
end
|
47
81
|
|
48
82
|
describe '#size' do
|
49
83
|
it 'returns the amount of logs' do
|
50
|
-
expect(logger.size).to eq(
|
84
|
+
expect(logger.size).to eq(logs.size)
|
51
85
|
end
|
52
86
|
end
|
53
87
|
|
54
88
|
describe '#fetch' do
|
55
89
|
it 'returns all logs' do
|
56
|
-
expect(logger.fetch).to eq(
|
90
|
+
expect(logger.fetch).to eq(fetched_logs)
|
57
91
|
end
|
58
92
|
end
|
59
93
|
|
60
94
|
describe '#clear' do
|
61
95
|
it 'returns all logs' do
|
62
|
-
expect(
|
96
|
+
expect(fetched_logs).to eq(logger.clear)
|
63
97
|
end
|
64
98
|
|
65
99
|
it 'clears all log entries' do
|
66
|
-
expect { logger.clear }.to change {
|
100
|
+
expect { logger.clear }.to change { logs_in_db }.to([])
|
67
101
|
end
|
68
102
|
end
|
69
103
|
end
|