transaction_logger 1.0.2 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +16 -3
- data/lib/transaction_logger/version.rb +1 -1
- data/lib/transaction_logger.rb +14 -3
- data/spec/transaction_logger/transaction_spec.rb +54 -0
- data/spec/transaction_logger_configure_spec.rb +43 -0
- data/spec/transaction_logger_spec.rb +66 -71
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2e0b34abcfe7bb0e2aab8a11e94fa283e3cd4c8a
|
4
|
+
data.tar.gz: 2b5525a2f7b0d65d0acaf2deb86765075509483e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 946306e6960761db73217526eba5262b97817fb4a902f07e7d475730f8ef9f813367a1435ac9b09f3434654a0cae3f10e511871c8c446442d6f70174cc427e66
|
7
|
+
data.tar.gz: 1028997dbb986ff41f157d9676d5b086005f45723b9fa488fd15f53a29225bcb80509d8ee3380bc3c23793fc25d851bbf86102bed6edb717b90b2c668c3d0ad4
|
data/README.md
CHANGED
@@ -86,10 +86,19 @@ class YourClass
|
|
86
86
|
end
|
87
87
|
```
|
88
88
|
|
89
|
-
By default, the transaction will be named *YourClass:some_method
|
89
|
+
By default, the transaction will be named *YourClass:some_method*. You can easily change this by adding the name to the options params:
|
90
90
|
|
91
91
|
```ruby
|
92
|
-
add_transaction_log :some_method, {name: "Custom Name"
|
92
|
+
add_transaction_log :some_method, {name: "Custom Name" }
|
93
|
+
```
|
94
|
+
|
95
|
+
You can set a *context* to the options that is pushed to the logger. It can either anything supporting `.to_hash` or a `Proc`.
|
96
|
+
The proc will be evaluated in the scope of the traced method.
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
add_transaction_log :some_method, {context: "Custom Context" }
|
100
|
+
add_transaction_log :some_method, {context: { key: "value context" } }
|
101
|
+
add_transaction_log :some_method, {context: -> { request.params } }
|
93
102
|
```
|
94
103
|
|
95
104
|
### Example
|
@@ -108,7 +117,7 @@ class ExampleClass
|
|
108
117
|
logger.info "Success"
|
109
118
|
end
|
110
119
|
|
111
|
-
add_transaction_log :some_method, {context: {some_id: 12}}
|
120
|
+
add_transaction_log :some_method, { context: { some_id: 12 } }
|
112
121
|
end
|
113
122
|
```
|
114
123
|
|
@@ -143,6 +152,10 @@ The expected output is:
|
|
143
152
|
|
144
153
|
## Version History
|
145
154
|
|
155
|
+
### v1.1.0
|
156
|
+
- Fixed issues [#32](https://github.com/blinkist/transaction_logger/issues/32) for missing context
|
157
|
+
- Added support for Proc as context [#34](https://github.com/blinkist/transaction_logger/issues/34)
|
158
|
+
|
146
159
|
### v1.0.1
|
147
160
|
- Fixed issues with undefined trap_logger method
|
148
161
|
- Hid module methods other than add_transaction_log
|
data/lib/transaction_logger.rb
CHANGED
@@ -28,17 +28,28 @@ module TransactionLogger
|
|
28
28
|
def add_transaction_log(method, options={})
|
29
29
|
old_method = instance_method method
|
30
30
|
|
31
|
-
options = {}
|
32
31
|
options[:prefix] = TransactionLogger::Configure.instance_variable_get :@prefix
|
33
32
|
options[:logger] = TransactionLogger::Configure.instance_variable_get :@logger
|
34
33
|
options[:level_threshold] = TransactionLogger::Configure.instance_variable_get :@level_threshold
|
35
34
|
|
36
35
|
define_method method do
|
36
|
+
context = options[:context]
|
37
|
+
|
38
|
+
if context.is_a? Proc
|
39
|
+
begin
|
40
|
+
context = instance_exec(&context)
|
41
|
+
rescue => e
|
42
|
+
context = "TransactionLogger: couldn't evaluate context: #{e.message}"
|
43
|
+
# Write an error to the untrapped logger
|
44
|
+
logger.error context
|
45
|
+
logger.error e.backtrace.take(10).join "\n"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
37
49
|
TransactionManager.start options, lambda { |transaction|
|
38
50
|
transaction.name = options[:name]
|
39
51
|
transaction.name ||= "#{old_method.bind(self).owner}#{method.inspect}"
|
40
|
-
transaction.context =
|
41
|
-
transaction.context ||= {}
|
52
|
+
transaction.context = context || {}
|
42
53
|
|
43
54
|
# Check for a logger on the instance
|
44
55
|
if methods.include? :logger
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "logger"
|
3
|
+
|
4
|
+
describe TransactionLogger::Transaction do
|
5
|
+
let (:test_lmbda) {
|
6
|
+
lambda do |_t|
|
7
|
+
end
|
8
|
+
}
|
9
|
+
|
10
|
+
subject {
|
11
|
+
TransactionLogger::Transaction.new(
|
12
|
+
{ prefix: nil, logger: Logger.new(STDOUT), level_threshold: nil }, test_lmbda)
|
13
|
+
}
|
14
|
+
|
15
|
+
describe "#run" do
|
16
|
+
context "when no exception is raised" do
|
17
|
+
let (:test_lmbda) {
|
18
|
+
lambda do |_t|
|
19
|
+
"result"
|
20
|
+
end
|
21
|
+
}
|
22
|
+
|
23
|
+
let (:result) { subject.run }
|
24
|
+
|
25
|
+
it "returns lmda" do
|
26
|
+
expect(result).to eq "result"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context "when an exception is raised" do
|
31
|
+
let (:test_lmbda) {
|
32
|
+
lambda do |_t|
|
33
|
+
fail "test error"
|
34
|
+
end
|
35
|
+
}
|
36
|
+
|
37
|
+
let (:child) {
|
38
|
+
TransactionLogger::Transaction.new(
|
39
|
+
{ prefix: nil, logger: Logger.new(STDOUT), level_threshold: nil }, test_lmbda)
|
40
|
+
}
|
41
|
+
|
42
|
+
let (:result) { subject.run }
|
43
|
+
|
44
|
+
it "raises an exception" do
|
45
|
+
expect { result }.to raise_error "test error"
|
46
|
+
end
|
47
|
+
|
48
|
+
it "calls failure" do
|
49
|
+
expect(subject).to receive(:failure)
|
50
|
+
result
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "logger"
|
3
|
+
|
4
|
+
describe TransactionLogger::Configure do
|
5
|
+
let (:test_lmbda) {
|
6
|
+
lambda do |_t|
|
7
|
+
end
|
8
|
+
}
|
9
|
+
|
10
|
+
describe ".log_prefix" do
|
11
|
+
subject {
|
12
|
+
TransactionLogger::Transaction.new(
|
13
|
+
{ prefix: nil, logger: Logger.new(STDOUT), level_threshold: nil }, test_lmbda)
|
14
|
+
}
|
15
|
+
|
16
|
+
context "when there is no prefix" do
|
17
|
+
it "does not change the output" do
|
18
|
+
expect(subject.to_hash).to include("name" => "undefined")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context "when a prefix is defined" do
|
23
|
+
let (:prefix) { "bta_" }
|
24
|
+
|
25
|
+
before :example do
|
26
|
+
described_class.log_prefix = prefix
|
27
|
+
end
|
28
|
+
|
29
|
+
subject {
|
30
|
+
TransactionLogger::Transaction.new(
|
31
|
+
{ prefix: described_class.log_prefix, logger: Logger.new(STDOUT), level_threshold: nil }, test_lmbda)
|
32
|
+
}
|
33
|
+
|
34
|
+
after :example do
|
35
|
+
described_class.log_prefix = ""
|
36
|
+
end
|
37
|
+
|
38
|
+
it "adds the prefix to every key" do
|
39
|
+
expect(subject.to_hash).to include("bta_name" => "undefined")
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -74,6 +74,72 @@ describe TransactionLogger do
|
|
74
74
|
end
|
75
75
|
end
|
76
76
|
|
77
|
+
context "logging options" do
|
78
|
+
let(:logger_stub) { double(Logger) }
|
79
|
+
|
80
|
+
let(:test_logger) {
|
81
|
+
Class.new do
|
82
|
+
include TransactionLogger
|
83
|
+
|
84
|
+
def do_something
|
85
|
+
raise "Error"
|
86
|
+
end
|
87
|
+
|
88
|
+
def logger
|
89
|
+
Logger.new STDOUT
|
90
|
+
end
|
91
|
+
|
92
|
+
add_transaction_log :do_something, name: "my name", context: "my context"
|
93
|
+
end
|
94
|
+
}
|
95
|
+
|
96
|
+
it "sets transaction name and context" do
|
97
|
+
TransactionLogger::Configure.logger = logger_stub
|
98
|
+
|
99
|
+
expect(logger_stub).to receive(:error) do |msg|
|
100
|
+
expect(msg["context"]).to eq "my context"
|
101
|
+
expect(msg["name"]).to eq "my name"
|
102
|
+
end
|
103
|
+
|
104
|
+
test = test_logger.new
|
105
|
+
expect { test.do_something }.to raise_error "Error"
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
context "Dynamic context" do
|
110
|
+
let(:instance_logger_lambda) {
|
111
|
+
Class.new do
|
112
|
+
include TransactionLogger
|
113
|
+
|
114
|
+
def do_something
|
115
|
+
logger.info "TEST"
|
116
|
+
end
|
117
|
+
|
118
|
+
def dynamic_context
|
119
|
+
"Dynamic Context"
|
120
|
+
end
|
121
|
+
|
122
|
+
def logger
|
123
|
+
Logger.new STDOUT
|
124
|
+
end
|
125
|
+
|
126
|
+
add_transaction_log :do_something, context: -> { dynamic_context }
|
127
|
+
end
|
128
|
+
}
|
129
|
+
|
130
|
+
it "executes the lambda bound to the logged instance" do
|
131
|
+
test = instance_logger_lambda.new
|
132
|
+
expect(test).to receive(:dynamic_context).and_call_original
|
133
|
+
test.do_something
|
134
|
+
end
|
135
|
+
|
136
|
+
it "doesn't fail the method execution if the lambda eval fails" do
|
137
|
+
test = instance_logger_lambda.new
|
138
|
+
allow(test).to receive(:dynamic_context).and_raise "Some error"
|
139
|
+
expect { test.do_something }.to_not raise_error
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
77
143
|
subject {
|
78
144
|
TransactionLogger::Transaction.new(
|
79
145
|
{ prefix: nil, logger: Logger.new(STDOUT), level_threshold: nil }, test_lmbda)
|
@@ -112,75 +178,4 @@ describe TransactionLogger do
|
|
112
178
|
end
|
113
179
|
}
|
114
180
|
end
|
115
|
-
|
116
|
-
describe TransactionLogger::Configure do
|
117
|
-
describe ".log_prefix" do
|
118
|
-
context "when there is no prefix" do
|
119
|
-
it "does not change the output" do
|
120
|
-
expect(subject.to_hash).to include("name" => "undefined")
|
121
|
-
end
|
122
|
-
end
|
123
|
-
|
124
|
-
context "when a prefix is defined" do
|
125
|
-
let (:prefix) { "bta_" }
|
126
|
-
|
127
|
-
before :example do
|
128
|
-
described_class.log_prefix = prefix
|
129
|
-
end
|
130
|
-
|
131
|
-
subject {
|
132
|
-
TransactionLogger::Transaction.new(
|
133
|
-
{ prefix: described_class.log_prefix, logger: Logger.new(STDOUT), level_threshold: nil }, test_lmbda)
|
134
|
-
}
|
135
|
-
|
136
|
-
after :example do
|
137
|
-
described_class.log_prefix = ""
|
138
|
-
end
|
139
|
-
|
140
|
-
it "adds the prefix to every key" do
|
141
|
-
expect(subject.to_hash).to include("bta_name" => "undefined")
|
142
|
-
end
|
143
|
-
end
|
144
|
-
end
|
145
|
-
end
|
146
|
-
|
147
|
-
describe "#run" do
|
148
|
-
context "when no exception is raised" do
|
149
|
-
let (:test_lmbda) {
|
150
|
-
lambda do |_t|
|
151
|
-
"result"
|
152
|
-
end
|
153
|
-
}
|
154
|
-
|
155
|
-
let (:result) { subject.run }
|
156
|
-
|
157
|
-
it "returns lmda" do
|
158
|
-
expect(result).to eq "result"
|
159
|
-
end
|
160
|
-
end
|
161
|
-
|
162
|
-
context "when an exception is raised" do
|
163
|
-
let (:test_lmbda) {
|
164
|
-
lambda do |_t|
|
165
|
-
fail "test error"
|
166
|
-
end
|
167
|
-
}
|
168
|
-
|
169
|
-
let (:child) {
|
170
|
-
TransactionLogger::Transaction.new(
|
171
|
-
{ prefix: nil, logger: Logger.new(STDOUT), level_threshold: nil }, test_lmbda)
|
172
|
-
}
|
173
|
-
|
174
|
-
let (:result) { subject.run }
|
175
|
-
|
176
|
-
it "raises an exception" do
|
177
|
-
expect { result }.to raise_error "test error"
|
178
|
-
end
|
179
|
-
|
180
|
-
it "calls failure" do
|
181
|
-
expect(subject).to receive(:failure)
|
182
|
-
result
|
183
|
-
end
|
184
|
-
end
|
185
|
-
end
|
186
181
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: transaction_logger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Donner
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-08-
|
12
|
+
date: 2015-08-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -176,7 +176,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
176
176
|
version: '0'
|
177
177
|
requirements: []
|
178
178
|
rubyforge_project:
|
179
|
-
rubygems_version: 2.4.
|
179
|
+
rubygems_version: 2.4.5
|
180
180
|
signing_key:
|
181
181
|
specification_version: 4
|
182
182
|
summary: Contextual Business Transaction Logger for Ruby
|