ougai 2.0.0 → 2.1.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.
@@ -1,618 +0,0 @@
1
- require 'spec_helper'
2
- require 'stringio'
3
- require 'json'
4
-
5
- describe Ougai::ChildLogger do
6
- let(:pid) { Process.pid }
7
-
8
- matcher :be_log_message do |message, level|
9
- match do |actual|
10
- actual[:name] == 'rspec' \
11
- && actual[:msg] == message \
12
- && actual[:level] == level \
13
- && actual[:pid] == pid \
14
- && actual[:v] == 0
15
- end
16
- end
17
-
18
- let(:io) { StringIO.new }
19
- let(:parent_logger) { Ougai::Logger.new(io) }
20
-
21
- let(:items) do
22
- io.rewind
23
- io.readlines.map do |line|
24
- JSON.parse(line.chomp, symbolize_names: true)
25
- end
26
- end
27
-
28
- let(:item) {
29
- items[0]
30
- }
31
-
32
- describe '#level' do
33
- let(:logger) { parent_logger.child }
34
- let(:log_msg) { 'log message' }
35
-
36
- shared_examples 'trace logging' do
37
- it 'outputs trace message' do
38
- logger.trace(log_msg)
39
- expect(item).to be_log_message(log_msg, 10)
40
- end
41
-
42
- it 'outputs debug message' do
43
- logger.debug(log_msg)
44
- expect(item).to be_log_message(log_msg, 20)
45
- end
46
-
47
- it 'is consistent with the methods severity allows' do
48
- expect(logger.trace?).to be_truthy
49
- expect(logger.debug?).to be_truthy
50
- expect(logger.info?).to be_truthy
51
- expect(logger.warn?).to be_truthy
52
- expect(logger.error?).to be_truthy
53
- expect(logger.fatal?).to be_truthy
54
- end
55
- end
56
-
57
- shared_examples 'debug logging' do
58
- it 'does not output trace message' do
59
- logger.trace(log_msg)
60
- expect(item).to be_nil
61
- end
62
-
63
- it 'outputs debug message' do
64
- logger.debug(log_msg)
65
- expect(item).to be_log_message(log_msg, 20)
66
- end
67
-
68
- it 'outputs info message' do
69
- logger.info(log_msg)
70
- expect(item).to be_log_message(log_msg, 30)
71
- end
72
-
73
- it 'is consistent with the methods severity allows' do
74
- expect(logger.trace?).to be_falsey
75
- expect(logger.debug?).to be_truthy
76
- expect(logger.info?).to be_truthy
77
- expect(logger.warn?).to be_truthy
78
- expect(logger.error?).to be_truthy
79
- expect(logger.fatal?).to be_truthy
80
- end
81
- end
82
-
83
- shared_examples 'info logging' do
84
- it 'does not output debug message' do
85
- logger.debug(log_msg)
86
- expect(item).to be_nil
87
- end
88
-
89
- it 'outputs info message' do
90
- logger.info(log_msg)
91
- expect(item).to be_log_message(log_msg, 30)
92
- end
93
-
94
- it 'outputs warning message' do
95
- logger.warn(log_msg)
96
- expect(item).to be_log_message(log_msg, 40)
97
- end
98
-
99
- it 'is consistent with the methods severity allows' do
100
- expect(logger.trace?).to be_falsey
101
- expect(logger.debug?).to be_falsey
102
- expect(logger.info?).to be_truthy
103
- expect(logger.warn?).to be_truthy
104
- expect(logger.error?).to be_truthy
105
- expect(logger.fatal?).to be_truthy
106
- end
107
- end
108
-
109
- shared_examples 'warn logging' do
110
- it 'does not output info message' do
111
- logger.info(log_msg)
112
- expect(item).to be_nil
113
- end
114
-
115
- it 'outputs warning message' do
116
- logger.warn(log_msg)
117
- expect(item).to be_log_message(log_msg, 40)
118
- end
119
-
120
- it 'outputs error message' do
121
- logger.error(log_msg)
122
- expect(item).to be_log_message(log_msg, 50)
123
- end
124
-
125
- it 'is consistent with the methods severity allows' do
126
- expect(logger.trace?).to be_falsey
127
- expect(logger.debug?).to be_falsey
128
- expect(logger.info?).to be_falsey
129
- expect(logger.warn?).to be_truthy
130
- expect(logger.error?).to be_truthy
131
- expect(logger.fatal?).to be_truthy
132
- end
133
- end
134
-
135
- shared_examples 'error logging' do
136
- it 'does not output warning message' do
137
- logger.warn(log_msg)
138
- expect(item).to be_nil
139
- end
140
-
141
- it 'outputs error message' do
142
- logger.error(log_msg)
143
- expect(item).to be_log_message(log_msg, 50)
144
- end
145
-
146
- it 'outputs fatal message' do
147
- logger.fatal(log_msg)
148
- expect(item).to be_log_message(log_msg, 60)
149
- end
150
-
151
- it 'is consistent with the methods severity allows' do
152
- expect(logger.trace?).to be_falsey
153
- expect(logger.debug?).to be_falsey
154
- expect(logger.info?).to be_falsey
155
- expect(logger.warn?).to be_falsey
156
- expect(logger.error?).to be_truthy
157
- expect(logger.fatal?).to be_truthy
158
- end
159
- end
160
-
161
- shared_examples 'fatal logging' do
162
- it 'does not output error message' do
163
- logger.error(log_msg)
164
- expect(item).to be_nil
165
- end
166
-
167
- it 'outputs fatal message' do
168
- logger.fatal(log_msg)
169
- expect(item).to be_log_message(log_msg, 60)
170
- end
171
-
172
- it 'outputs unknown message' do
173
- logger.unknown(log_msg)
174
- expect(item).to be_log_message(log_msg, 70)
175
- end
176
-
177
- it 'is consistent with the methods severity allows' do
178
- expect(logger.trace?).to be_falsey
179
- expect(logger.debug?).to be_falsey
180
- expect(logger.info?).to be_falsey
181
- expect(logger.warn?).to be_falsey
182
- expect(logger.error?).to be_falsey
183
- expect(logger.fatal?).to be_truthy
184
- end
185
- end
186
-
187
- shared_examples 'unknown logging' do
188
- it 'does not output fatal message' do
189
- logger.fatal(log_msg)
190
- expect(item).to be_nil
191
- end
192
-
193
- it 'outputs unknown message' do
194
- logger.unknown(log_msg)
195
- expect(item).to be_log_message(log_msg, 70)
196
- end
197
-
198
- it 'is consistent with the methods severity allows' do
199
- expect(logger.trace?).to be_falsey
200
- expect(logger.debug?).to be_falsey
201
- expect(logger.info?).to be_falsey
202
- expect(logger.warn?).to be_falsey
203
- expect(logger.error?).to be_falsey
204
- expect(logger.fatal?).to be_falsey
205
- end
206
- end
207
-
208
- context 'TRACE the same level as parent' do
209
- it_behaves_like 'trace logging' do
210
- before do
211
- parent_logger.level = Ougai::Logger::TRACE
212
- logger.level = Ougai::Logger::TRACE
213
- end
214
- end
215
- end
216
-
217
- context 'DEBUG above parent level' do
218
- it_behaves_like 'debug logging' do
219
- before do
220
- parent_logger.level = Ougai::Logger::TRACE
221
- logger.level = Ougai::Logger::DEBUG
222
- end
223
- end
224
- end
225
-
226
- context 'INFO above parent level' do
227
- it_behaves_like 'info logging' do
228
- before do
229
- parent_logger.level = Ougai::Logger::DEBUG
230
- logger.level = Ougai::Logger::INFO
231
- end
232
- end
233
- end
234
-
235
- context 'WARN above parent level' do
236
- it_behaves_like 'warn logging' do
237
- before do
238
- parent_logger.level = Ougai::Logger::INFO
239
- logger.level = Ougai::Logger::WARN
240
- end
241
- end
242
- end
243
-
244
- context 'ERROR above parent level' do
245
- it_behaves_like 'error logging' do
246
- before do
247
- parent_logger.level = Ougai::Logger::WARN
248
- logger.level = Ougai::Logger::ERROR
249
- end
250
- end
251
- end
252
-
253
- context 'FATAL above parent level' do
254
- it_behaves_like 'fatal logging' do
255
- before do
256
- parent_logger.level = Ougai::Logger::ERROR
257
- logger.level = Ougai::Logger::FATAL
258
- end
259
- end
260
- end
261
-
262
- context 'UNKNOWN the same level as parent' do
263
- it_behaves_like 'unknown logging' do
264
- before do
265
- parent_logger.level = Ougai::Logger::UNKNOWN
266
- logger.level = Ougai::Logger::UNKNOWN
267
- end
268
- end
269
- end
270
-
271
- context 'propagated from parent TRACE' do
272
- it_behaves_like 'trace logging' do
273
- before do
274
- parent_logger.level = Ougai::Logger::TRACE
275
- end
276
- end
277
- end
278
-
279
- context 'propagated from parent DEBUG' do
280
- it_behaves_like 'debug logging' do
281
- before do
282
- parent_logger.level = Ougai::Logger::DEBUG
283
- end
284
- end
285
- end
286
-
287
- context 'propagated from parent INFO' do
288
- it_behaves_like 'info logging' do
289
- before do
290
- parent_logger.level = Ougai::Logger::INFO
291
- end
292
- end
293
- end
294
-
295
- context 'propagated from parent WARN' do
296
- it_behaves_like 'warn logging' do
297
- before do
298
- parent_logger.level = Ougai::Logger::WARN
299
- end
300
- end
301
- end
302
-
303
- context 'propagated from parent ERROR' do
304
- it_behaves_like 'error logging' do
305
- before do
306
- parent_logger.level = Ougai::Logger::ERROR
307
- end
308
- end
309
- end
310
-
311
- context 'propagated from parent FATAL' do
312
- it_behaves_like 'fatal logging' do
313
- before do
314
- parent_logger.level = Ougai::Logger::FATAL
315
- end
316
- end
317
- end
318
-
319
- context 'propagated from parent UNKNOWN' do
320
- it_behaves_like 'unknown logging' do
321
- before do
322
- parent_logger.level = Ougai::Logger::UNKNOWN
323
- end
324
- end
325
- end
326
-
327
- context 'set a level once, set nil' do
328
- before do
329
- parent_logger.level = Ougai::Logger::WARN
330
- logger.level = Ougai::Logger::INFO
331
- end
332
-
333
- it 'propagates from parent level' do
334
- expect(logger.level).to eq Ougai::Logger::INFO
335
- logger.level = nil
336
- expect(logger.level).to eq Ougai::Logger::WARN
337
- end
338
- end
339
-
340
- context 'set wrong name level' do
341
- it 'throws ArgumentErrror' do
342
- expect { logger.level = :wrong_level }.to raise_error(ArgumentError)
343
- end
344
- end
345
- end
346
-
347
- describe '#sev_threshold' do
348
- let(:logger) { parent_logger.child }
349
-
350
- it 'is the alias of level' do
351
- logger.sev_threshold = Ougai::Logger::INFO
352
- expect(logger.sev_threshold).to eq Ougai::Logger::INFO
353
- expect(logger.level).to eq Ougai::Logger::INFO
354
-
355
- logger.level = :trace
356
- expect(logger.sev_threshold).to eq Ougai::Logger::TRACE
357
- expect(logger.level).to eq Ougai::Logger::TRACE
358
-
359
- logger.sev_threshold = 'unknown'
360
- expect(logger.sev_threshold).to eq Ougai::Logger::UNKNOWN
361
- expect(logger.level).to eq Ougai::Logger::UNKNOWN
362
- end
363
- end
364
-
365
- describe '#chain' do
366
- let(:log_level) { 30 }
367
- let(:log_msg) { 'log message' }
368
- let(:parent_log_msg) { 'parent log message' }
369
-
370
- context 'parent with fields, child with fields' do
371
- before do
372
- parent_logger.with_fields = { foo: 1, pos: 'parent' }
373
- end
374
-
375
- let(:logger) { parent_logger.child(bar: '1', pos: 'child') }
376
-
377
- it 'outputs with merged parent and child fields' do
378
- logger.info(log_msg)
379
- parent_logger.info(parent_log_msg)
380
-
381
- expect(items[0]).to be_log_message(log_msg, log_level)
382
- expect(items[0]).to include(foo: 1, bar: '1', pos: 'child')
383
- expect(items[1]).to be_log_message(parent_log_msg, log_level)
384
- expect(items[1]).to include(foo: 1, pos: 'parent')
385
- expect(items[1]).not_to include(:bar)
386
- end
387
-
388
- context 'after updating with_fieldses of parent and child' do
389
- before do
390
- parent_logger.with_fields = { foo: 11 }
391
- logger.with_fields = { bar: '11' }
392
- end
393
-
394
- it 'outputs with child fields' do
395
- logger.info(log_msg)
396
- parent_logger.info(parent_log_msg)
397
-
398
- expect(items[0]).to be_log_message(log_msg, log_level)
399
- expect(items[0]).to include(foo: 11, bar: '11')
400
- expect(items[0]).not_to include(:pos)
401
- expect(items[1]).to be_log_message(parent_log_msg, log_level)
402
- expect(items[1]).to include(foo: 11)
403
- expect(items[1]).not_to include(:bar, :pos)
404
- end
405
- end
406
- end
407
-
408
- context 'parent with fields, child without fields' do
409
- before do
410
- parent_logger.with_fields = { foo: 2, pos: 'parent' }
411
- end
412
-
413
- let(:logger) { parent_logger.child }
414
-
415
- it 'output valid' do
416
- logger.info(log_msg)
417
- parent_logger.info(parent_log_msg)
418
-
419
- expect(items[0]).to be_log_message(log_msg, log_level)
420
- expect(items[0]).to include(foo: 2, pos: 'parent')
421
- expect(items[1]).to be_log_message(parent_log_msg, log_level)
422
- expect(items[1]).to include(foo: 2, pos: 'parent')
423
- end
424
-
425
- context 'after updating parent logger with_fields' do
426
- before do
427
- parent_logger.with_fields = { foo: 22 }
428
- end
429
-
430
- it 'output with new parent fields' do
431
- logger.info(log_msg)
432
- parent_logger.info(parent_log_msg)
433
-
434
- expect(items[0]).to be_log_message(log_msg, log_level)
435
- expect(items[0]).to include(foo: 22)
436
- expect(items[0]).not_to include(:pos)
437
- expect(items[1]).to be_log_message(parent_log_msg, log_level)
438
- expect(items[1]).to include(foo: 22)
439
- expect(items[1]).not_to include(:pos)
440
- end
441
- end
442
- end
443
-
444
- context 'parent without fields, child with fields' do
445
- before do
446
- parent_logger.with_fields = {}
447
- end
448
-
449
- let(:logger) { parent_logger.child(bar: '3', pos: 'child') }
450
-
451
- it 'output valid' do
452
- logger.info(log_msg)
453
- parent_logger.info(parent_log_msg)
454
-
455
- expect(items[0]).to be_log_message(log_msg, log_level)
456
- expect(items[0]).to include(bar: '3', pos: 'child')
457
- expect(items[1]).to be_log_message(parent_log_msg, log_level)
458
- expect(items[1]).not_to include(:bar, :pos)
459
- end
460
-
461
- context 'after updating child logger with_fields' do
462
- before do
463
- logger.with_fields = { bar: '33' }
464
- end
465
-
466
- it 'output valid' do
467
- logger.info(log_msg)
468
- parent_logger.info(parent_log_msg)
469
-
470
- expect(items[0]).to be_log_message(log_msg, log_level)
471
- expect(items[0]).to include(bar: '33')
472
- expect(items[0]).not_to include(:pos)
473
- expect(items[1]).to be_log_message(parent_log_msg, log_level)
474
- expect(items[1]).not_to include(:bar, :pos)
475
- end
476
- end
477
- end
478
-
479
- context 'grandchild logger' do
480
- before do
481
- parent_logger.with_fields = { tag: 'parent', tags: ['parent'], event: { module: 'core' } }
482
- end
483
-
484
- let(:logger) { parent_logger.child(tag: 'child', tags: ['child'], event: { dataset: 'core.child' }) }
485
- let(:grand_logger) { logger.child(tag: 'grandchild', tags: ['grandchild'], event: { action: 'log-action' }) }
486
-
487
- it 'outputs with all merged fields' do
488
- grand_logger.info('Hi', foo: 3)
489
- logger.info(log_msg, foo: 2)
490
- parent_logger.info(parent_log_msg, foo: 10, event: { module: 'service' })
491
- parent_logger.info('Good evening!', foo: 11, event: { duration: 150 })
492
-
493
- expect(items[0]).to be_log_message('Hi', log_level)
494
- expect(items[0]).to include(
495
- tag: 'grandchild',
496
- tags: ['parent', 'child', 'grandchild'],
497
- foo: 3,
498
- event: { module: 'core', dataset: 'core.child', action: 'log-action' }
499
- )
500
-
501
- expect(items[1]).to be_log_message(log_msg, log_level)
502
- expect(items[1]).to include(
503
- tag: 'child',
504
- tags: ['parent', 'child'],
505
- foo: 2,
506
- event: { module: 'core', dataset: 'core.child' }
507
- )
508
-
509
- expect(items[2]).to be_log_message(parent_log_msg, log_level)
510
- expect(items[2]).to include(tag: 'parent', tags: ['parent'], foo: 10, event: { module: 'service' })
511
- expect(items[3]).to be_log_message('Good evening!', log_level)
512
- expect(items[3]).to include(tag: 'parent', tags: ['parent'], foo: 11, event: { module: 'core', duration: 150 })
513
- end
514
-
515
- context 'after updating child logger with_fields' do
516
- before do
517
- logger.with_fields = { bar: '33' }
518
- end
519
-
520
- it 'outputs with child fields' do
521
- logger.info(log_msg)
522
- expect(items[0]).to be_log_message(log_msg, log_level)
523
- expect(items[0]).to include(bar: '33')
524
- expect(items[0]).not_to include(:pos)
525
- end
526
- end
527
- end
528
- end
529
-
530
- describe '#before_log' do
531
- let(:logger) { parent_logger.child }
532
- let(:log_msg) { 'before_log test' }
533
-
534
- before do
535
- parent_logger.level = Logger::INFO
536
- end
537
-
538
- context 'child logger to be set before_log' do
539
- before do
540
- logger.before_log = lambda do |data|
541
- data[:context_id] = 123
542
- end
543
- end
544
-
545
- it 'outputs the field to be added in before_log' do
546
- logger.info(log_msg)
547
- expect(item).to be_log_message(log_msg, 30)
548
- expect(item).to include(context_id: 123)
549
- end
550
- end
551
-
552
- context 'parent logger to be set before_log' do
553
- before do
554
- parent_logger.before_log = lambda do |data|
555
- data[:context_id] = 12345
556
- end
557
- end
558
-
559
- it 'outputs the field to be added in before_log' do
560
- logger.info(log_msg)
561
- expect(item).to be_log_message(log_msg, 30)
562
- expect(item).to include(context_id: 12345)
563
- end
564
- end
565
-
566
- context 'both child logger and parent logger to be set before_log' do
567
- before do
568
- logger.before_log = lambda do |data|
569
- data[:context_id] = 67890
570
- data[:context_name] = 'sub'
571
- end
572
- parent_logger.before_log = lambda do |data|
573
- data[:context_id] = 12345
574
- end
575
- end
576
-
577
- it 'outputs the fields to be added in each before_log' do
578
- logger.info(log_msg)
579
- expect(item).to be_log_message(log_msg, 30)
580
- expect(item).to include(context_id: 12345) # parent
581
- expect(item).to include(context_name: 'sub') # child
582
- end
583
- end
584
- end
585
-
586
- describe '#child' do
587
- let!(:root) { double('root logger') }
588
-
589
- context 'when the class is original' do
590
- subject!(:org_instance) { described_class.new(root, {}) }
591
-
592
- it 'returns an instance of the same class' do
593
- expect(org_instance.child).to be_an_instance_of(described_class)
594
- end
595
- end
596
-
597
- context 'when the class is sub-class' do
598
- subject!(:sc_instance) { Class.new(described_class).new(root, {}) }
599
-
600
- it 'returns an instance of the child_class' do
601
- expect(sc_instance.child).to be_an_instance_of(sc_instance.class)
602
- end
603
- end
604
-
605
- context 'block is given' do
606
- let!(:fields) { double('fields') }
607
-
608
- subject { described_class.new(root, {}) }
609
-
610
- it 'yields child logger' do
611
- subject.child(fields) do |cl|
612
- expect(cl.instance_variable_get(:@parent)).to eq(subject)
613
- expect(cl.instance_variable_get(:@with_fields)).to eq(fields)
614
- end
615
- end
616
- end
617
- end
618
- end
@@ -1,98 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Ougai::Formatters::Base do
4
- subject { described_class.new(app_name, hostname) }
5
-
6
- context 'default' do
7
- let!(:app_name) { nil }
8
- let!(:hostname) { nil }
9
-
10
- it 'has datetime format default ISO8601' do
11
- expect(subject.datetime_format).to match(/^\%FT\%T\.\%3N(Z|\%\:z)$/)
12
- end
13
-
14
- it 'has datetime_format accessor' do
15
- subject.datetime_format = '%I:%M:%S %p'
16
- expect(subject.datetime_format).to eq('%I:%M:%S %p')
17
-
18
- # revert default format by to set nil
19
- subject.datetime_format = nil
20
- expect(subject.datetime_format).to match(/^\%FT\%T\.\%3N(Z|\%\:z)$/)
21
- end
22
- end
23
-
24
- context 'without arguments and hostname contains a UTF-8 char' do
25
- let!(:app_name) { nil }
26
- let!(:hostname) { nil }
27
-
28
- it 'has default app_name and default hostname' do
29
- myhostname = "Taro\xE2\x80\x99s-MacBook".force_encoding('ASCII-8BIT')
30
- allow(Socket).to receive(:gethostname).and_return(myhostname)
31
- expect(subject.app_name).to eq('rspec')
32
- expect(subject.hostname).to eq("Taro’s-MacBook")
33
- end
34
- end
35
-
36
- context 'with app_name' do
37
- let!(:app_name) { 'myapp' }
38
- let!(:hostname) { nil }
39
-
40
- it 'has specified app_name and default hostname' do
41
- myhostname = "Hanako's PC".encode('ASCII-8BIT')
42
- allow(Socket).to receive(:gethostname).and_return(myhostname)
43
- expect(subject.app_name).to eq('myapp')
44
- expect(subject.hostname).to eq("Hanako's PC")
45
- end
46
- end
47
-
48
- context 'with hostname' do
49
- let!(:app_name) { nil }
50
- let!(:hostname) { 'myhost' }
51
-
52
- it 'has default app_name and specified hostname' do
53
- expect(subject.app_name).to eq('rspec')
54
- expect(subject.hostname).to eq('myhost')
55
- end
56
- end
57
-
58
- context 'with app_name and hostname' do
59
- let!(:app_name) { 'myapp' }
60
- let!(:hostname) { 'myhost' }
61
-
62
- it 'has specified app_name and specified hostname' do
63
- expect(subject.app_name).to eq('myapp')
64
- expect(subject.hostname).to eq('myhost')
65
- end
66
- end
67
-
68
- describe '#serialize_exc' do
69
- let!(:app_name) { 'myapp' }
70
- let!(:hostname) { 'myhost' }
71
- let!(:errmsg) { 'dummy error' }
72
-
73
- it 'returning data with stack as String' do
74
- begin
75
- raise errmsg
76
- rescue => e
77
- result = subject.serialize_exc(e)
78
- end
79
- expect(result[:message]).to eq(errmsg)
80
- expect(result[:stack]).to be_instance_of(String)
81
- end
82
-
83
- context 'not serialize backtrace' do
84
- it 'returning data with stack as Array' do
85
- subject.serialize_backtrace = false
86
- subject.trace_max_lines = 6
87
- begin
88
- raise errmsg
89
- rescue => e
90
- result = subject.serialize_exc(e)
91
- end
92
- expect(result[:message]).to eq(errmsg)
93
- expect(result[:stack]).to be_instance_of(Array)
94
- expect(result[:stack].size).to eq(6)
95
- end
96
- end
97
- end
98
- end