twiglet 3.10.0 → 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: 6362109470f346c6703ed9e13e82904195f201cac31e1edf1dad8714c4312d79
4
- data.tar.gz: a06b359e837cf995f78efdab94c53e5017ccc4ca6d06373a05148f0e4b93621c
3
+ metadata.gz: 99e8d740d7d9a800eef5d22fe68252f41bd6d75042d2a8f69596b9361a6daf4d
4
+ data.tar.gz: fe9ac8d41fed72423a4ee2e9b209f0c10f742546fbd36704a1c391dba6004e64
5
5
  SHA512:
6
- metadata.gz: cd56a05ee8a42910901a85de11940ea16b1e6a4578fd2501aeb4e794674561dd9594bb10fedc0b07c207df5823c44361cf1a72d753b241fe4e564e786464f459
7
- data.tar.gz: 7fa75694d0ff55caa28df71703fd4e8c14e00185149c456b26c8030315519e0caafcc77e0e10efe1d04aae1093dd53c9f1b4462b97f04d8352c996241a271662
6
+ metadata.gz: d14b3282ec480b9d628902261ac467f89bf5f635723d420a1b05607b7c6c4f549f6aaa70d0eba8513c23100e1f8142ed2099b07295c0ce288b85108478861daa
7
+ data.tar.gz: 9d8119f8ce88a5fed9a0cc1e159180d623a194ee2eacfa0ac898a3d289b6318343c3583764223d132bc0cc1550b7c37bac2bcd2f6b39e530747f9f1d1a201f6f
@@ -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,15 +82,12 @@ module Twiglet
82
82
  end
83
83
 
84
84
  def context_provider(&blk)
85
- new_context_provider = blk
86
- if @args[:context_provider]
87
- new_context_provider = lambda do
88
- @args[:context_provider].call.merge(blk.call)
89
- end
90
- end
85
+ new_context_providers = Array(@args[:context_providers])
86
+ new_context_providers << blk
87
+
91
88
  self.class.new(
92
89
  @service_name,
93
- **@args.merge(context_provider: new_context_provider)
90
+ **@args.merge(context_providers: new_context_providers)
94
91
  )
95
92
  end
96
93
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Twiglet
4
- VERSION = '3.10.0'
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
@@ -189,9 +189,37 @@ describe Twiglet::Logger do
189
189
  end
190
190
 
191
191
  it "should be able to add contextual information to events with the context_provider" do
192
- purchase_logger = @logger.context_provider do
193
- { 'context' => { 'id' => 'my-context-id' } }
194
- 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
+ )
199
+
200
+ # do stuff
201
+ purchase_logger.info(
202
+ {
203
+ message: 'customer bought a dog',
204
+ pet: { name: 'Barker', species: 'dog', breed: 'Bitsa' }
205
+ }
206
+ )
207
+
208
+ log = read_json @buffer
209
+
210
+ assert_equal 'customer bought a dog', log[:message]
211
+ assert_equal 'my-context-id', log[:context][:id]
212
+ end
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
+ )
195
223
 
196
224
  # do stuff
197
225
  purchase_logger.info(
@@ -205,6 +233,7 @@ describe Twiglet::Logger do
205
233
 
206
234
  assert_equal 'customer bought a dog', log[:message]
207
235
  assert_equal 'my-context-id', log[:context][:id]
236
+ assert_equal 'test', log[:context][:type]
208
237
  end
209
238
 
210
239
  it "chaining .with and .context_provider is possible" do
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.10.0
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: 2024-08-02 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