twiglet 2.3.9 → 2.3.10
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 +4 -4
- data/README.md +17 -5
- data/lib/twiglet/logger.rb +2 -2
- data/lib/twiglet/version.rb +1 -1
- data/test/logger_test.rb +10 -0
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 113edbe5053c76bc7490f90c4e3d85e68077547d26d9dc4e6e98ea474cc5beea
|
|
4
|
+
data.tar.gz: 94b8925876c5cc095db0f5a8380701ebd9e85d22dbca3bf2aa91cd46012920db
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 38f61f947ee52bfd29a0990d9dd2624ae68f628e66ecb2e67569499e3d7a81459f66d0316c9edd4ec45b2d514707474a91c0d6bd2c7661646d21ad0322a40f33
|
|
7
|
+
data.tar.gz: 20afdfefcd5b37363eca9797472bf1fb9f0947af68b53e994786671a69c2d26acce667351794c2e5540de786115c95cec34b8d8a52d16a206128a88382d71ff2
|
data/README.md
CHANGED
|
@@ -42,9 +42,10 @@ This will write to STDOUT a JSON string:
|
|
|
42
42
|
|
|
43
43
|
Obviously the timestamp will be different.
|
|
44
44
|
|
|
45
|
-
Alternatively, if you just want to log some error
|
|
45
|
+
Alternatively, if you just want to log some error string:
|
|
46
|
+
|
|
46
47
|
```ruby
|
|
47
|
-
logger.error(
|
|
48
|
+
logger.error("Emergency! There's an Emergency going on")
|
|
48
49
|
```
|
|
49
50
|
|
|
50
51
|
This will write to STDOUT a JSON string:
|
|
@@ -53,11 +54,22 @@ This will write to STDOUT a JSON string:
|
|
|
53
54
|
{"service":{"name":"service name"},"@timestamp":"2020-05-14T10:54:59.164+01:00","log":{"level":"error"}, "message":"Emergency! There's an Emergency going on"}
|
|
54
55
|
```
|
|
55
56
|
|
|
56
|
-
|
|
57
|
+
A message is always required unless a block is provided. The message can be an object or a string.
|
|
58
|
+
|
|
59
|
+
An optional error can also be provided, in which case the error message and backtrace will be logged in the relevant ECS compliant fields:
|
|
57
60
|
|
|
58
61
|
```ruby
|
|
59
62
|
db_err = StandardError.new('Connection timed-out')
|
|
60
63
|
logger.error({ message: 'DB connection failed.' }, db_err)
|
|
64
|
+
|
|
65
|
+
# this is also valid
|
|
66
|
+
logger.error('DB connection failed.', db_err)
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
These will both result in the same JSON string written to STDOUT:
|
|
70
|
+
|
|
71
|
+
```json
|
|
72
|
+
{"ecs":{"version":"1.5.0"},"@timestamp":"2020-08-21T15:44:37.890Z","service":{"name":"service name"},"log":{"level":"error"},"message":"DB connection failed.","error":{"message":"Connection timed-out"}}
|
|
61
73
|
```
|
|
62
74
|
|
|
63
75
|
Add log event specific information simply as attributes in a hash:
|
|
@@ -81,18 +93,18 @@ This writes:
|
|
|
81
93
|
{"service":{"name":"service name"},"@timestamp":"2020-05-14T10:56:49.527+01:00","log":{"level":"info"},"event":{"action":"HTTP request"},"message":"GET /pets success","trace":{"id":"1c8a5fb2-fecd-44d8-92a4-449eb2ce4dcb"},"http":{"request":{"method":"get"},"response":{"status_code":200}},"url":{"path":"/pets"}}
|
|
82
94
|
```
|
|
83
95
|
|
|
84
|
-
Similar to error you can use
|
|
96
|
+
Similar to error you can use string logging here as:
|
|
85
97
|
|
|
86
98
|
```
|
|
87
99
|
logger.info('GET /pets success')
|
|
88
100
|
```
|
|
101
|
+
|
|
89
102
|
This writes:
|
|
90
103
|
|
|
91
104
|
```json
|
|
92
105
|
{"service":{"name":"service name"},"@timestamp":"2020-05-14T10:56:49.527+01:00","log":{"level":"info"}}
|
|
93
106
|
```
|
|
94
107
|
|
|
95
|
-
|
|
96
108
|
It may be that when making a series of logs that write information about a single event, you may want to avoid duplication by creating an event specific logger that includes the context:
|
|
97
109
|
|
|
98
110
|
```ruby
|
data/lib/twiglet/logger.rb
CHANGED
|
@@ -29,7 +29,7 @@ module Twiglet
|
|
|
29
29
|
super(output, formatter: formatter, level: level)
|
|
30
30
|
end
|
|
31
31
|
|
|
32
|
-
def error(message =
|
|
32
|
+
def error(message = nil, error = nil, &block)
|
|
33
33
|
if error
|
|
34
34
|
error_fields = {
|
|
35
35
|
'error': {
|
|
@@ -37,7 +37,7 @@ module Twiglet
|
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
39
|
add_stack_trace(error_fields, error)
|
|
40
|
-
message.is_a?(Hash) ? message.merge
|
|
40
|
+
message = message.is_a?(Hash) ? message.merge(error_fields) : error_fields.merge(message: message)
|
|
41
41
|
end
|
|
42
42
|
|
|
43
43
|
super(message, &block)
|
data/lib/twiglet/version.rb
CHANGED
data/test/logger_test.rb
CHANGED
|
@@ -230,6 +230,16 @@ describe Twiglet::Logger do
|
|
|
230
230
|
refute actual_log[:error].key?(:stack_trace)
|
|
231
231
|
end
|
|
232
232
|
|
|
233
|
+
it 'should log an error with string message' do
|
|
234
|
+
e = StandardError.new('Unknown error')
|
|
235
|
+
@logger.error('Artificially raised exception with string message', e)
|
|
236
|
+
|
|
237
|
+
actual_log = read_json(@buffer)
|
|
238
|
+
|
|
239
|
+
assert_equal 'Artificially raised exception with string message', actual_log[:message]
|
|
240
|
+
assert_equal 'Unknown error', actual_log[:error][:message]
|
|
241
|
+
end
|
|
242
|
+
|
|
233
243
|
LEVELS.each do |attrs|
|
|
234
244
|
it "should correctly log level when calling #{attrs[:method]}" do
|
|
235
245
|
@logger.public_send(attrs[:method], {message: 'a log message'})
|
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: 2.3.
|
|
4
|
+
version: 2.3.10
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Simply Business
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2020-08-
|
|
11
|
+
date: 2020-08-26 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: Like a log, only smaller.
|
|
14
14
|
email:
|