twiglet 3.2.0 → 3.3.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: 6970a1b98fbd77737c3742429691e3c03c7966e894e2f6a523824df785941858
4
- data.tar.gz: 3a878416f432a4c9e38b0299afb6204fd2202168646d028298d40ab2c6cc7746
3
+ metadata.gz: 7a491b04b5c5a74e3addcdf2c8fb7712fddaf8f9966162f87f899166e05d08a0
4
+ data.tar.gz: eb4fe8a112d256d709a11792fd72d90b96f6bad05e2b814b99b8e7347b803f6f
5
5
  SHA512:
6
- metadata.gz: 8a2e0c5bd321f495f4cf2c5d69a8caaff889e352305c5a900c1c7415e8637d0127729c91bd030353c1dd3b56e9c5623880f46849d3ff3eac1d502c3df8752c54
7
- data.tar.gz: 0076c02f4070de277e488278f9f858d529b0ab39d8f8c07bc59a9d583c40421f0231b769a7ffb6a6ad1b422ed17f22ee4cd2a7eb68e436dc3b2b6c70c0246c8e
6
+ metadata.gz: 2fd77d2dd0403cbb6437d321eda9d0fb405a8efb7faec3cbfdc6d348c80de00064628d36ae50ded029cfe99d7b317f21a9a044af09174ead2350f71ccdc195c8
7
+ data.tar.gz: 6bf9879a1cf6d439f830394ca8cfb566e5b7145b6fc255e8b89be9fe57d6b89e33e7eaa2cf79d78a29a99ab8f8a8ebacf2f1219de3ed2ba12430b1c0f064480a
@@ -9,7 +9,7 @@ jobs:
9
9
 
10
10
  steps:
11
11
  - name: bump version
12
- uses: simplybusiness/dobby@v2.0.0
12
+ uses: simplybusiness/dobby@v2.1.0
13
13
  env:
14
14
  DOBBY_APP_ID: ${{ secrets.DOBBY_APP_ID }}
15
15
  DOBBY_PRIVATE_KEY: ${{ secrets.DOBBY_PRIVATE_KEY }}
@@ -10,7 +10,7 @@ jobs:
10
10
  runs-on: ubuntu-18.04
11
11
 
12
12
  steps:
13
- - uses: simplybusiness/version-forget-me-not@v2
13
+ - uses: simplybusiness/version-forget-me-not@v2.1.0
14
14
  env:
15
15
  ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
16
16
  VERSION_FILE_PATH: "lib/twiglet/version.rb"
data/README.md CHANGED
@@ -144,7 +144,72 @@ logger.formatter
144
144
  Take a look at this sample [Rack application](examples/rack/example_rack_app.rb#L15) with an ECS compliant
145
145
  [request logger](/examples/rack/request_logger.rb) as a template when configuring your own request logging middleware with Twiglet.
146
146
 
147
- ## Use of dotted keys (DEPRECATED)
147
+ ### Log format validation
148
+ Twiglet allows for the configuration of a custom validation schema. The validation schema must be [JSON Schema](https://json-schema.org/) compliant. Any fields not explicitly included in the provided schema are permitted by default.
149
+
150
+ For example, given the following JSON Schema:
151
+ ```ruby
152
+ validation_schema = <<-JSON
153
+ {
154
+ "type": "object",
155
+ "required": ["pet"],
156
+ "properties": {
157
+ "pet": {
158
+ "type": "object",
159
+ "required": ["name", "best_boy_or_girl?"],
160
+ "properties": {
161
+ "name": {
162
+ "type": "string",
163
+ "minLength": 1
164
+ },
165
+ "good_boy?": {
166
+ "type": "boolean"
167
+ }
168
+ }
169
+ }
170
+ }
171
+ }
172
+ JSON
173
+ ```
174
+
175
+ The logger can be instantiated with the custom schema
176
+ ```ruby
177
+ custom_logger = Twiglet::Logger.new('service name', validation_schema: validation_schema)
178
+ ```
179
+
180
+ Compliant log messages will log as normal.
181
+ ```ruby
182
+ # this is compliant
183
+ custom_logger.debug(pet: { name: 'Davis', good_boy?: true })
184
+
185
+ # the result
186
+ {:ecs=>{:version=>"1.5.0"}, :@timestamp=>"2020-05-11T15:01:01.000Z", :service=>{:name=>"petshop"}, :log=>{:level=>"debug"}, :pet=>{:name=>"Davis", :good_boy?=>true}}
187
+ ```
188
+
189
+ Non compliant messages will raise an error.
190
+ ```ruby
191
+ begin
192
+ custom_logger.debug(pet: { name: 'Davis' })
193
+ rescue JSON::Schema::ValidationError
194
+ # we forgot to specify that he's a good boy!
195
+ puts 'uh-oh'
196
+ end
197
+ ```
198
+
199
+ #### Customizing error responses
200
+ Depending on the application, it may not be desirable for the logger to raise Runtime errors. Twiglet allows you to configure a custom response for handling validation errors.
201
+
202
+ Configure error handling by writing a block
203
+ ```ruby
204
+ logger.configure_validation_error_response do |error|
205
+ # validation error handling goes here
206
+ # for example:
207
+ {YOUR APPLICATION BUG TRACKING SERVICE}.notify_error(error)
208
+ end
209
+
210
+ ```
211
+
212
+ ### Use of dotted keys (DEPRECATED)
148
213
 
149
214
  Writing nested json objects could be confusing. This library has a built-in feature to convert dotted keys into nested objects, so if you log like this:
150
215
 
@@ -65,12 +65,12 @@ describe RequestLogger do
65
65
  end
66
66
 
67
67
  it 'logs an error message when a request is bad' do
68
- -> { bad_request.get("/some/path") }.must_raise StandardError
68
+ expect { bad_request.get("/some/path") }.must_raise StandardError
69
69
  log = JSON.parse(output.string)
70
70
  assert_equal log['log']['level'], 'error'
71
71
  assert_equal log['error']['message'], 'some exception'
72
72
  assert_equal log['error']['type'], 'StandardError'
73
- assert_includes log['error']['stack_trace'], 'request_logger_test.rb'
73
+ assert_includes log['error']['stack_trace'].first, 'examples/rack/request_logger_test.rb'
74
74
  end
75
75
  end
76
76
 
@@ -24,6 +24,6 @@ module HashExtensions
24
24
  key.to_s
25
25
  .split('.')
26
26
  .reverse
27
- .reduce(val) { |nested, key_part| Hash[key_part.to_sym, nested] }
27
+ .reduce(val) { |nested, key_part| { key_part.to_sym => nested } }
28
28
  end
29
29
  end
@@ -21,7 +21,7 @@ module Twiglet
21
21
 
22
22
  now = args.fetch(:now, -> { Time.now.utc })
23
23
  output = args.fetch(:output, $stdout)
24
- level = args.fetch(:level, Logger::DEBUG)
24
+ level = args.fetch(:level, DEBUG)
25
25
  validation_schema = args.fetch(:validation_schema, File.read("#{__dir__}/validation_schema.json"))
26
26
 
27
27
  raise 'Service name is mandatory' \
@@ -70,7 +70,7 @@ module Twiglet
70
70
  private
71
71
 
72
72
  def add_stack_trace(hash_to_add_to, error)
73
- hash_to_add_to[:error][:stack_trace] = error.backtrace.join("\n") if error.backtrace
73
+ hash_to_add_to[:error][:stack_trace] = error.backtrace if error.backtrace
74
74
  end
75
75
  end
76
76
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Twiglet
4
- VERSION = '3.2.0'
4
+ VERSION = '3.3.0'
5
5
  end
data/test/logger_test.rb CHANGED
@@ -217,7 +217,7 @@ describe Twiglet::Logger do
217
217
  assert_equal 'Artificially raised exception', actual_log[:message]
218
218
  assert_equal 'divided by 0', actual_log[:error][:message]
219
219
  assert_equal 'ZeroDivisionError', actual_log[:error][:type]
220
- assert_match 'logger_test.rb', actual_log[:error][:stack_trace].lines.first
220
+ assert_match 'test/logger_test.rb', actual_log[:error][:stack_trace].first
221
221
  end
222
222
 
223
223
  it 'should log an error without backtrace' do
@@ -357,6 +357,17 @@ describe Twiglet::Logger do
357
357
  it 'initializes the logger with the provided level' do
358
358
  assert_equal Logger::WARN, Twiglet::Logger.new('petshop', level: :warn).level
359
359
  end
360
+
361
+ it 'does not log lower level' do
362
+ logger = Twiglet::Logger.new(
363
+ 'petshop',
364
+ now: @now,
365
+ output: @buffer,
366
+ level: Logger::INFO
367
+ )
368
+ logger.debug({ name: 'Davis', best_boy_or_girl?: true, species: 'dog' })
369
+ assert_empty @buffer.read
370
+ end
360
371
  end
361
372
 
362
373
  describe 'configuring error response' 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.2.0
4
+ version: 3.3.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: 2021-02-16 00:00:00.000000000 Z
11
+ date: 2021-06-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json-schema
@@ -141,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
141
141
  - !ruby/object:Gem::Version
142
142
  version: '0'
143
143
  requirements: []
144
- rubygems_version: 3.2.3
144
+ rubygems_version: 3.2.15
145
145
  signing_key:
146
146
  specification_version: 4
147
147
  summary: Twiglet