transaction_logger 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -13
- data/lib/transaction_logger/version.rb +1 -1
- data/lib/transaction_logger.rb +67 -56
- data/spec/transaction_logger_spec.rb +21 -19
- metadata +3 -4
- data/LICENSE.txt +0 -22
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c85e18e15f0aa46051bdd6d5d6d827ca662fce6b
|
4
|
+
data.tar.gz: 65889439258e4082571f87310c90a035bf23229d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7b02d897ef866380ce133cd11215f9c974907008aa9d072f549fb7a9652e7511e0b49e7b5326f334c58bd99831d67dc4f795181db8ac3b4ba786b98703a22659
|
7
|
+
data.tar.gz: 9010d7c2270809e8436b22334f7d36ed864fb7aba114ae51d30873ce3af799358bf80df6167466a440694915f1c4ff4481a8d1e8da88d63035487bb6597a6929
|
data/README.md
CHANGED
@@ -42,7 +42,7 @@ Configure the logger by calling TransactionLogger.logger, such as with Ruby's Lo
|
|
42
42
|
|
43
43
|
```ruby
|
44
44
|
logger = Logger.new STDOUT # Ruby default logger setup
|
45
|
-
TransactionLogger.logger = logger
|
45
|
+
TransactionLogger::Configure.logger = logger
|
46
46
|
```
|
47
47
|
|
48
48
|
Calling Transaction_Logger.logger with no parameter sets the logger to a new instance of Logger as shown above.
|
@@ -52,7 +52,7 @@ Calling Transaction_Logger.logger with no parameter sets the logger to a new ins
|
|
52
52
|
You can add a prefix to every hash key in the log by using the class method log_prefix:
|
53
53
|
|
54
54
|
```ruby
|
55
|
-
TransactionLogger.log_prefix = "transaction_logger_"
|
55
|
+
TransactionLogger::Configure.log_prefix = "transaction_logger_"
|
56
56
|
# output hash:
|
57
57
|
# {
|
58
58
|
# "transaction_logger_name" => "some name"
|
@@ -66,7 +66,7 @@ TransactionLogger.log_prefix = "transaction_logger_"
|
|
66
66
|
You may also choose at which log level the TransactionLogger sends it's log hash. By default, *error* is the threshold, so that if an *error* or *fatal* log is made, then the TransactionLogger will send a JSON hash to it's configured logger. If you wish to set the threshold to *warn*, you can configure the TransactionLogger to do so:
|
67
67
|
|
68
68
|
```ruby
|
69
|
-
TransactionLogger.level_threshold = :warn
|
69
|
+
TransactionLogger::Configure.level_threshold = :warn
|
70
70
|
```
|
71
71
|
|
72
72
|
## Usage
|
@@ -94,16 +94,7 @@ add_transaction_log :some_method, {name: "Custom Name", context: {}}
|
|
94
94
|
|
95
95
|
### Example
|
96
96
|
|
97
|
-
|
98
|
-
|
99
|
-
```ruby
|
100
|
-
logger = Logger.new STDOUT
|
101
|
-
|
102
|
-
# Sets output to the new Logger
|
103
|
-
TransactionLogger.logger = logger
|
104
|
-
```
|
105
|
-
|
106
|
-
Here is a transaction that raises an error:
|
97
|
+
Assuming there is already an instance of Ruby's Logger class, here is a transaction that raises an error:
|
107
98
|
|
108
99
|
```ruby
|
109
100
|
class ExampleClass
|
@@ -152,6 +143,11 @@ The expected output is:
|
|
152
143
|
|
153
144
|
## Version History
|
154
145
|
|
146
|
+
### v1.0.1
|
147
|
+
- Fixed issues with undefined trap_logger method
|
148
|
+
- Hid module methods other than add_transaction_log
|
149
|
+
- TransactionLogger configuration updated
|
150
|
+
|
155
151
|
### v1.0.0
|
156
152
|
|
157
153
|
- AOP approach that provides a much cleaner, easier implementation of the TransactionLogger
|
data/lib/transaction_logger.rb
CHANGED
@@ -5,52 +5,10 @@ require "transaction_logger/transaction_manager"
|
|
5
5
|
module TransactionLogger
|
6
6
|
|
7
7
|
# @private
|
8
|
-
#
|
8
|
+
# Includes ClassMethods of including class to the TransactionLogger
|
9
9
|
#
|
10
10
|
def self.included(base)
|
11
|
-
base.extend
|
12
|
-
end
|
13
|
-
|
14
|
-
# Sets the hash keys on the TransactionLogger's log to have a prefix.
|
15
|
-
#
|
16
|
-
# Using .log_prefix "str_", the output of the log hash will contain keys
|
17
|
-
# prefixed with "str_", such as { "str_name" => "Class.method" }.
|
18
|
-
#
|
19
|
-
# @param prefix [#to_s] Any String or Object that responds to to_s
|
20
|
-
#
|
21
|
-
def self.log_prefix=(prefix)
|
22
|
-
@prefix = "#{prefix}"
|
23
|
-
end
|
24
|
-
|
25
|
-
# @private
|
26
|
-
# Returns the log_prefix
|
27
|
-
#
|
28
|
-
# @return [String] The currently stored prefix.
|
29
|
-
#
|
30
|
-
def self.log_prefix
|
31
|
-
@prefix
|
32
|
-
end
|
33
|
-
|
34
|
-
# Sets the TransactionLogger's output to a specific instance of Logger.
|
35
|
-
#
|
36
|
-
# @param logger [Logger] Any instace of ruby Logger
|
37
|
-
#
|
38
|
-
class << self
|
39
|
-
attr_writer :logger
|
40
|
-
end
|
41
|
-
|
42
|
-
# Sets the TransactionLogger's output to a new instance of Logger
|
43
|
-
#
|
44
|
-
def self.logger
|
45
|
-
@logger ||= Logger.new(STDOUT)
|
46
|
-
end
|
47
|
-
|
48
|
-
# Sets the TransactionLogger's logger level threshold.
|
49
|
-
#
|
50
|
-
# @param level [Symbol] A symbol recognized by logger, such as :warn
|
51
|
-
#
|
52
|
-
class << self
|
53
|
-
attr_writer :level_threshold
|
11
|
+
base.extend ClassMethods
|
54
12
|
end
|
55
13
|
|
56
14
|
module ClassMethods
|
@@ -70,11 +28,10 @@ module TransactionLogger
|
|
70
28
|
def add_transaction_log(method, options={})
|
71
29
|
old_method = instance_method method
|
72
30
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
options = { prefix: prefix, logger: logger, level_threshold: level_threshold }
|
31
|
+
options = {}
|
32
|
+
options[:prefix] = TransactionLogger::Configure.instance_variable_get :@prefix
|
33
|
+
options[:logger] = TransactionLogger::Configure.instance_variable_get :@logger
|
34
|
+
options[:level_threshold] = TransactionLogger::Configure.instance_variable_get :@level_threshold
|
78
35
|
|
79
36
|
define_method method do
|
80
37
|
TransactionManager.start options, lambda { |transaction|
|
@@ -83,28 +40,82 @@ module TransactionLogger
|
|
83
40
|
transaction.context = options[:context]
|
84
41
|
transaction.context ||= {}
|
85
42
|
|
86
|
-
|
43
|
+
method_info = {}
|
44
|
+
method_info[:logger_method] = self.class.instance_method :logger
|
45
|
+
method_info[:calling_method] = caller_locations(1, 1)[0].label
|
46
|
+
method_info[:includer] = self
|
47
|
+
|
48
|
+
TransactionLogger::Helper.trap_logger method, transaction, method_info
|
87
49
|
old_method.bind(self).call
|
88
50
|
}
|
89
51
|
end
|
90
52
|
end
|
91
53
|
|
54
|
+
end
|
55
|
+
|
56
|
+
class Helper
|
92
57
|
# @private
|
93
58
|
# Traps the original logger inside the TransactionLogger
|
94
59
|
#
|
95
60
|
# @param method [Symbol]
|
96
61
|
# @param transaction [Transaction]
|
97
62
|
#
|
98
|
-
def trap_logger(_method, transaction)
|
99
|
-
logger_method =
|
63
|
+
def self.trap_logger(_method, transaction, method_info={})
|
64
|
+
logger_method = method_info[:logger_method]
|
65
|
+
calling_method = method_info[:calling_method]
|
66
|
+
includer = method_info[:includer]
|
100
67
|
|
101
|
-
define_method :logger
|
102
|
-
@original_logger ||= logger_method.bind(
|
103
|
-
calling_method = caller_locations(1, 1)[0].label
|
68
|
+
includer.class.send :define_method, :logger, lambda {
|
69
|
+
@original_logger ||= logger_method.bind(includer).call
|
104
70
|
|
105
71
|
@trapped_logger ||= {}
|
106
72
|
@trapped_logger[calling_method] ||= LoggerProxy.new @original_logger, transaction
|
107
|
-
|
73
|
+
}
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
class Configure
|
79
|
+
# Sets the hash keys on the TransactionLogger's log to have a prefix.
|
80
|
+
#
|
81
|
+
# Using .log_prefix "str_", the output of the log hash will contain keys
|
82
|
+
# prefixed with "str_", such as { "str_name" => "Class.method" }.
|
83
|
+
#
|
84
|
+
# @param prefix [#to_s] Any String or Object that responds to to_s
|
85
|
+
#
|
86
|
+
def self.log_prefix=(prefix)
|
87
|
+
@prefix = prefix
|
88
|
+
end
|
89
|
+
|
90
|
+
# @private
|
91
|
+
# Returns the log_prefix
|
92
|
+
#
|
93
|
+
# @return [String] The currently stored prefix.
|
94
|
+
#
|
95
|
+
def self.log_prefix
|
96
|
+
@prefix
|
97
|
+
end
|
98
|
+
|
99
|
+
# Sets the TransactionLogger's output to a specific instance of Logger.
|
100
|
+
#
|
101
|
+
# @param logger [Logger] Any instace of ruby Logger
|
102
|
+
#
|
103
|
+
class << self
|
104
|
+
attr_writer :logger
|
105
|
+
end
|
106
|
+
|
107
|
+
# Sets the TransactionLogger's output to a new instance of Logger
|
108
|
+
#
|
109
|
+
def self.logger
|
110
|
+
@logger ||= Logger.new(STDOUT)
|
111
|
+
end
|
112
|
+
|
113
|
+
# Sets the TransactionLogger's logger level threshold.
|
114
|
+
#
|
115
|
+
# @param level [Symbol] A symbol recognized by logger, such as :warn
|
116
|
+
#
|
117
|
+
class << self
|
118
|
+
attr_writer :level_threshold
|
108
119
|
end
|
109
120
|
end
|
110
121
|
|
@@ -46,31 +46,33 @@ describe TransactionLogger do
|
|
46
46
|
}
|
47
47
|
end
|
48
48
|
|
49
|
-
describe
|
50
|
-
|
51
|
-
|
52
|
-
|
49
|
+
describe TransactionLogger::Configure do
|
50
|
+
describe ".log_prefix" do
|
51
|
+
context "when there is no prefix" do
|
52
|
+
it "does not change the output" do
|
53
|
+
expect(subject.to_hash).to include("name" => "undefined")
|
54
|
+
end
|
53
55
|
end
|
54
|
-
end
|
55
56
|
|
56
|
-
|
57
|
-
|
57
|
+
context "when a prefix is defined" do
|
58
|
+
let (:prefix) { "bta_" }
|
58
59
|
|
59
|
-
|
60
|
-
|
61
|
-
|
60
|
+
before :example do
|
61
|
+
described_class.log_prefix = prefix
|
62
|
+
end
|
62
63
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
64
|
+
subject {
|
65
|
+
TransactionLogger::Transaction.new(
|
66
|
+
{ prefix: described_class.log_prefix, logger: Logger.new(STDOUT), level_threshold: nil }, test_lmbda)
|
67
|
+
}
|
67
68
|
|
68
|
-
|
69
|
-
|
70
|
-
|
69
|
+
after :example do
|
70
|
+
described_class.log_prefix = ""
|
71
|
+
end
|
71
72
|
|
72
|
-
|
73
|
-
|
73
|
+
it "adds the prefix to every key" do
|
74
|
+
expect(subject.to_hash).to include("bta_name" => "undefined")
|
75
|
+
end
|
74
76
|
end
|
75
77
|
end
|
76
78
|
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.1
|
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-07-
|
12
|
+
date: 2015-07-21 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -142,7 +142,6 @@ files:
|
|
142
142
|
- ".yardopts"
|
143
143
|
- Gemfile
|
144
144
|
- LICENSE
|
145
|
-
- LICENSE.txt
|
146
145
|
- README.md
|
147
146
|
- Rakefile
|
148
147
|
- lib/transaction_logger.rb
|
@@ -173,7 +172,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
173
172
|
version: '0'
|
174
173
|
requirements: []
|
175
174
|
rubyforge_project:
|
176
|
-
rubygems_version: 2.4.
|
175
|
+
rubygems_version: 2.4.3
|
177
176
|
signing_key:
|
178
177
|
specification_version: 4
|
179
178
|
summary: Contextual Business Transaction Logger for Ruby
|
data/LICENSE.txt
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
Copyright (c) 2015 Sebastian Schleicher
|
2
|
-
|
3
|
-
MIT License
|
4
|
-
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
-
a copy of this software and associated documentation files (the
|
7
|
-
"Software"), to deal in the Software without restriction, including
|
8
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
-
permit persons to whom the Software is furnished to do so, subject to
|
11
|
-
the following conditions:
|
12
|
-
|
13
|
-
The above copyright notice and this permission notice shall be
|
14
|
-
included in all copies or substantial portions of the Software.
|
15
|
-
|
16
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|