ougai 1.2.0 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +22 -0
- data/lib/ougai/child_logger.rb +5 -3
- data/lib/ougai/logger.rb +11 -5
- data/lib/ougai/logging.rb +2 -1
- data/lib/ougai/version.rb +1 -1
- data/spec/child_logger_spec.rb +56 -0
- data/spec/logger_spec.rb +33 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0235a013e7e1d174d71adb866072ce73243cb8a8
|
4
|
+
data.tar.gz: ffde73e617129cf7287abfd53cbd5f151e166816
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: be64d5eb15dca49f85a323b2c3fac463d83a42dd3f5f0bca6d318ca27cd8646e74dd3e3d658d3248469ccf4e6320c39512a390a91ca92d8a46dde3ab1f7f4c3a
|
7
|
+
data.tar.gz: a634e9fa73595c01748390bb6043e220c8f0b65cc052202320cf6fa31ea4b3be0984c623171afc46ea72e227d3f32bceb770b3baf07d8f0421e54067ac2326f8
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -214,6 +214,28 @@ gc_logger.debug('something detail', age: 34, weight: 72)
|
|
214
214
|
|
215
215
|
If any field exists in both parent log and child log, the parent value is overridden or merged by child value.
|
216
216
|
|
217
|
+
### Hook before logging
|
218
|
+
|
219
|
+
Setting `before_log` of logger or child an *lambda* with `data` field, a process can be run before log each output.
|
220
|
+
|
221
|
+
* Adding variable data (like Thread ID) to logging data can be defined in common.
|
222
|
+
* Returning `false` in *lambda*, the log is cancelled and does not output.
|
223
|
+
* The *before_log* of child logger is run ahead of the parent logger's.
|
224
|
+
|
225
|
+
```ruby
|
226
|
+
logger.before_log = lambda do |data|
|
227
|
+
data[:thread_id] = Thread.current.object_id.to_s(36)
|
228
|
+
end
|
229
|
+
|
230
|
+
logger.debug('on main thread')
|
231
|
+
Thread.new { logger.debug('on another thread') }
|
232
|
+
```
|
233
|
+
|
234
|
+
```json
|
235
|
+
{"name":"main","hostname":"mint","pid":13975,"level":20,"time":"2017-08-06T15:35:53.435+09:00","v":0,"msg":"on main thread","thread_id":"gqe0ava6c"}
|
236
|
+
{"name":"main","hostname":"mint","pid":13975,"level":20,"time":"2017-08-06T15:35:53.435+09:00","v":0,"msg":"on another thread","thread_id":"gqe0cb14g"}
|
237
|
+
```
|
238
|
+
|
217
239
|
## View log by node-bunyan
|
218
240
|
|
219
241
|
Install [bunyan](https://github.com/trentm/node-bunyan) via NPM
|
data/lib/ougai/child_logger.rb
CHANGED
@@ -11,14 +11,16 @@ module Ougai
|
|
11
11
|
@parent.level
|
12
12
|
end
|
13
13
|
|
14
|
-
def chain(severity, args, fields)
|
15
|
-
|
14
|
+
def chain(severity, args, fields, hooks)
|
15
|
+
hooks.push(@before_log) if @before_log
|
16
|
+
@parent.chain(severity, args, merge_fields(@with_fields, fields), hooks)
|
16
17
|
end
|
17
18
|
|
18
19
|
protected
|
19
20
|
|
20
21
|
def append(severity, args)
|
21
|
-
@
|
22
|
+
hooks = @before_log ? [@before_log] : []
|
23
|
+
@parent.chain(severity, args, @with_fields, hooks)
|
22
24
|
end
|
23
25
|
end
|
24
26
|
end
|
data/lib/ougai/logger.rb
CHANGED
@@ -25,22 +25,28 @@ module Ougai
|
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
-
def chain(severity, args, fields)
|
29
|
-
|
28
|
+
def chain(severity, args, fields, hooks)
|
29
|
+
hooks.push(@before_log) if @before_log
|
30
|
+
write(severity, args, merge_fields(@with_fields, fields), hooks)
|
30
31
|
end
|
31
32
|
|
32
33
|
protected
|
33
34
|
|
34
35
|
def append(severity, args)
|
35
|
-
|
36
|
+
hooks = @before_log ? [@before_log] : []
|
37
|
+
write(severity, args, @with_fields, hooks)
|
36
38
|
end
|
37
39
|
|
38
40
|
def create_formatter
|
39
41
|
Formatters::Bunyan.new
|
40
42
|
end
|
41
43
|
|
42
|
-
def write(severity, args, fields)
|
43
|
-
|
44
|
+
def write(severity, args, fields, hooks)
|
45
|
+
data = merge_fields(fields, to_item(args))
|
46
|
+
hooks.each do |hook|
|
47
|
+
return false if hook.call(data) == false
|
48
|
+
end
|
49
|
+
add(severity, data)
|
44
50
|
end
|
45
51
|
|
46
52
|
def to_item(args)
|
data/lib/ougai/logging.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
module Ougai
|
2
2
|
module Logging
|
3
3
|
attr_accessor :with_fields
|
4
|
+
attr_writer :before_log
|
4
5
|
|
5
6
|
def debug(message = nil, ex = nil, data = nil, &block)
|
6
7
|
return true if level > Logger::DEBUG
|
@@ -41,7 +42,7 @@ module Ougai
|
|
41
42
|
ChildLogger.new(self, fields)
|
42
43
|
end
|
43
44
|
|
44
|
-
def chain(
|
45
|
+
def chain(_severity, _args, _fields, _hooks)
|
45
46
|
raise NotImplementedError
|
46
47
|
end
|
47
48
|
|
data/lib/ougai/version.rb
CHANGED
data/spec/child_logger_spec.rb
CHANGED
@@ -297,4 +297,60 @@ describe Ougai::ChildLogger do
|
|
297
297
|
end
|
298
298
|
end
|
299
299
|
end
|
300
|
+
|
301
|
+
describe '#before_log' do
|
302
|
+
let(:logger) { parent_logger.child }
|
303
|
+
let(:log_msg) { 'before_log test' }
|
304
|
+
|
305
|
+
before do
|
306
|
+
parent_logger.level = Logger::INFO
|
307
|
+
end
|
308
|
+
|
309
|
+
context 'child logger to be set before_log' do
|
310
|
+
before do
|
311
|
+
logger.before_log = lambda do |data|
|
312
|
+
data[:context_id] = 123
|
313
|
+
end
|
314
|
+
end
|
315
|
+
|
316
|
+
it 'outputs the field to be added in before_log' do
|
317
|
+
logger.info(log_msg)
|
318
|
+
expect(item).to be_log_message(log_msg, 30)
|
319
|
+
expect(item).to include(context_id: 123)
|
320
|
+
end
|
321
|
+
end
|
322
|
+
|
323
|
+
context 'parent logger to be set before_log' do
|
324
|
+
before do
|
325
|
+
parent_logger.before_log = lambda do |data|
|
326
|
+
data[:context_id] = 12345
|
327
|
+
end
|
328
|
+
end
|
329
|
+
|
330
|
+
it 'outputs the field to be added in before_log' do
|
331
|
+
logger.info(log_msg)
|
332
|
+
expect(item).to be_log_message(log_msg, 30)
|
333
|
+
expect(item).to include(context_id: 12345)
|
334
|
+
end
|
335
|
+
end
|
336
|
+
|
337
|
+
context 'both child logger and parent logger to be set before_log' do
|
338
|
+
before do
|
339
|
+
logger.before_log = lambda do |data|
|
340
|
+
data[:context_id] = 67890
|
341
|
+
data[:context_name] = 'sub'
|
342
|
+
end
|
343
|
+
parent_logger.before_log = lambda do |data|
|
344
|
+
data[:context_id] = 12345
|
345
|
+
end
|
346
|
+
end
|
347
|
+
|
348
|
+
it 'outputs the fields to be added in each before_log' do
|
349
|
+
logger.info(log_msg)
|
350
|
+
expect(item).to be_log_message(log_msg, 30)
|
351
|
+
expect(item).to include(context_id: 12345) # parent
|
352
|
+
expect(item).to include(context_name: 'sub') # child
|
353
|
+
end
|
354
|
+
end
|
355
|
+
end
|
300
356
|
end
|
data/spec/logger_spec.rb
CHANGED
@@ -483,4 +483,37 @@ describe Ougai::Logger do
|
|
483
483
|
end
|
484
484
|
end
|
485
485
|
end
|
486
|
+
|
487
|
+
describe '#before_log' do
|
488
|
+
let(:log_msg) { 'before_log test' }
|
489
|
+
|
490
|
+
context 'set context data' do
|
491
|
+
before do
|
492
|
+
logger.level = Logger::INFO
|
493
|
+
logger.before_log = lambda do |data|
|
494
|
+
data[:context_id] = 123
|
495
|
+
end
|
496
|
+
end
|
497
|
+
|
498
|
+
it 'outputs with context data' do
|
499
|
+
logger.info(log_msg)
|
500
|
+
expect(item).to be_log_message(log_msg, 30)
|
501
|
+
expect(item).to include(context_id: 123)
|
502
|
+
end
|
503
|
+
end
|
504
|
+
|
505
|
+
context 'cancelling log' do
|
506
|
+
before do
|
507
|
+
logger.level = Logger::INFO
|
508
|
+
logger.before_log = lambda do |data|
|
509
|
+
false
|
510
|
+
end
|
511
|
+
end
|
512
|
+
|
513
|
+
it 'outputs none' do
|
514
|
+
logger.info(log_msg)
|
515
|
+
expect(item).to be_nil
|
516
|
+
end
|
517
|
+
end
|
518
|
+
end
|
486
519
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ougai
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Toshimitsu Takahashi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-08-
|
11
|
+
date: 2017-08-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|