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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3b87b30fcde8a6363eb43ef4451718f3e15066a686b6ecd5340cb504cc3f0ebd
4
- data.tar.gz: 6efcea94e5b2623e3c01e974f34ad80f0c7238fb655f306415ad75a7105b0164
3
+ metadata.gz: 99e8d740d7d9a800eef5d22fe68252f41bd6d75042d2a8f69596b9361a6daf4d
4
+ data.tar.gz: fe9ac8d41fed72423a4ee2e9b209f0c10f742546fbd36704a1c391dba6004e64
5
5
  SHA512:
6
- metadata.gz: 962d159d17c025956e0294d07ae9f452f10298c17fdaa3d56eb56e7547eee03ef324d260d58151553ada630eb2a5326650d22b6649ceb00ca8412dda7b27cb92
7
- data.tar.gz: e5a2394439c12bedc56de394e5275ae383c4c40debba8cdcd9a81aefe962afc9d81089df599ed6201103646136969356d7101bb570be8736987cfb3c150edb65
6
+ metadata.gz: d14b3282ec480b9d628902261ac467f89bf5f635723d420a1b05607b7c6c4f549f6aaa70d0eba8513c23100e1f8142ed2099b07295c0ce288b85108478861daa
7
+ data.tar.gz: 9d8119f8ce88a5fed9a0cc1e159180d623a194ee2eacfa0ac898a3d289b6318343c3583764223d132bc0cc1550b7c37bac2bcd2f6b39e530747f9f1d1a201f6f
@@ -2,7 +2,7 @@
2
2
 
3
3
  module HashExtensions
4
4
  def to_nested
5
- self unless contains_dotted_key?
5
+ return self unless contains_dotted_key?
6
6
 
7
7
  keys.reduce({}) do |nested, key|
8
8
  nested.deep_merge(build_nested_object(key, self[key]))
@@ -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
- @context_provider = context_provider
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 = @context_provider&.call || {}
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
@@ -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
- context_provider: args[:context_provider],
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(context_provider: blk)
90
+ **@args.merge(context_providers: new_context_providers)
88
91
  )
89
92
  end
90
93
 
@@ -8,7 +8,7 @@ module Twiglet
8
8
  when Hash
9
9
  replace(msg.transform_keys!(&:to_sym))
10
10
  else
11
- super(msg)
11
+ super
12
12
  end
13
13
  end
14
14
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Twiglet
4
- VERSION = '3.9.2'
4
+ VERSION = '3.11.0'
5
5
  end
@@ -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
- purchase_logger = @logger.context_provider do
192
- { 'context' => { 'id' => 'my-context-id' } }
193
- end
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/json is required' do
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.add_runtime_dependency 'json-schema'
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.9.2
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: 2023-09-27 00:00:00.000000000 Z
11
+ date: 2024-08-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json-schema