twiglet 3.9.2 → 3.11.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.
- checksums.yaml +4 -4
- data/lib/hash_extensions.rb +1 -1
- data/lib/twiglet/formatter.rb +5 -2
- data/lib/twiglet/logger.rb +5 -2
- data/lib/twiglet/message.rb +1 -1
- data/lib/twiglet/version.rb +1 -1
- data/test/formatter_test.rb +28 -0
- data/test/logger_test.rb +55 -5
- data/twiglet.gemspec +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 99e8d740d7d9a800eef5d22fe68252f41bd6d75042d2a8f69596b9361a6daf4d
|
4
|
+
data.tar.gz: fe9ac8d41fed72423a4ee2e9b209f0c10f742546fbd36704a1c391dba6004e64
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d14b3282ec480b9d628902261ac467f89bf5f635723d420a1b05607b7c6c4f549f6aaa70d0eba8513c23100e1f8142ed2099b07295c0ce288b85108478861daa
|
7
|
+
data.tar.gz: 9d8119f8ce88a5fed9a0cc1e159180d623a194ee2eacfa0ac898a3d289b6318343c3583764223d132bc0cc1550b7c37bac2bcd2f6b39e530747f9f1d1a201f6f
|
data/lib/hash_extensions.rb
CHANGED
data/lib/twiglet/formatter.rb
CHANGED
@@ -11,12 +11,13 @@ module Twiglet
|
|
11
11
|
validator:,
|
12
12
|
default_properties: {},
|
13
13
|
context_provider: nil,
|
14
|
+
context_providers: [],
|
14
15
|
now: -> { Time.now.utc }
|
15
16
|
)
|
16
17
|
@service_name = service_name
|
17
18
|
@now = now
|
18
19
|
@default_properties = default_properties
|
19
|
-
@
|
20
|
+
@context_providers = context_provider ? [context_provider] : context_providers
|
20
21
|
@validator = validator
|
21
22
|
|
22
23
|
super()
|
@@ -45,7 +46,9 @@ module Twiglet
|
|
45
46
|
}
|
46
47
|
}
|
47
48
|
|
48
|
-
context = @
|
49
|
+
context = @context_providers.reduce({}) do |c, context_provider|
|
50
|
+
c.deep_merge(context_provider.call)
|
51
|
+
end
|
49
52
|
|
50
53
|
JSON.generate(
|
51
54
|
base_message
|
data/lib/twiglet/logger.rb
CHANGED
@@ -33,7 +33,7 @@ module Twiglet
|
|
33
33
|
formatter = Twiglet::Formatter.new(
|
34
34
|
service_name,
|
35
35
|
default_properties: args.fetch(:default_properties, {}),
|
36
|
-
|
36
|
+
context_providers: Array(args[:context_provider] || args[:context_providers]),
|
37
37
|
now: now,
|
38
38
|
validator: @validator
|
39
39
|
)
|
@@ -82,9 +82,12 @@ module Twiglet
|
|
82
82
|
end
|
83
83
|
|
84
84
|
def context_provider(&blk)
|
85
|
+
new_context_providers = Array(@args[:context_providers])
|
86
|
+
new_context_providers << blk
|
87
|
+
|
85
88
|
self.class.new(
|
86
89
|
@service_name,
|
87
|
-
**@args.merge(
|
90
|
+
**@args.merge(context_providers: new_context_providers)
|
88
91
|
)
|
89
92
|
end
|
90
93
|
|
data/lib/twiglet/message.rb
CHANGED
data/lib/twiglet/version.rb
CHANGED
data/test/formatter_test.rb
CHANGED
@@ -58,4 +58,32 @@ describe Twiglet::Formatter do
|
|
58
58
|
}
|
59
59
|
assert_equal JSON.parse(msg), expected_log
|
60
60
|
end
|
61
|
+
|
62
|
+
it 'merges the outputs of all context providers into the messages log' do
|
63
|
+
provider_1 = -> { { 'request' => { 'id' => '1234567890' } } }
|
64
|
+
provider_2 = -> { { 'request' => { 'type' => 'test' } } }
|
65
|
+
formatter = Twiglet::Formatter.new(
|
66
|
+
'petshop', now: @now, validator: Twiglet::Validator.new({}.to_json),
|
67
|
+
context_providers: [provider_1, provider_2]
|
68
|
+
)
|
69
|
+
msg = formatter.call('warn', nil, nil, 'shop is running low on dog food')
|
70
|
+
expected_log = {
|
71
|
+
"ecs" => {
|
72
|
+
"version" => '1.5.0'
|
73
|
+
},
|
74
|
+
"@timestamp" => '2020-05-11T15:01:01.000Z',
|
75
|
+
"service" => {
|
76
|
+
"name" => 'petshop'
|
77
|
+
},
|
78
|
+
"log" => {
|
79
|
+
"level" => 'warn'
|
80
|
+
},
|
81
|
+
"message" => 'shop is running low on dog food',
|
82
|
+
"request" => {
|
83
|
+
'id' => '1234567890',
|
84
|
+
'type' => 'test'
|
85
|
+
}
|
86
|
+
}
|
87
|
+
assert_equal expected_log, JSON.parse(msg)
|
88
|
+
end
|
61
89
|
end
|
data/test/logger_test.rb
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
require 'minitest/autorun'
|
4
4
|
require 'minitest/mock'
|
5
5
|
require_relative '../lib/twiglet/logger'
|
6
|
+
require 'active_support'
|
6
7
|
|
7
8
|
LEVELS = [
|
8
9
|
{ method: :debug, level: 'debug' },
|
@@ -188,9 +189,13 @@ describe Twiglet::Logger do
|
|
188
189
|
end
|
189
190
|
|
190
191
|
it "should be able to add contextual information to events with the context_provider" do
|
191
|
-
|
192
|
-
|
193
|
-
|
192
|
+
provider = -> { { 'context' => { 'id' => 'my-context-id' } } }
|
193
|
+
purchase_logger = Twiglet::Logger.new(
|
194
|
+
'petshop',
|
195
|
+
now: @now,
|
196
|
+
output: @buffer,
|
197
|
+
context_provider: provider
|
198
|
+
)
|
194
199
|
|
195
200
|
# do stuff
|
196
201
|
purchase_logger.info(
|
@@ -206,6 +211,31 @@ describe Twiglet::Logger do
|
|
206
211
|
assert_equal 'my-context-id', log[:context][:id]
|
207
212
|
end
|
208
213
|
|
214
|
+
it "should be able to add contextual information to events with multiple context providers" do
|
215
|
+
provider_1 = -> { { 'context' => { 'id' => 'my-context-id' } } }
|
216
|
+
provider_2 = -> { { 'context' => { 'type' => 'test' } } }
|
217
|
+
purchase_logger = Twiglet::Logger.new(
|
218
|
+
'petshop',
|
219
|
+
now: @now,
|
220
|
+
output: @buffer,
|
221
|
+
context_providers: [provider_1, provider_2]
|
222
|
+
)
|
223
|
+
|
224
|
+
# do stuff
|
225
|
+
purchase_logger.info(
|
226
|
+
{
|
227
|
+
message: 'customer bought a dog',
|
228
|
+
pet: { name: 'Barker', species: 'dog', breed: 'Bitsa' }
|
229
|
+
}
|
230
|
+
)
|
231
|
+
|
232
|
+
log = read_json @buffer
|
233
|
+
|
234
|
+
assert_equal 'customer bought a dog', log[:message]
|
235
|
+
assert_equal 'my-context-id', log[:context][:id]
|
236
|
+
assert_equal 'test', log[:context][:type]
|
237
|
+
end
|
238
|
+
|
209
239
|
it "chaining .with and .context_provider is possible" do
|
210
240
|
# Let's add some context to this customer journey
|
211
241
|
purchase_logger = @logger.with(
|
@@ -266,6 +296,27 @@ describe Twiglet::Logger do
|
|
266
296
|
assert_equal 'my-context-id', log[:context][:id]
|
267
297
|
end
|
268
298
|
|
299
|
+
it "previously supplied context providers should be preserved" do
|
300
|
+
# Let's add some context to this customer journey
|
301
|
+
purchase_logger = @logger
|
302
|
+
.context_provider { { 'first-context' => { 'first-id' => 'my-first-context-id' } } }
|
303
|
+
.context_provider { { 'second-context' => { 'second-id' => 'my-second-context-id' } } }
|
304
|
+
# do stuff
|
305
|
+
purchase_logger.info(
|
306
|
+
{
|
307
|
+
message: 'customer bought a dog',
|
308
|
+
pet: { name: 'Barker', species: 'dog', breed: 'Bitsa' }
|
309
|
+
}
|
310
|
+
)
|
311
|
+
|
312
|
+
log = read_json @buffer
|
313
|
+
|
314
|
+
assert_equal 'customer bought a dog', log[:message]
|
315
|
+
assert_equal 'Barker', log[:pet][:name]
|
316
|
+
assert_equal 'my-first-context-id', log[:'first-context'][:'first-id']
|
317
|
+
assert_equal 'my-second-context-id', log[:'second-context'][:'second-id']
|
318
|
+
end
|
319
|
+
|
269
320
|
it "should log 'message' string property" do
|
270
321
|
message = {}
|
271
322
|
message['message'] = 'Guinea pigs arrived'
|
@@ -393,8 +444,7 @@ describe Twiglet::Logger do
|
|
393
444
|
assert_equal 'Some error', actual_log[:message]
|
394
445
|
end
|
395
446
|
|
396
|
-
it 'should log error type properly even when active_support
|
397
|
-
require 'active_support/json'
|
447
|
+
it 'should log error type properly even when active_support is required' do
|
398
448
|
e = StandardError.new('Unknown error')
|
399
449
|
@logger.error('Artificially raised exception with string message', e)
|
400
450
|
|
data/twiglet.gemspec
CHANGED
@@ -21,7 +21,7 @@ Gem::Specification.new do |gem|
|
|
21
21
|
|
22
22
|
gem.license = 'Copyright SimplyBusiness'
|
23
23
|
|
24
|
-
gem.
|
24
|
+
gem.add_dependency 'json-schema'
|
25
25
|
gem.add_development_dependency 'minitest'
|
26
26
|
gem.add_development_dependency 'rake'
|
27
27
|
gem.add_development_dependency 'simplecov', '0.17.1'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: twiglet
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.11.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Simply Business
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-08-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json-schema
|