twiglet 3.10.0 → 3.12.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: 7f7f408c7f0546be8ef926a307e70a58af7a7ff3427b1744bdbb4cfbc4653189
4
+ data.tar.gz: aaf5bce57110c3accb2353250c528a525b0948fbfda377f3b6fc6308e50030ba
5
5
  SHA512:
6
- metadata.gz: cd56a05ee8a42910901a85de11940ea16b1e6a4578fd2501aeb4e794674561dd9594bb10fedc0b07c207df5823c44361cf1a72d753b241fe4e564e786464f459
7
- data.tar.gz: 7fa75694d0ff55caa28df71703fd4e8c14e00185149c456b26c8030315519e0caafcc77e0e10efe1d04aae1093dd53c9f1b4462b97f04d8352c996241a271662
6
+ metadata.gz: 830da1bdf6311f12b6a92cab29c8e25453d8ee0bd0007d3b41e211a628983ea20c3956dc6cab9b6d991901ebb7cfb718f0296b11b82e89c20deee187a4f6ae87
7
+ data.tar.gz: 140e91a61d0883769aea1e0828022ae0662750a571fc35531f3604d1708a24efbfc2b229624faef3855234d59048134c8f29700505d8ca674f7a9a830e5d1465
@@ -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
@@ -22,7 +22,7 @@ module Twiglet
22
22
 
23
23
  now = args.fetch(:now, -> { Time.now.utc })
24
24
  output = args.fetch(:output, $stdout)
25
- level = args.fetch(:level, DEBUG)
25
+ level = args.fetch(:level, INFO)
26
26
  validation_schema = args.fetch(:validation_schema, DEFAULT_VALIDATION_SCHEMA)
27
27
 
28
28
  raise 'Service name is mandatory' \
@@ -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.12.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
@@ -23,7 +23,8 @@ describe Twiglet::Logger do
23
23
  @logger = Twiglet::Logger.new(
24
24
  'petshop',
25
25
  now: @now,
26
- output: @buffer
26
+ output: @buffer,
27
+ level: Logger::DEBUG
27
28
  )
28
29
  end
29
30
 
@@ -189,9 +190,37 @@ describe Twiglet::Logger do
189
190
  end
190
191
 
191
192
  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
193
+ provider = -> { { 'context' => { 'id' => 'my-context-id' } } }
194
+ purchase_logger = Twiglet::Logger.new(
195
+ 'petshop',
196
+ now: @now,
197
+ output: @buffer,
198
+ context_provider: provider
199
+ )
200
+
201
+ # do stuff
202
+ purchase_logger.info(
203
+ {
204
+ message: 'customer bought a dog',
205
+ pet: { name: 'Barker', species: 'dog', breed: 'Bitsa' }
206
+ }
207
+ )
208
+
209
+ log = read_json @buffer
210
+
211
+ assert_equal 'customer bought a dog', log[:message]
212
+ assert_equal 'my-context-id', log[:context][:id]
213
+ end
214
+
215
+ it "should be able to add contextual information to events with multiple context providers" do
216
+ provider_1 = -> { { 'context' => { 'id' => 'my-context-id' } } }
217
+ provider_2 = -> { { 'context' => { 'type' => 'test' } } }
218
+ purchase_logger = Twiglet::Logger.new(
219
+ 'petshop',
220
+ now: @now,
221
+ output: @buffer,
222
+ context_providers: [provider_1, provider_2]
223
+ )
195
224
 
196
225
  # do stuff
197
226
  purchase_logger.info(
@@ -205,6 +234,7 @@ describe Twiglet::Logger do
205
234
 
206
235
  assert_equal 'customer bought a dog', log[:message]
207
236
  assert_equal 'my-context-id', log[:context][:id]
237
+ assert_equal 'test', log[:context][:type]
208
238
  end
209
239
 
210
240
  it "chaining .with and .context_provider is possible" do
@@ -623,7 +653,8 @@ describe Twiglet::Logger do
623
653
  'petshop',
624
654
  now: @now,
625
655
  output: @buffer,
626
- validation_schema: validation_schema
656
+ validation_schema: validation_schema,
657
+ level: Logger::DEBUG
627
658
  )
628
659
  end
629
660
 
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.12.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-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json-schema