twiglet 3.7.2 → 3.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fdfe6db753dcf115b7965ff0cfd9682e23694b43765fd69cdd64986e7fc51d41
4
- data.tar.gz: fb110c32f6b4556efd1eb3f4ff673ca7b2e975008973ad296445389d2372a8c8
3
+ metadata.gz: 1cdc9271dfb49fa3e76ebd4684c2b6ad6164aba6a67c6586ebbd6c13c59d9070
4
+ data.tar.gz: 77d75cab942109f1d6895d7cf92179df48d8ecccef266beb9bff8d11dc1f942a
5
5
  SHA512:
6
- metadata.gz: 7d0add22464cd885a861d2d7c5e85d7b7e2a444cb4516c26c261ea9992b1e2988a2a7e47d17d4d123d8131b34198af2d084350c2aa7262b44db513fccc33ea11
7
- data.tar.gz: d9772fd14f86d73b17c25a0b4f469b1ff9248ecba733d0d09d31be1d6c6c69c01a2e4d8e1152d68b2b8fce57f1acb13745ae45d5069846df38119663ea8262ae
6
+ metadata.gz: 46b45ee3e218bf6499264f930d1aa8c4d60200c6b73e444400f640a764f29873eb4c3c49bf86c711cf38faf2c36b7b00005729e87b374180dfa6c214fd47d1bb
7
+ data.tar.gz: dc62148dac2c6b797eacc86b01b001f54c643732256fbd682adb369eeb522ed6379849ca623fa3e658e7972c6c781149d047423251100e7c4251f84e0702ebd5
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Twiglet
4
+ class ErrorSerialiser
5
+ def serialise_error(error)
6
+ error_fields = {
7
+ error: {
8
+ type: error.class.to_s,
9
+ message: error.message
10
+ }
11
+ }
12
+ add_stack_trace(error_fields, error)
13
+ end
14
+
15
+ private
16
+
17
+ def add_stack_trace(hash_to_add_to, error)
18
+ hash_to_add_to[:error][:stack_trace] = error.backtrace if error.backtrace
19
+ hash_to_add_to
20
+ end
21
+ end
22
+ end
@@ -34,7 +34,7 @@ module Twiglet
34
34
  ecs: {
35
35
  version: '1.5.0'
36
36
  },
37
- "@timestamp": @now.call.iso8601(3),
37
+ '@timestamp': @now.call.iso8601(3),
38
38
  service: {
39
39
  name: @service_name
40
40
  },
@@ -6,6 +6,7 @@ require_relative 'formatter'
6
6
  require_relative '../hash_extensions'
7
7
  require_relative 'message'
8
8
  require_relative 'validator'
9
+ require_relative 'error_serialiser'
9
10
 
10
11
  module Twiglet
11
12
  class Logger < ::Logger
@@ -87,19 +88,9 @@ module Twiglet
87
88
  private
88
89
 
89
90
  def error_message(error, message = nil)
90
- error_fields = {
91
- error: {
92
- type: error.class.to_s,
93
- message: error.message
94
- }
95
- }
96
- add_stack_trace(error_fields, error)
91
+ error_hash = Twiglet::ErrorSerialiser.new.serialise_error(error)
97
92
  message = error.message if message.nil? || message.empty?
98
- Message.new(message).merge(error_fields)
99
- end
100
-
101
- def add_stack_trace(hash_to_add_to, error)
102
- hash_to_add_to[:error][:stack_trace] = error.backtrace if error.backtrace
93
+ Message.new(message).merge(error_hash)
103
94
  end
104
95
  end
105
96
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Twiglet
4
- VERSION = '3.7.2'
4
+ VERSION = '3.8.0'
5
5
  end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'minitest/autorun'
4
+ require 'minitest/mock'
5
+ require_relative '../lib/twiglet/error_serialiser'
6
+
7
+ describe Twiglet::ErrorSerialiser do
8
+ describe 'logging an exception' do
9
+ it 'should log an error with backtrace' do
10
+ 1 / 0
11
+ rescue StandardError => e
12
+ error_hash = Twiglet::ErrorSerialiser.new.serialise_error(e)
13
+ assert_equal 'divided by 0', error_hash[:error][:message]
14
+ assert_equal 'ZeroDivisionError', error_hash[:error][:type]
15
+ assert_match 'test/error_serialiser_test.rb', error_hash[:error][:stack_trace].first
16
+ end
17
+ end
18
+ end
@@ -17,7 +17,7 @@ describe HashExtensions do
17
17
  log: {
18
18
  level: 'error'
19
19
  },
20
- "@timestamp": '2020-05-09T15:13:20.736Z'
20
+ '@timestamp': '2020-05-09T15:13:20.736Z'
21
21
  }
22
22
 
23
23
  expected = actual.to_nested
@@ -26,8 +26,8 @@ describe HashExtensions do
26
26
 
27
27
  it 'should convert keys with . into nested objects' do
28
28
  actual = {
29
- "service.name": 'petshop',
30
- "log.level": 'error'
29
+ 'service.name': 'petshop',
30
+ 'log.level': 'error'
31
31
  }
32
32
 
33
33
  nested = actual.to_nested
@@ -38,10 +38,10 @@ describe HashExtensions do
38
38
 
39
39
  it 'should group nested objects' do
40
40
  actual = {
41
- "service.name": 'petshop',
42
- "service.id": 'ps001',
43
- "service.version": '0.9.1',
44
- "log.level": 'error'
41
+ 'service.name': 'petshop',
42
+ 'service.id': 'ps001',
43
+ 'service.version': '0.9.1',
44
+ 'log.level': 'error'
45
45
  }
46
46
 
47
47
  nested = actual.to_nested
@@ -54,10 +54,10 @@ describe HashExtensions do
54
54
 
55
55
  it 'should cope with more than two levels' do
56
56
  actual = {
57
- "http.request.method": 'get',
58
- "http.request.body.bytes": 112,
59
- "http.response.bytes": 1564,
60
- "http.response.status_code": 200
57
+ 'http.request.method': 'get',
58
+ 'http.request.body.bytes': 112,
59
+ 'http.response.bytes': 1564,
60
+ 'http.response.status_code': 200
61
61
  }
62
62
 
63
63
  nested = actual.to_nested
data/test/logger_test.rb CHANGED
@@ -63,7 +63,7 @@ describe Twiglet::Logger do
63
63
  ecs: {
64
64
  version: '1.5.0'
65
65
  },
66
- "@timestamp": '2020-05-11T15:01:01.000Z',
66
+ '@timestamp': '2020-05-11T15:01:01.000Z',
67
67
  service: {
68
68
  name: 'petshop'
69
69
  },
@@ -269,19 +269,19 @@ describe Twiglet::Logger do
269
269
  @logger.info({ message: 'there' })
270
270
 
271
271
  expected_output =
272
- '{"ecs":{"version":"1.5.0"},"@timestamp":"2020-05-11T15:01:01.000Z",'\
273
- '"service":{"name":"petshop"},"log":{"level":"debug"},"message":"hi"}'\
274
- "\n"\
275
- '{"ecs":{"version":"1.5.0"},"@timestamp":"2020-05-11T15:01:01.000Z",'\
276
- '"service":{"name":"petshop"},"log":{"level":"info"},"message":"there"}'\
277
- "\n"\
272
+ '{"ecs":{"version":"1.5.0"},"@timestamp":"2020-05-11T15:01:01.000Z",' \
273
+ '"service":{"name":"petshop"},"log":{"level":"debug"},"message":"hi"}' \
274
+ "\n" \
275
+ '{"ecs":{"version":"1.5.0"},"@timestamp":"2020-05-11T15:01:01.000Z",' \
276
+ '"service":{"name":"petshop"},"log":{"level":"info"},"message":"there"}' \
277
+ "\n" \
278
278
 
279
279
  assert_equal expected_output, @buffer.string
280
280
  end
281
281
 
282
282
  it 'should work with mixed string and symbol properties' do
283
283
  log = {
284
- "trace.id": '1c8a5fb2-fecd-44d8-92a4-449eb2ce4dcb'
284
+ 'trace.id': '1c8a5fb2-fecd-44d8-92a4-449eb2ce4dcb'
285
285
  }
286
286
  event = {}
287
287
  log['event'] = event
@@ -404,7 +404,7 @@ describe Twiglet::Logger do
404
404
  ecs: {
405
405
  version: '1.5.0'
406
406
  },
407
- "@timestamp": '2020-05-11T15:01:01.000Z',
407
+ '@timestamp': '2020-05-11T15:01:01.000Z',
408
408
  service: {
409
409
  name: 'petshop'
410
410
  },
@@ -460,11 +460,11 @@ describe Twiglet::Logger do
460
460
  it 'should be able to convert dotted keys to nested objects' do
461
461
  @logger.debug(
462
462
  {
463
- "trace.id": '1c8a5fb2-fecd-44d8-92a4-449eb2ce4dcb',
463
+ 'trace.id': '1c8a5fb2-fecd-44d8-92a4-449eb2ce4dcb',
464
464
  message: 'customer bought a dog',
465
- "pet.name": 'Barker',
466
- "pet.species": 'dog',
467
- "pet.breed": 'Bitsa'
465
+ 'pet.name': 'Barker',
466
+ 'pet.species': 'dog',
467
+ 'pet.breed': 'Bitsa'
468
468
  }
469
469
  )
470
470
  log = read_json(@buffer)
@@ -479,10 +479,10 @@ describe Twiglet::Logger do
479
479
  it 'should be able to mix dotted keys and nested objects' do
480
480
  @logger.debug(
481
481
  {
482
- "trace.id": '1c8a5fb2-fecd-44d8-92a4-449eb2ce4dcb',
482
+ 'trace.id': '1c8a5fb2-fecd-44d8-92a4-449eb2ce4dcb',
483
483
  message: 'customer bought a dog',
484
484
  pet: { name: 'Barker', breed: 'Bitsa' },
485
- "pet.species": 'dog'
485
+ 'pet.species': 'dog'
486
486
  }
487
487
  )
488
488
  log = read_json(@buffer)
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.7.2
4
+ version: 3.8.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-04-11 00:00:00.000000000 Z
11
+ date: 2023-05-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json-schema
@@ -113,6 +113,7 @@ files:
113
113
  - examples/rack/request_logger.rb
114
114
  - examples/rack/request_logger_test.rb
115
115
  - lib/hash_extensions.rb
116
+ - lib/twiglet/error_serialiser.rb
116
117
  - lib/twiglet/formatter.rb
117
118
  - lib/twiglet/logger.rb
118
119
  - lib/twiglet/message.rb
@@ -120,6 +121,7 @@ files:
120
121
  - lib/twiglet/validator.rb
121
122
  - lib/twiglet/version.rb
122
123
  - mkdocs.yml
124
+ - test/error_serialiser_test.rb
123
125
  - test/formatter_test.rb
124
126
  - test/hash_extensions_test.rb
125
127
  - test/logger_test.rb