rf_logger 0.0.4 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -13
- data/.rspec +1 -1
- data/.travis.yml +3 -0
- data/README.md +99 -2
- data/lib/rf_logger/active_record/logger.rb +35 -0
- data/lib/rf_logger/active_record.rb +1 -0
- data/lib/rf_logger/configuration.rb +1 -1
- data/lib/rf_logger/notifications/error_notification_environment_constraints.rb +2 -1
- data/lib/rf_logger/rails/initialize_request_middleware.rb +9 -0
- data/lib/rf_logger/rails/key_value_tagged_logging.rb +58 -0
- data/lib/rf_logger/rails/rails_compatibility.rb +33 -0
- data/lib/rf_logger/rails.rb +5 -0
- data/lib/rf_logger/request/request_headers.rb +34 -0
- data/lib/rf_logger/request/request_middleware.rb +30 -0
- data/lib/rf_logger/request/request_tags.rb +9 -0
- data/lib/rf_logger/rory/initialize_request_middleware.rb +7 -0
- data/lib/rf_logger/rory.rb +2 -0
- data/lib/rf_logger/sequel/base.rb +46 -0
- data/lib/rf_logger/sequel/logger.rb +53 -0
- data/lib/rf_logger/sequel.rb +2 -0
- data/lib/rf_logger/version.rb +1 -1
- data/lib/rf_logger.rb +13 -42
- data/rf_logger.gemspec +5 -5
- data/spec/lib/rf_logger/active_record/logger_spec.rb +49 -0
- data/spec/lib/rf_logger/configuration_spec.rb +19 -14
- data/spec/lib/rf_logger/log_for_notification_spec.rb +3 -3
- data/spec/lib/rf_logger/notifications/error_notification_environment_constraints_spec.rb +26 -12
- data/spec/lib/rf_logger/notifications/error_notification_spec.rb +9 -9
- data/spec/lib/rf_logger/rails/rails_compatibility_spec.rb +42 -0
- data/spec/lib/rf_logger/request/request_headers_spec.rb +83 -0
- data/spec/lib/rf_logger/request/request_middleware_spec.rb +35 -0
- data/spec/lib/rf_logger/{sequel_logger_spec.rb → sequel/logger_spec.rb} +46 -16
- data/spec/lib/rf_logger/simple_logger_spec.rb +8 -8
- data/spec/lib/rf_logger_spec.rb +8 -6
- data/spec/spec_helper.rb +2 -1
- data/spec/support/request_id_shared_examples.rb +21 -0
- metadata +63 -37
- data/lib/rf_logger/sequel_logger.rb +0 -41
@@ -1,4 +1,8 @@
|
|
1
|
-
|
1
|
+
require "rf_logger/notifications/error_notification"
|
2
|
+
require "rf_logger/configuration"
|
3
|
+
require "rf_logger/levels"
|
4
|
+
|
5
|
+
RSpec.describe RfLogger::Configuration do
|
2
6
|
class SomeNotifier; end
|
3
7
|
let(:configuration) {described_class.new}
|
4
8
|
before :each do
|
@@ -10,6 +14,7 @@ describe RfLogger::Configuration do
|
|
10
14
|
Object.send(:remove_const, :Rory) if defined?(Rory)
|
11
15
|
Object.send(:remove_const, :Padrino) if defined?(Padrino)
|
12
16
|
Object.send(:remove_const, :Sinatra) if defined?(Sinatra::Application)
|
17
|
+
configuration.clear!
|
13
18
|
end
|
14
19
|
|
15
20
|
describe "#environment" do
|
@@ -19,7 +24,7 @@ describe RfLogger::Configuration do
|
|
19
24
|
|
20
25
|
it 'returns environment set by user' do
|
21
26
|
configuration.environment = 'test'
|
22
|
-
configuration.environment.
|
27
|
+
expect(configuration.environment).to eq('test')
|
23
28
|
end
|
24
29
|
|
25
30
|
it 'uses Rails.env if Rails is defined' do
|
@@ -29,7 +34,7 @@ describe RfLogger::Configuration do
|
|
29
34
|
end
|
30
35
|
end
|
31
36
|
|
32
|
-
configuration.environment.
|
37
|
+
expect(configuration.environment).to eq('monkey')
|
33
38
|
end
|
34
39
|
|
35
40
|
it 'uses Padrino.environment if defined' do
|
@@ -39,7 +44,7 @@ describe RfLogger::Configuration do
|
|
39
44
|
end
|
40
45
|
end
|
41
46
|
|
42
|
-
configuration.environment.
|
47
|
+
expect(configuration.environment).to eq('padrino')
|
43
48
|
end
|
44
49
|
|
45
50
|
it 'uses Sinatra::Application.environment if defined' do
|
@@ -51,7 +56,7 @@ describe RfLogger::Configuration do
|
|
51
56
|
end
|
52
57
|
end
|
53
58
|
|
54
|
-
configuration.environment.
|
59
|
+
expect(configuration.environment).to eq('sinatra')
|
55
60
|
end
|
56
61
|
|
57
62
|
it 'uses ENV["RORY_STAGE"] if Rory is defined' do
|
@@ -59,7 +64,7 @@ describe RfLogger::Configuration do
|
|
59
64
|
end
|
60
65
|
ENV['RORY_STAGE'] = 'rory'
|
61
66
|
|
62
|
-
configuration.environment.
|
67
|
+
expect(configuration.environment).to eq('rory')
|
63
68
|
ENV['RORY_STAGE'] = nil
|
64
69
|
end
|
65
70
|
|
@@ -84,22 +89,22 @@ describe RfLogger::Configuration do
|
|
84
89
|
it 'resets notification subject' do
|
85
90
|
configuration.notification_subject = 'foo'
|
86
91
|
configuration.clear!
|
87
|
-
configuration.notification_subject.
|
92
|
+
expect(configuration.notification_subject).to be_nil
|
88
93
|
end
|
89
94
|
|
90
95
|
it 'resets environmental error notifier settings' do
|
91
|
-
RfLogger.configuration.
|
96
|
+
allow(RfLogger.configuration).to receive_messages(:environment => 'test')
|
92
97
|
configuration.set_notifier_list { |n| n.add_notifier SomeNotifier }
|
93
98
|
configuration.clear!
|
94
|
-
configuration.notifiers.keys.
|
95
|
-
configuration.notifiers.values.uniq.
|
99
|
+
expect(configuration.notifiers.keys).to match_array(RfLogger::LEVELS)
|
100
|
+
expect(configuration.notifiers.values.uniq).to eq([[]])
|
96
101
|
end
|
97
102
|
end
|
98
103
|
|
99
104
|
describe 'notification_subject' do
|
100
105
|
it 'sets the value' do
|
101
106
|
configuration.notification_subject = 'Foo!'
|
102
|
-
configuration.notification_subject.
|
107
|
+
expect(configuration.notification_subject).to eq('Foo!')
|
103
108
|
end
|
104
109
|
end
|
105
110
|
|
@@ -107,7 +112,7 @@ describe RfLogger::Configuration do
|
|
107
112
|
class SomeOtherNotifier; end
|
108
113
|
class AThirdNotifier; end
|
109
114
|
it 'calls add_notifier on ErrorNotification and adds the notifier to config list' do
|
110
|
-
RfLogger.configuration.
|
115
|
+
allow(RfLogger.configuration).to receive_messages(:environment => 'test')
|
111
116
|
|
112
117
|
configuration.set_notifier_list do |n|
|
113
118
|
n.add_notifier SomeNotifier, :only => ['test'], :levels => [:fatal]
|
@@ -115,9 +120,9 @@ describe RfLogger::Configuration do
|
|
115
120
|
n.add_notifier AThirdNotifier, :levels => [:fatal]
|
116
121
|
end
|
117
122
|
|
118
|
-
configuration.notifiers.delete(:fatal).
|
123
|
+
expect(configuration.notifiers.delete(:fatal)).to match_array([SomeNotifier, AThirdNotifier])
|
119
124
|
# all other levels shouldn't have notifiers
|
120
|
-
configuration.notifiers.values.uniq.
|
125
|
+
expect(configuration.notifiers.values.uniq).to eq([[]])
|
121
126
|
end
|
122
127
|
end
|
123
128
|
end
|
@@ -12,18 +12,18 @@ describe RfLogger::LogForNotification do
|
|
12
12
|
|
13
13
|
describe 'subject' do
|
14
14
|
it 'reports log level, actor, and action of the error' do
|
15
|
-
subject.subject.
|
15
|
+
expect(subject.subject).to match(/ERROR.*fruit_cocktail.*finding_maraschino_cherries/)
|
16
16
|
end
|
17
17
|
|
18
18
|
it 'uses the configured subject, if given' do
|
19
19
|
RfLogger.configuration.notification_subject = "Help! {{level}} for {{actor}}/{{action}}"
|
20
|
-
subject.subject.
|
20
|
+
expect(subject.subject).to eq("Help! ERROR for fruit_cocktail/finding_maraschino_cherries")
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
24
|
describe 'details' do
|
25
25
|
it 'should include metadata' do
|
26
|
-
subject.details.
|
26
|
+
expect(subject.details).to match(/something_is_wrong.*with_the_refrigerator/)
|
27
27
|
end
|
28
28
|
end
|
29
29
|
end
|
@@ -3,48 +3,62 @@ require 'spec_helper'
|
|
3
3
|
describe RfLogger::ErrorNotification::EnvironmentConstraints do
|
4
4
|
describe '#included?' do
|
5
5
|
it 'returns true if no constraints for :only are given' do
|
6
|
-
described_class.new('alpha', {}).included
|
6
|
+
expect(described_class.new('alpha', {}).included?).to be(true)
|
7
7
|
end
|
8
8
|
|
9
9
|
it 'returns true if the environment is included in :only' do
|
10
|
-
|
10
|
+
expect(
|
11
|
+
described_class.new('alpha', {:only => ['alpha', 'beta']}).included?
|
12
|
+
).to be(true)
|
11
13
|
end
|
12
14
|
|
13
15
|
it 'returns false if the environment is not in :only' do
|
14
|
-
|
16
|
+
expect(
|
17
|
+
described_class.new('alpha', {:only => ['beta']}).included?
|
18
|
+
).to be(false)
|
15
19
|
end
|
16
20
|
end
|
17
21
|
|
18
22
|
describe '#excluded?' do
|
19
23
|
it 'returns false if no constraints for :except are given' do
|
20
|
-
described_class.new('alpha', {}).excluded
|
24
|
+
expect(described_class.new('alpha', {}).excluded?).to be(false)
|
21
25
|
end
|
22
26
|
|
23
27
|
it 'returns true if the environment is included in :except' do
|
24
|
-
|
28
|
+
expect(
|
29
|
+
described_class.new('alpha', {:except => ['alpha', 'beta']}).excluded?
|
30
|
+
).to be(true)
|
25
31
|
end
|
26
32
|
|
27
33
|
it 'returns false if the environment is not in :except' do
|
28
|
-
|
34
|
+
expect(
|
35
|
+
described_class.new('alpha', {:except => ['beta']}).excluded?
|
36
|
+
).to be(false)
|
29
37
|
end
|
30
38
|
end
|
31
39
|
|
32
40
|
describe '#valid_notifier?' do
|
33
41
|
it 'returns true if there are no constraints' do
|
34
|
-
described_class.new('alpha', nil).valid_notifier
|
35
|
-
described_class.new('alpha', {}).valid_notifier
|
42
|
+
expect(described_class.new('alpha', nil).valid_notifier?).to be(true)
|
43
|
+
expect(described_class.new('alpha', {}).valid_notifier?).to be(true)
|
36
44
|
end
|
37
45
|
|
38
46
|
it 'returns true if the environment is included in :only and not included in :except' do
|
39
|
-
|
47
|
+
expect(
|
48
|
+
described_class.new('alpha', {:only => ['alpha'], :except => []}).valid_notifier?
|
49
|
+
).to be(true)
|
40
50
|
end
|
41
51
|
|
42
52
|
it 'returns false if the environment is included in :except' do
|
43
|
-
|
53
|
+
expect(
|
54
|
+
described_class.new('alpha', {:except => ['alpha']}).valid_notifier?
|
55
|
+
).to be(false)
|
44
56
|
end
|
45
57
|
|
46
58
|
it 'returns false if the environment is not in :only' do
|
47
|
-
|
59
|
+
expect(
|
60
|
+
described_class.new('alpha', {:only => ['beta']}).valid_notifier?
|
61
|
+
).to be(false)
|
48
62
|
end
|
49
|
-
|
63
|
+
end
|
50
64
|
end
|
@@ -17,9 +17,9 @@ describe RfLogger::ErrorNotification do
|
|
17
17
|
c.add_notifier 'swiftly clean', :levels => [:fatal]
|
18
18
|
end
|
19
19
|
|
20
|
-
described_class.notifiers.delete(:error).
|
21
|
-
described_class.notifiers.delete(:fatal).
|
22
|
-
described_class.notifiers.values.uniq.
|
20
|
+
expect(described_class.notifiers.delete(:error)).to match_array(['perfectly refined'])
|
21
|
+
expect(described_class.notifiers.delete(:fatal)).to match_array(['perfectly refined', 'swiftly clean'])
|
22
|
+
expect(described_class.notifiers.values.uniq).to eq([[]])
|
23
23
|
end
|
24
24
|
|
25
25
|
it 'allows multiple types of notification' do
|
@@ -27,10 +27,10 @@ describe RfLogger::ErrorNotification do
|
|
27
27
|
c.add_notifier 'gravy_pie'
|
28
28
|
c.add_notifier 'assiduous_hedgehog'
|
29
29
|
end
|
30
|
-
described_class.notifiers.keys.
|
30
|
+
expect(described_class.notifiers.keys).to eq(RfLogger::LEVELS)
|
31
31
|
described_class.notifiers.values.uniq.tap do |unique_notifiers|
|
32
|
-
unique_notifiers.count.
|
33
|
-
unique_notifiers[0].
|
32
|
+
expect(unique_notifiers.count).to eq(1)
|
33
|
+
expect(unique_notifiers[0]).to match_array(['assiduous_hedgehog', 'gravy_pie'])
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
@@ -43,8 +43,8 @@ describe RfLogger::ErrorNotification do
|
|
43
43
|
end
|
44
44
|
|
45
45
|
described_class.notifiers.values.uniq.tap do |unique_notifiers|
|
46
|
-
unique_notifiers.count.
|
47
|
-
unique_notifiers[0].
|
46
|
+
expect(unique_notifiers.count).to eq(1)
|
47
|
+
expect(unique_notifiers[0]).to eq(['terribly sweet'])
|
48
48
|
end
|
49
49
|
end
|
50
50
|
end
|
@@ -60,7 +60,7 @@ describe RfLogger::ErrorNotification do
|
|
60
60
|
it 'calls error_notification on all configured notifiers' do
|
61
61
|
log = double(:log, :level => :warn)
|
62
62
|
described_class.notifiers[:warn].each do |n|
|
63
|
-
n.
|
63
|
+
expect(n).to receive(:send_notification).with(log)
|
64
64
|
end
|
65
65
|
described_class.dispatch_error(log)
|
66
66
|
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require "rf_logger/rails/rails_compatibility"
|
2
|
+
|
3
|
+
RSpec.describe RfLogger::RailsCompatibility do
|
4
|
+
|
5
|
+
let(:rails_gem_version) { Gem::Version.new(rails_version) }
|
6
|
+
subject { described_class.new(rails_version: rails_gem_version) }
|
7
|
+
|
8
|
+
context "far left" do
|
9
|
+
let(:rails_version) { 2.9 }
|
10
|
+
it { expect { subject.call }.to raise_error(described_class::Incompatible) }
|
11
|
+
end
|
12
|
+
|
13
|
+
context "edge left" do
|
14
|
+
let(:rails_version) { 3.1 }
|
15
|
+
it { expect { subject.call }.to raise_error(described_class::Incompatible) }
|
16
|
+
end
|
17
|
+
|
18
|
+
context "middle edge left" do
|
19
|
+
let(:rails_version) { 3.2 }
|
20
|
+
it { expect { |b| subject.call(&b) }.to yield_control }
|
21
|
+
end
|
22
|
+
|
23
|
+
context "center edge" do
|
24
|
+
let(:rails_version) { 4.0 }
|
25
|
+
it { expect { |b| subject.call(&b) }.to yield_control }
|
26
|
+
end
|
27
|
+
|
28
|
+
context "middle edge right" do
|
29
|
+
let(:rails_version) { "5.0.10" }
|
30
|
+
it { expect { |b| subject.call(&b) }.to yield_control }
|
31
|
+
end
|
32
|
+
|
33
|
+
context "edge right" do
|
34
|
+
let(:rails_version) { 5.1 }
|
35
|
+
it { expect { subject.call }.to raise_error(described_class::Incompatible) }
|
36
|
+
end
|
37
|
+
|
38
|
+
context "far right" do
|
39
|
+
let(:rails_version) {6.0}
|
40
|
+
it { expect { subject.call }.to raise_error(described_class::Incompatible) }
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require "rf_logger/request/request_headers"
|
2
|
+
require "thread/inheritable_attributes"
|
3
|
+
|
4
|
+
RSpec.describe RfLogger::RequestHeaders do
|
5
|
+
before { Thread.current.set_inheritable_attribute(:rf_logger_request_tags, nil) }
|
6
|
+
|
7
|
+
describe "#to_hash" do
|
8
|
+
context "when no type is given" do
|
9
|
+
it "defaults to json" do
|
10
|
+
expect(described_class.new.content_type).to eq "application/json"
|
11
|
+
expect(described_class.new.to_hash).to eq({ "Content-Type" => "application/json" })
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context "when providing additional types" do
|
16
|
+
it "returns given type" do
|
17
|
+
expect(described_class.new(type: "application/xml").content_type).to eq "application/xml"
|
18
|
+
expect(described_class.new(type: "application/xml").to_hash).to eq({ "Content-Type" => "application/xml" })
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context "when api_token is not given" do
|
23
|
+
it "returns no key or value" do
|
24
|
+
expect(described_class.new.api_token).to eq nil
|
25
|
+
expect(described_class.new.to_hash).to eq({ "Content-Type" => "application/json" })
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context "when api_token is given" do
|
30
|
+
it "returns given token" do
|
31
|
+
expect(described_class.new(api_token: "1090").api_token).to eq "1090"
|
32
|
+
expect(described_class.new(api_token: "1090").to_hash).to eq({ "Content-Type" => "application/json", "Api-Token" => "1090" })
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context "when request_id is given" do
|
37
|
+
it "returns given request_id" do
|
38
|
+
expect(described_class.new(request_id: "21090").request_id).to eq "21090"
|
39
|
+
expect(described_class.new(request_id: "21090").to_hash).to eq({ "Content-Type" => "application/json", "X-Request-Id" => "21090" })
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context "when request_id is not given" do
|
44
|
+
after { Thread.current.set_inheritable_attribute(:rf_logger_request_tags, nil) }
|
45
|
+
|
46
|
+
context "when in the context of a request" do
|
47
|
+
before { Thread.current.set_inheritable_attribute(:rf_logger_request_tags, { request_id: "41087" }) }
|
48
|
+
|
49
|
+
it "returns given request_id" do
|
50
|
+
expect(described_class.new.request_id).to eq "41087"
|
51
|
+
expect(described_class.new.to_hash).to eq({ "Content-Type" => "application/json", "X-Request-Id" => "41087" })
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context "when not in the context of a request" do
|
56
|
+
it "returns given request_id" do
|
57
|
+
expect(described_class.new.request_id).to eq nil
|
58
|
+
expect(described_class.new.to_hash).to eq({ "Content-Type" => "application/json" })
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
context "given any additional symbol keys" do
|
64
|
+
it "converts them to dashes and uppercases the first letter" do
|
65
|
+
expect(described_class.new(accept: "application/json",
|
66
|
+
api_version: "2.1").to_hash).
|
67
|
+
to eq({ "Content-Type" => "application/json", "Accept" => "application/json", "Api-Version" => "2.1" })
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
context "given the key of other" do
|
72
|
+
it "leaves the key name as is" do
|
73
|
+
expect(described_class.new(other: { "API_Version" => "10" }).to_hash).
|
74
|
+
to eq({ "Content-Type" => "application/json", "API_Version" => "10" })
|
75
|
+
end
|
76
|
+
|
77
|
+
it "excludes any key values where the values is nil" do
|
78
|
+
expect(described_class.new(other: { "API_Version" => nil }).to_hash).
|
79
|
+
to eq({ "Content-Type" => "application/json" })
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require "rf_logger/request/request_middleware"
|
2
|
+
|
3
|
+
RSpec.describe RfLogger::RequestMiddleware do
|
4
|
+
subject { RfLogger::RequestMiddleware.new(*init_arguments).call(env) }
|
5
|
+
let(:env) { {} }
|
6
|
+
let(:init_arguments) { [->(_) {}] }
|
7
|
+
after { Thread.current[:rf_logger_request_id] = nil }
|
8
|
+
|
9
|
+
context "when tagged is missing" do
|
10
|
+
let(:env) { { "X-Request-Id" => "uuid-from_env" } }
|
11
|
+
it "defaults to getting the request_id" do
|
12
|
+
subject
|
13
|
+
expect(Thread.current[:inheritable_attributes][:rf_logger_request_tags]).to eq({ :request_id => "uuid-from_env" })
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
context "when tagged option is given that exists" do
|
18
|
+
let(:env) { { "X-OtherHeader" => "other_request_id" } }
|
19
|
+
let(:init_arguments) { [->(_) {}, tagged: { custom_env_key: "X-OtherHeader" }] }
|
20
|
+
|
21
|
+
it "sets current Thread variable" do
|
22
|
+
subject
|
23
|
+
expect(Thread.current[:inheritable_attributes][:rf_logger_request_tags]).to eq({ :custom_env_key => "other_request_id" })
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context "when tagged option is given that does not exists" do
|
28
|
+
let(:init_arguments) { [->(_) {}, tagged: { custom_env_key: "X-WontFindMe" }] }
|
29
|
+
|
30
|
+
it "sets current Thread variable" do
|
31
|
+
subject
|
32
|
+
expect(Thread.current[:inheritable_attributes][:rf_logger_request_tags]).to eq({:custom_env_key=>nil})
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -1,26 +1,33 @@
|
|
1
1
|
require 'sequel'
|
2
|
-
|
3
|
-
require
|
2
|
+
Sequel::Model.db = Sequel.mock
|
3
|
+
require "rf_logger/sequel/logger"
|
4
|
+
|
5
|
+
describe RfLogger::Sequel::Logger do
|
6
|
+
include_examples "RfLogger::RequestId", subject: described_class
|
7
|
+
|
8
|
+
it "keeps backwards compatibility" do
|
9
|
+
expect(described_class).to eq RfLogger::SequelLogger
|
10
|
+
end
|
4
11
|
|
5
|
-
describe RfLogger::SequelLogger do
|
6
12
|
before :each do
|
7
|
-
Time.
|
8
|
-
described_class.dataset =
|
9
|
-
|
13
|
+
allow(Time).to receive_messages(:now => 'NOW')
|
14
|
+
described_class.dataset =
|
15
|
+
Sequel::Model.db[:logs].columns(:actor, :action, :target_type, :target_id,
|
10
16
|
:metadata, :created_at, :updated_at, :level)
|
17
|
+
allow(described_class).to receive(:rf_logger_request_tags){{request_id: "909090"}}
|
11
18
|
end
|
12
19
|
|
13
20
|
RfLogger::LEVELS.each do |level|
|
14
21
|
describe ".#{level}" do
|
15
22
|
it "logs information in the database with #{level.upcase} level" do
|
16
|
-
described_class.
|
23
|
+
expect(described_class).to receive(:add).
|
17
24
|
with(level, :something => :happened)
|
18
25
|
described_class.send(level.to_sym, :something => :happened)
|
19
26
|
end
|
20
27
|
|
21
28
|
it 'dispatches error notifications' do
|
22
|
-
described_class.
|
23
|
-
RfLogger::ErrorNotification.
|
29
|
+
allow(described_class).to receive(:add)
|
30
|
+
expect(RfLogger::ErrorNotification).to receive(:dispatch_error)
|
24
31
|
described_class.send(level.to_sym, :something => :happened)
|
25
32
|
end
|
26
33
|
end
|
@@ -28,7 +35,7 @@ describe RfLogger::SequelLogger do
|
|
28
35
|
|
29
36
|
describe '.add' do
|
30
37
|
it 'adds given object to the log at given level' do
|
31
|
-
|
38
|
+
expect(described_class).to receive(:create).with(
|
32
39
|
:actor => 'cat herder',
|
33
40
|
:action => 'herd some cats',
|
34
41
|
:target_type => 'Cat',
|
@@ -38,7 +45,8 @@ describe RfLogger::SequelLogger do
|
|
38
45
|
:danger => {
|
39
46
|
:level => 'really_high',
|
40
47
|
:rodent => 'mouse',
|
41
|
-
}
|
48
|
+
},
|
49
|
+
:request_tags => { :request_id => "909090" }
|
42
50
|
},
|
43
51
|
:level => RfLogger::LEVELS.index(:info),
|
44
52
|
:created_at => 'NOW',
|
@@ -54,16 +62,31 @@ describe RfLogger::SequelLogger do
|
|
54
62
|
:danger => {
|
55
63
|
:level => 'really_high',
|
56
64
|
:rodent => 'mouse',
|
57
|
-
}
|
65
|
+
},
|
66
|
+
:request_tags => { :request_id => "909090" }
|
58
67
|
}
|
59
68
|
})
|
60
69
|
end
|
61
70
|
|
71
|
+
context "when rf_logger_request_tags is empty" do
|
72
|
+
it "return a metadata with no request_tags key" do
|
73
|
+
allow(described_class).to receive(:rf_logger_request_tags){nil}
|
74
|
+
|
75
|
+
expect(described_class).to receive(:create).with(
|
76
|
+
({ :level => 1,
|
77
|
+
:actor => "",
|
78
|
+
:metadata => { },
|
79
|
+
:created_at => "NOW" })
|
80
|
+
)
|
81
|
+
described_class.add(:info, {})
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
62
85
|
it 'sets actor to blank string if not provided' do
|
63
|
-
described_class.
|
86
|
+
expect(described_class).to receive(:create).with(
|
64
87
|
:actor => '',
|
65
88
|
:action => 'palpitate',
|
66
|
-
:metadata => {},
|
89
|
+
:metadata => {:request_tags=>{:request_id=>"909090"}},
|
67
90
|
:created_at => 'NOW',
|
68
91
|
:level => RfLogger::LEVELS.index(:info))
|
69
92
|
|
@@ -71,21 +94,28 @@ describe RfLogger::SequelLogger do
|
|
71
94
|
end
|
72
95
|
|
73
96
|
it 'sets metadata to empty hash if not provided' do
|
74
|
-
described_class.
|
97
|
+
expect(described_class).to receive(:create).with(
|
75
98
|
:actor => '',
|
76
99
|
:action => 'palpitate',
|
77
|
-
:metadata => {},
|
100
|
+
:metadata => {:request_tags=>{:request_id=>"909090"}},
|
78
101
|
:created_at => 'NOW',
|
79
102
|
:level => RfLogger::LEVELS.index(:info)
|
80
103
|
)
|
81
104
|
|
82
105
|
described_class.add(:info, { :action => 'palpitate' })
|
83
106
|
end
|
107
|
+
end
|
84
108
|
|
109
|
+
describe "#metadata" do
|
85
110
|
it 'returns a hash for metadata even though it is stored as JSON' do
|
86
111
|
subject.metadata = {'foo' => 'bar'}
|
87
112
|
expect(subject.metadata).to eq({'foo' => 'bar'})
|
88
113
|
end
|
114
|
+
|
115
|
+
it 'returns nil if column is null' do
|
116
|
+
subject.metadata = nil
|
117
|
+
expect(subject.metadata).to be_nil
|
118
|
+
end
|
89
119
|
end
|
90
120
|
|
91
121
|
describe 'display_level' do
|
@@ -6,7 +6,7 @@ describe RfLogger::SimpleLogger do
|
|
6
6
|
RfLogger::LEVELS.each do |level|
|
7
7
|
describe ".#{level}" do
|
8
8
|
it "adds given object to the log with '#{level}' level" do
|
9
|
-
described_class.
|
9
|
+
expect(described_class).to receive(:add).
|
10
10
|
with(level, :something => :happened)
|
11
11
|
described_class.send(level.to_sym, :something => :happened)
|
12
12
|
end
|
@@ -17,10 +17,10 @@ describe RfLogger::SimpleLogger do
|
|
17
17
|
it 'adds given object to the log at given level' do
|
18
18
|
described_class.add(:info, :super_serious_occurrence)
|
19
19
|
described_class.add(:debug, :weird_thing)
|
20
|
-
described_class.entries.
|
20
|
+
expect(described_class.entries).to eq([
|
21
21
|
{ :level => 1, :level_name => :info, :entry => :super_serious_occurrence },
|
22
22
|
{ :level => 0, :level_name => :debug, :entry => :weird_thing }
|
23
|
-
]
|
23
|
+
])
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
@@ -30,22 +30,22 @@ describe RfLogger::SimpleLogger do
|
|
30
30
|
described_class.debug 'other thing'
|
31
31
|
described_class.info 'third thing'
|
32
32
|
described_class.fatal 'final thing'
|
33
|
-
described_class.entries.
|
33
|
+
expect(described_class.entries).to eq([
|
34
34
|
{ :level => 1, :level_name => :info, :entry => 'thing' },
|
35
35
|
{ :level => 0, :level_name => :debug, :entry => 'other thing' },
|
36
36
|
{ :level => 1, :level_name => :info, :entry => 'third thing' },
|
37
37
|
{ :level => 4, :level_name => :fatal, :entry => 'final thing' }
|
38
|
-
]
|
38
|
+
])
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
42
|
describe '.clear!' do
|
43
43
|
it 'deletes all entries' do
|
44
|
-
described_class.entries.
|
44
|
+
expect(described_class.entries).to be_empty
|
45
45
|
described_class.info 'thing'
|
46
|
-
described_class.entries.
|
46
|
+
expect(described_class.entries).not_to be_empty
|
47
47
|
described_class.clear!
|
48
|
-
described_class.entries.
|
48
|
+
expect(described_class.entries).to be_empty
|
49
49
|
end
|
50
50
|
end
|
51
51
|
end
|
data/spec/lib/rf_logger_spec.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require "rf_logger/sequel/base"
|
2
|
+
|
1
3
|
describe RfLogger do
|
2
4
|
describe ".configure" do
|
3
5
|
it 'yields the current configuration' do
|
@@ -16,8 +18,8 @@ describe RfLogger do
|
|
16
18
|
c.environment = 'production'
|
17
19
|
end
|
18
20
|
|
19
|
-
described_class.configuration.notification_subject.
|
20
|
-
described_class.configuration.environment.
|
21
|
+
expect(described_class.configuration.notification_subject).to eq('Foo')
|
22
|
+
expect(described_class.configuration.environment).to eq('production')
|
21
23
|
end
|
22
24
|
|
23
25
|
it 'requires a block' do
|
@@ -45,25 +47,25 @@ describe RfLogger do
|
|
45
47
|
it 'resets configuration' do
|
46
48
|
old_config = described_class.configuration
|
47
49
|
described_class.clear_configuration!
|
48
|
-
described_class.configuration.
|
50
|
+
expect(described_class.configuration).not_to eq(old_config)
|
49
51
|
end
|
50
52
|
end
|
51
53
|
|
52
54
|
describe '.environment' do
|
53
55
|
it 'can set directly' do
|
54
56
|
described_class.environment = 'foo'
|
55
|
-
described_class.configuration.environment.
|
57
|
+
expect(described_class.configuration.environment).to eq('foo')
|
56
58
|
end
|
57
59
|
end
|
58
60
|
|
59
61
|
describe '.configuration' do
|
60
62
|
it 'creates an instance of RfLogger::Configuration' do
|
61
|
-
described_class.configuration.
|
63
|
+
expect(described_class.configuration).to be_an_instance_of(RfLogger::Configuration)
|
62
64
|
end
|
63
65
|
|
64
66
|
it 'returns the same instance when called multiple times' do
|
65
67
|
configuration = described_class.configuration
|
66
|
-
described_class.configuration.
|
68
|
+
expect(described_class.configuration).to eq(configuration)
|
67
69
|
end
|
68
70
|
end
|
69
71
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
require 'simplecov'
|
2
2
|
SimpleCov.start
|
3
|
+
lib = File.expand_path('../../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
5
|
|
4
6
|
# Requires supporting ruby files with custom matchers and macros, etc,
|
5
7
|
# in spec/support/ and its subdirectories.
|
@@ -8,7 +10,6 @@ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
|
8
10
|
require './lib/rf_logger'
|
9
11
|
|
10
12
|
RSpec.configure do |config|
|
11
|
-
config.treat_symbols_as_metadata_keys_with_true_values = true
|
12
13
|
config.run_all_when_everything_filtered = true
|
13
14
|
config.order = 'random'
|
14
15
|
end
|