twiglet 3.2.0 → 3.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/dobby-actions.yml +1 -1
- data/.github/workflows/version-forget-me-not.yml +1 -1
- data/README.md +66 -1
- data/examples/rack/request_logger_test.rb +2 -2
- data/lib/hash_extensions.rb +1 -1
- data/lib/twiglet/logger.rb +2 -2
- data/lib/twiglet/version.rb +1 -1
- data/test/logger_test.rb +12 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7a491b04b5c5a74e3addcdf2c8fb7712fddaf8f9966162f87f899166e05d08a0
|
4
|
+
data.tar.gz: eb4fe8a112d256d709a11792fd72d90b96f6bad05e2b814b99b8e7347b803f6f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2fd77d2dd0403cbb6437d321eda9d0fb405a8efb7faec3cbfdc6d348c80de00064628d36ae50ded029cfe99d7b317f21a9a044af09174ead2350f71ccdc195c8
|
7
|
+
data.tar.gz: 6bf9879a1cf6d439f830394ca8cfb566e5b7145b6fc255e8b89be9fe57d6b89e33e7eaa2cf79d78a29a99ab8f8a8ebacf2f1219de3ed2ba12430b1c0f064480a
|
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
|
-
|
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
|
-
|
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
|
|
data/lib/hash_extensions.rb
CHANGED
data/lib/twiglet/logger.rb
CHANGED
@@ -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,
|
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
|
73
|
+
hash_to_add_to[:error][:stack_trace] = error.backtrace if error.backtrace
|
74
74
|
end
|
75
75
|
end
|
76
76
|
end
|
data/lib/twiglet/version.rb
CHANGED
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].
|
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.
|
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-
|
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.
|
144
|
+
rubygems_version: 3.2.15
|
145
145
|
signing_key:
|
146
146
|
specification_version: 4
|
147
147
|
summary: Twiglet
|