ougai 1.7.1-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,688 @@
1
+ require 'spec_helper'
2
+ require 'stringio'
3
+ require 'json'
4
+
5
+ describe Ougai::Logger 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
+ matcher :include_data do |data|
19
+ match do |actual|
20
+ data.each do |key, expected|
21
+ return false unless actual[key] == expected
22
+ end
23
+ true
24
+ end
25
+ end
26
+
27
+ matcher :include_error do |expected|
28
+ match do |actual|
29
+ err = actual[:err]
30
+ err[:message] == expected \
31
+ && err[:name] = 'StandardError' \
32
+ && err[:stack].include?('<main>')
33
+ end
34
+ end
35
+
36
+ let(:io) { StringIO.new }
37
+ let(:logger) { described_class.new(io) }
38
+
39
+ let(:item) do
40
+ log_str = io.string
41
+ begin
42
+ JSON.parse(log_str, symbolize_names: true)
43
+ rescue Exception
44
+ nil
45
+ end
46
+ end
47
+
48
+ class Dummy
49
+ def to_hash
50
+ { foo: 1 }
51
+ end
52
+ end
53
+
54
+ shared_examples 'log' do
55
+ context 'with message' do
56
+ it 'outputs valid' do
57
+ logger.send(method, log_msg)
58
+ expect(item).to be_log_message(log_msg, log_level)
59
+ end
60
+
61
+ it 'outputs valid by block' do
62
+ logger.send(method) { log_msg }
63
+ expect(item).to be_log_message(log_msg, log_level)
64
+ end
65
+ end
66
+
67
+ context 'with exception' do
68
+ it 'outputs valid' do
69
+ begin
70
+ raise StandardError, 'errmsg'
71
+ rescue => ex
72
+ logger.send(method, ex)
73
+ end
74
+
75
+ expect(item).to be_log_message('errmsg', log_level)
76
+ expect(item).to include_error('errmsg')
77
+ end
78
+
79
+ it 'outputs valid by block' do
80
+ begin
81
+ raise StandardError, 'errmsg'
82
+ rescue => ex
83
+ logger.send(method) { ex }
84
+ end
85
+
86
+ expect(item).to be_log_message('errmsg', log_level)
87
+ expect(item).to include_error('errmsg')
88
+ end
89
+ end
90
+
91
+ context 'with data that contains msg' do
92
+ it 'outputs valid' do
93
+ logger.send(method, { msg: log_msg, data_id: 108, action: 'dump' })
94
+ expect(item).to be_log_message(log_msg, log_level)
95
+ expect(item).to include_data(data_id: 108, action: 'dump')
96
+ end
97
+
98
+ it 'outputs valid by block' do
99
+ logger.send(method) do
100
+ { msg: log_msg, data_id: 108, action: 'dump' }
101
+ end
102
+ expect(item).to be_log_message(log_msg, log_level)
103
+ expect(item).to include_data(data_id: 108, action: 'dump')
104
+ end
105
+
106
+ it 'outputs valid with fields' do
107
+ logger.with_fields = { coreField1: 123, core_field2: 'core', 'core_field3' => 456 }
108
+ logger.send(method, { msg: log_msg, data_id: 109, action: 'do' })
109
+ expect(item).to be_log_message(log_msg, log_level)
110
+ expect(item).to include_data(data_id: 109, action: 'do', coreField1: 123, core_field2: 'core', core_field3: 456)
111
+ end
112
+
113
+ it 'outputs valid with fields overridden' do
114
+ logger.with_fields = { core_field1: 'original', core_field2: 'original', err: 'original' }
115
+ logger.send(method, { msg: log_msg, data_id: 110, action: 'do', core_field1: 'override' })
116
+ expect(item).to be_log_message(log_msg, log_level)
117
+ expect(item).to include_data(data_id: 110, action: 'do', core_field1: 'override', core_field2: 'original', err: 'original')
118
+ end
119
+ end
120
+
121
+ context 'with data that does not contain msg' do
122
+ it 'outputs valid' do
123
+ logger.send(method, { data_id: 109, action: 'dump' })
124
+ expect(item).to be_log_message('No message', log_level)
125
+ expect(item).to include_data(data_id: 109, action: 'dump')
126
+ end
127
+
128
+ it 'outputs valid by block' do
129
+ logger.send(method) do
130
+ { data_id: 109, action: 'dump' }
131
+ end
132
+ expect(item).to be_log_message('No message', log_level)
133
+ expect(item).to include_data(data_id: 109, action: 'dump')
134
+ end
135
+ end
136
+
137
+ context 'with data that can respond to_hash' do
138
+ it 'outputs valid' do
139
+ logger.send(method, Dummy.new)
140
+ expect(item).to be_log_message('No message', log_level)
141
+ expect(item).to include_data(foo: 1)
142
+ end
143
+ end
144
+
145
+ context 'with data that cannot respond to_hash' do
146
+ it '(array) outputs valid' do
147
+ logger.send(method, ['bar', 2])
148
+ expect(item).to be_log_message('No message', log_level)
149
+ expect(item).to include_data(data: ['bar', 2])
150
+ end
151
+
152
+ it '(number) outputs valid' do
153
+ logger.send(method, 999)
154
+ expect(item).to be_log_message('No message', log_level)
155
+ expect(item).to include_data(data: 999)
156
+ end
157
+ end
158
+
159
+ context 'with message and data' do
160
+ it 'outputs valid' do
161
+ logger.send(method, log_msg, data_id: 99, action: 'insert')
162
+ expect(item).to be_log_message(log_msg, log_level)
163
+ expect(item).to include_data(data_id: 99, action: 'insert')
164
+ end
165
+
166
+ it 'outputs valid by block' do
167
+ logger.send(method) { [log_msg, data_id: 99, action: 'insert'] }
168
+ expect(item).to be_log_message(log_msg, log_level)
169
+ expect(item).to include_data(data_id: 99, action: 'insert')
170
+ end
171
+ end
172
+
173
+ context 'with message and exception' do
174
+ it 'outputs valid' do
175
+ begin
176
+ raise StandardError, 'errmsg'
177
+ rescue => ex
178
+ logger.send(method, log_msg, ex)
179
+ end
180
+
181
+ expect(item).to be_log_message(log_msg, log_level)
182
+ expect(item).to include_error('errmsg')
183
+ end
184
+
185
+ it 'outputs valid by block' do
186
+ begin
187
+ raise StandardError, 'errmsg'
188
+ rescue => ex
189
+ logger.send(method) { [log_msg, ex] }
190
+ end
191
+
192
+ expect(item).to be_log_message(log_msg, log_level)
193
+ expect(item).to include_error('errmsg')
194
+ end
195
+
196
+ it 'outputs valid overridden err field' do
197
+ logger.with_fields = { err: 'original' }
198
+ begin
199
+ raise StandardError, 'errmsg2'
200
+ rescue => ex
201
+ logger.send(method, log_msg, ex)
202
+ end
203
+
204
+ expect(item).to include_error('errmsg2')
205
+ end
206
+ end
207
+
208
+ context 'with exception and data' do
209
+ it 'outputs valid' do
210
+ begin
211
+ raise StandardError, 'errmsg'
212
+ rescue => ex
213
+ logger.send(method, ex, something: { name: 'bar' })
214
+ end
215
+
216
+ expect(item).to include_error('errmsg')
217
+ expect(item).to include_data(something: { name: 'bar' })
218
+ end
219
+
220
+ it 'outputs valid by block' do
221
+ begin
222
+ raise StandardError, 'errmsg'
223
+ rescue => ex
224
+ logger.send(method) do
225
+ [ex, something: { name: 'bar' }]
226
+ end
227
+ end
228
+
229
+ expect(item).to include_error('errmsg')
230
+ expect(item).to include_data(something: { name: 'bar' })
231
+ end
232
+ end
233
+
234
+ context 'with message, exception and data' do
235
+ it 'outputs valid' do
236
+ begin
237
+ raise StandardError, 'errmsg'
238
+ rescue => ex
239
+ logger.send(method, log_msg, ex, something: { name: 'foo' })
240
+ end
241
+
242
+ expect(item).to be_log_message(log_msg, log_level)
243
+ expect(item).to include_error('errmsg')
244
+ expect(item).to include_data(something: { name: 'foo' })
245
+ end
246
+
247
+ it 'outputs valid by block' do
248
+ begin
249
+ raise StandardError, 'errmsg'
250
+ rescue => ex
251
+ logger.send(method) do
252
+ [log_msg, ex, something: { name: 'foo' }]
253
+ end
254
+ end
255
+
256
+ expect(item).to be_log_message(log_msg, log_level)
257
+ expect(item).to include_error('errmsg')
258
+ expect(item).to include_data(something: { name: 'foo' })
259
+ end
260
+ end
261
+
262
+ context 'without arguments' do
263
+ it 'outputs only default message' do
264
+ logger.send(method)
265
+ expect(item).to be_log_message('No message', log_level)
266
+ end
267
+ end
268
+ end
269
+
270
+ describe '#trace' do
271
+ let(:log_level) { 10 }
272
+ let(:log_msg) { 'trace message' }
273
+ let(:method) { 'trace' }
274
+
275
+ before { logger.level = :trace }
276
+
277
+ it_behaves_like 'log'
278
+
279
+ it 'is consistent with the methods severity allows' do
280
+ expect(logger.trace?).to be_truthy
281
+ expect(logger.debug?).to be_truthy
282
+ expect(logger.info?).to be_truthy
283
+ expect(logger.warn?).to be_truthy
284
+ expect(logger.error?).to be_truthy
285
+ expect(logger.fatal?).to be_truthy
286
+ end
287
+ end
288
+
289
+ describe '#debug' do
290
+ let(:log_level) { 20 }
291
+ let(:log_msg) { 'debug message' }
292
+ let(:method) { 'debug' }
293
+
294
+ it_behaves_like 'log'
295
+
296
+ it 'is consistent with the methods severity allows' do
297
+ expect(logger.trace?).to be_falsey
298
+ expect(logger.debug?).to be_truthy
299
+ end
300
+ end
301
+
302
+ describe '#info' do
303
+ let(:log_level) { 30 }
304
+ let(:log_msg) { 'info message' }
305
+ let(:method) { 'info' }
306
+
307
+ it_behaves_like 'log'
308
+ end
309
+
310
+ describe '#warn' do
311
+ let(:log_level) { 40 }
312
+ let(:log_msg) { 'info message' }
313
+ let(:method) { 'warn' }
314
+
315
+ it_behaves_like 'log'
316
+ end
317
+
318
+ describe '#error' do
319
+ let(:log_level) { 50 }
320
+ let(:log_msg) { 'error message' }
321
+ let(:method) { 'error' }
322
+
323
+ it_behaves_like 'log'
324
+ end
325
+
326
+ describe '#fatal' do
327
+ let(:log_level) { 60 }
328
+ let(:log_msg) { 'fatal message' }
329
+ let(:method) { 'fatal' }
330
+
331
+ it_behaves_like 'log'
332
+ end
333
+
334
+ describe '#level' do
335
+ context 'DEBUG' do
336
+ let(:log_msg) { 'log message' }
337
+ before { logger.level = Logger::DEBUG }
338
+
339
+ it 'outputs debug message' do
340
+ logger.debug(log_msg)
341
+ expect(item).to be_log_message(log_msg, 20)
342
+ end
343
+
344
+ it 'outputs info message' do
345
+ logger.info(log_msg)
346
+ expect(item).to be_log_message(log_msg, 30)
347
+ end
348
+
349
+ it 'outputs warning message' do
350
+ logger.warn(log_msg)
351
+ expect(item).to be_log_message(log_msg, 40)
352
+ end
353
+
354
+ it 'outputs error message' do
355
+ logger.error(log_msg)
356
+ expect(item).to be_log_message(log_msg, 50)
357
+ end
358
+
359
+ it 'outputs fatal message' do
360
+ logger.fatal(log_msg)
361
+ expect(item).to be_log_message(log_msg, 60)
362
+ end
363
+
364
+ it 'outputs unknown message' do
365
+ logger.unknown(log_msg)
366
+ expect(item).to be_log_message(log_msg, 70)
367
+ end
368
+ end
369
+
370
+ context 'INFO' do
371
+ let(:log_msg) { 'log message' }
372
+ before { logger.level = Logger::INFO }
373
+
374
+ it 'does not output debug message' do
375
+ logger.debug(log_msg)
376
+ expect(item).to be_nil
377
+ end
378
+
379
+ it 'outputs info message' do
380
+ logger.info(log_msg)
381
+ expect(item).to be_log_message(log_msg, 30)
382
+ end
383
+
384
+ it 'outputs warning message' do
385
+ logger.warn(log_msg)
386
+ expect(item).to be_log_message(log_msg, 40)
387
+ end
388
+
389
+ it 'outputs error message' do
390
+ logger.error(log_msg)
391
+ expect(item).to be_log_message(log_msg, 50)
392
+ end
393
+
394
+ it 'outputs fatal message' do
395
+ logger.fatal(log_msg)
396
+ expect(item).to be_log_message(log_msg, 60)
397
+ end
398
+
399
+ it 'outputs unknown message' do
400
+ logger.unknown(log_msg)
401
+ expect(item).to be_log_message(log_msg, 70)
402
+ end
403
+ end
404
+
405
+ context 'WARN' do
406
+ let(:log_msg) { 'log message' }
407
+ before { logger.level = Logger::WARN }
408
+
409
+ it 'does not output debug message' do
410
+ logger.debug(log_msg)
411
+ expect(item).to be_nil
412
+ end
413
+
414
+ it 'does not output info message' do
415
+ logger.info(log_msg)
416
+ expect(item).to be_nil
417
+ end
418
+
419
+ it 'outputs warning message' do
420
+ logger.warn(log_msg)
421
+ expect(item).to be_log_message(log_msg, 40)
422
+ end
423
+
424
+ it 'outputs error message' do
425
+ logger.error(log_msg)
426
+ expect(item).to be_log_message(log_msg, 50)
427
+ end
428
+
429
+ it 'outputs fatal message' do
430
+ logger.fatal(log_msg)
431
+ expect(item).to be_log_message(log_msg, 60)
432
+ end
433
+
434
+ it 'outputs unknown message' do
435
+ logger.unknown(log_msg)
436
+ expect(item).to be_log_message(log_msg, 70)
437
+ end
438
+ end
439
+
440
+ context 'ERROR' do
441
+ let(:log_msg) { 'log message' }
442
+ before { logger.level = Logger::ERROR }
443
+
444
+ it 'does not output debug message' do
445
+ logger.debug(log_msg)
446
+ expect(item).to be_nil
447
+ end
448
+
449
+ it 'does not output info message' do
450
+ logger.info(log_msg)
451
+ expect(item).to be_nil
452
+ end
453
+
454
+ it 'does not output warning message' do
455
+ logger.warn(log_msg)
456
+ expect(item).to be_nil
457
+ end
458
+
459
+ it 'outputs error message' do
460
+ logger.error(log_msg)
461
+ expect(item).to be_log_message(log_msg, 50)
462
+ end
463
+
464
+ it 'outputs fatal message' do
465
+ logger.fatal(log_msg)
466
+ expect(item).to be_log_message(log_msg, 60)
467
+ end
468
+
469
+ it 'outputs unknown message' do
470
+ logger.unknown(log_msg)
471
+ expect(item).to be_log_message(log_msg, 70)
472
+ end
473
+ end
474
+
475
+ context 'FATAL' do
476
+ let(:log_msg) { 'log message' }
477
+ before { logger.level = Logger::FATAL }
478
+
479
+ it 'does not output debug message' do
480
+ logger.debug(log_msg)
481
+ expect(item).to be_nil
482
+ end
483
+
484
+ it 'does not output info message' do
485
+ logger.info(log_msg)
486
+ expect(item).to be_nil
487
+ end
488
+
489
+ it 'does not output warning message' do
490
+ logger.warn(log_msg)
491
+ expect(item).to be_nil
492
+ end
493
+
494
+ it 'does not output error message' do
495
+ logger.error(log_msg)
496
+ expect(item).to be_nil
497
+ end
498
+
499
+ it 'outputs fatal message' do
500
+ logger.fatal(log_msg)
501
+ expect(item).to be_log_message(log_msg, 60)
502
+ end
503
+
504
+ it 'outputs unknown message' do
505
+ logger.unknown(log_msg)
506
+ expect(item).to be_log_message(log_msg, 70)
507
+ end
508
+ end
509
+
510
+ context 'UNKNOWN' do
511
+ let(:log_msg) { 'log message' }
512
+ before { logger.level = Logger::UNKNOWN }
513
+
514
+ it 'does not output debug message' do
515
+ logger.debug(log_msg)
516
+ expect(item).to be_nil
517
+ end
518
+
519
+ it 'does not output info message' do
520
+ logger.info(log_msg)
521
+ expect(item).to be_nil
522
+ end
523
+
524
+ it 'does not output warning message' do
525
+ logger.warn(log_msg)
526
+ expect(item).to be_nil
527
+ end
528
+
529
+ it 'does not output error message' do
530
+ logger.error(log_msg)
531
+ expect(item).to be_nil
532
+ end
533
+
534
+ it 'does not output fatal message' do
535
+ logger.fatal(log_msg)
536
+ expect(item).to be_nil
537
+ end
538
+
539
+ it 'outputs unknown message' do
540
+ logger.unknown(log_msg)
541
+ expect(item).to be_log_message(log_msg, 70)
542
+ end
543
+ end
544
+ end
545
+
546
+ describe '#before_log' do
547
+ let(:log_msg) { 'before_log test' }
548
+
549
+ context 'set context data' do
550
+ before do
551
+ logger.level = Logger::INFO
552
+ logger.before_log = lambda do |data|
553
+ data[:context_id] = 123
554
+ end
555
+ end
556
+
557
+ it 'outputs with context data' do
558
+ logger.info(log_msg)
559
+ expect(item).to be_log_message(log_msg, 30)
560
+ expect(item).to include(context_id: 123)
561
+ end
562
+ end
563
+
564
+ context 'cancelling log' do
565
+ before do
566
+ logger.level = Logger::INFO
567
+ logger.before_log = lambda do |data|
568
+ false
569
+ end
570
+ end
571
+
572
+ it 'outputs none' do
573
+ logger.info(log_msg)
574
+ expect(item).to be_nil
575
+ end
576
+ end
577
+ end
578
+
579
+ describe '#broadcast' do
580
+ let(:log_msg) { 'broadcast test message' }
581
+
582
+ let(:another_io) { StringIO.new }
583
+ let(:another_logger) { described_class.new(another_io) }
584
+
585
+ let(:another_item) do
586
+ log_str = another_io.string
587
+ begin
588
+ JSON.parse(log_str, symbolize_names: true)
589
+ rescue Exception
590
+ nil
591
+ end
592
+ end
593
+
594
+ before do
595
+ logger.extend Ougai::Logger.broadcast(another_logger)
596
+ end
597
+
598
+ context 'another logger level is the same as original one' do
599
+ before do
600
+ logger.level = Logger::INFO # propagate severity to another one
601
+ end
602
+
603
+ it 'does not output trace log on both loggers' do
604
+ logger.trace(log_msg, foo: 0)
605
+ expect(item).to be_nil
606
+ expect(another_item).to be_nil
607
+ end
608
+
609
+ it 'does not output debug log on both loggers' do
610
+ logger.debug(log_msg, foo: 1)
611
+ expect(item).to be_nil
612
+ expect(another_item).to be_nil
613
+ end
614
+
615
+ it 'does not output debug log with block on both loggers' do
616
+ logger.debug { [log_msg, foo: 1] }
617
+ expect(item).to be_nil
618
+ expect(another_item).to be_nil
619
+ end
620
+
621
+ it 'outputs info log on both loggers' do
622
+ logger.info(log_msg, foo: 2)
623
+ expect(item).to be_log_message(log_msg, 30)
624
+ expect(item).to include_data(foo: 2)
625
+ expect(another_item).to be_log_message(log_msg, 30)
626
+ expect(another_item).to include_data(foo: 2)
627
+ end
628
+
629
+ it 'outputs info log with block on both loggers' do
630
+ logger.info(log_msg, foo: 2)
631
+ expect(item).to be_log_message(log_msg, 30)
632
+ expect(item).to include_data(foo: 2)
633
+ expect(another_item).to be_log_message(log_msg, 30)
634
+ expect(another_item).to include_data(foo: 2)
635
+ end
636
+
637
+ it 'outputs warning log on both loggers' do
638
+ logger.warn(log_msg)
639
+ expect(item).to be_log_message(log_msg, 40)
640
+ expect(another_item).to be_log_message(log_msg, 40)
641
+ end
642
+ end
643
+
644
+ context 'another logger level is lower than original one' do
645
+ before do
646
+ logger.level = Logger::DEBUG
647
+ another_logger.level = :trace
648
+ end
649
+
650
+ it 'does not output trace log on both loggers' do
651
+ logger.trace(log_msg)
652
+ expect(item).to be_nil
653
+ expect(another_item).to be_log_message(log_msg, 10)
654
+ end
655
+
656
+ it 'outputs debug log on both loggers' do
657
+ logger.debug(log_msg)
658
+ expect(item).to be_log_message(log_msg, 20)
659
+ expect(another_item).to be_log_message(log_msg, 20)
660
+ end
661
+ end
662
+
663
+ context 'another logger level is greater than original one' do
664
+ before do
665
+ logger.level = Logger::INFO
666
+ another_logger.level = Logger::WARN
667
+ end
668
+
669
+ it 'outputs info log on only original logger' do
670
+ logger.info(log_msg)
671
+ expect(item).to be_log_message(log_msg, 30)
672
+ expect(another_item).to be_nil
673
+ end
674
+
675
+ it 'outputs warning log on both loggers' do
676
+ logger.warn(log_msg)
677
+ expect(item).to be_log_message(log_msg, 40)
678
+ expect(another_item).to be_log_message(log_msg, 40)
679
+ end
680
+ end
681
+
682
+ it 'close both loogers' do
683
+ logger.close
684
+ expect(io.closed?).to be_truthy
685
+ expect(another_io.closed?).to be_truthy
686
+ end
687
+ end
688
+ end