ougai 1.9.1-java → 2.1.0-java

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,608 +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'] }
482
- end
483
-
484
- let(:logger) { parent_logger.child(tag: 'child', tags: ['child']) }
485
- let(:grand_logger) { logger.child(tag: 'grandchild', tags: ['grandchild']) }
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)
491
- parent_logger.info('Good evening!', foo: 11)
492
-
493
- expect(items[0]).to be_log_message('Hi', log_level)
494
- expect(items[0]).to include(tag: 'grandchild', tags: ['parent', 'child', 'grandchild'], foo: 3)
495
-
496
- expect(items[1]).to be_log_message(log_msg, log_level)
497
- expect(items[1]).to include(tag: 'child', tags: ['parent', 'child'], foo: 2)
498
-
499
- expect(items[2]).to be_log_message(parent_log_msg, log_level)
500
- expect(items[2]).to include(tag: 'parent', tags: ['parent'], foo: 10)
501
- expect(items[3]).to be_log_message('Good evening!', log_level)
502
- expect(items[3]).to include(tag: 'parent', tags: ['parent'], foo: 11)
503
- end
504
-
505
- context 'after updating child logger with_fields' do
506
- before do
507
- logger.with_fields = { bar: '33' }
508
- end
509
-
510
- it 'outputs with child fields' do
511
- logger.info(log_msg)
512
- expect(items[0]).to be_log_message(log_msg, log_level)
513
- expect(items[0]).to include(bar: '33')
514
- expect(items[0]).not_to include(:pos)
515
- end
516
- end
517
- end
518
- end
519
-
520
- describe '#before_log' do
521
- let(:logger) { parent_logger.child }
522
- let(:log_msg) { 'before_log test' }
523
-
524
- before do
525
- parent_logger.level = Logger::INFO
526
- end
527
-
528
- context 'child logger to be set before_log' do
529
- before do
530
- logger.before_log = lambda do |data|
531
- data[:context_id] = 123
532
- end
533
- end
534
-
535
- it 'outputs the field to be added in before_log' do
536
- logger.info(log_msg)
537
- expect(item).to be_log_message(log_msg, 30)
538
- expect(item).to include(context_id: 123)
539
- end
540
- end
541
-
542
- context 'parent logger to be set before_log' do
543
- before do
544
- parent_logger.before_log = lambda do |data|
545
- data[:context_id] = 12345
546
- end
547
- end
548
-
549
- it 'outputs the field to be added in before_log' do
550
- logger.info(log_msg)
551
- expect(item).to be_log_message(log_msg, 30)
552
- expect(item).to include(context_id: 12345)
553
- end
554
- end
555
-
556
- context 'both child logger and parent logger to be set before_log' do
557
- before do
558
- logger.before_log = lambda do |data|
559
- data[:context_id] = 67890
560
- data[:context_name] = 'sub'
561
- end
562
- parent_logger.before_log = lambda do |data|
563
- data[:context_id] = 12345
564
- end
565
- end
566
-
567
- it 'outputs the fields to be added in each before_log' do
568
- logger.info(log_msg)
569
- expect(item).to be_log_message(log_msg, 30)
570
- expect(item).to include(context_id: 12345) # parent
571
- expect(item).to include(context_name: 'sub') # child
572
- end
573
- end
574
- end
575
-
576
- describe '#child' do
577
- let!(:root) { double('root logger') }
578
-
579
- context 'when the class is original' do
580
- subject!(:org_instance) { described_class.new(root, {}) }
581
-
582
- it 'returns an instance of the same class' do
583
- expect(org_instance.child).to be_an_instance_of(described_class)
584
- end
585
- end
586
-
587
- context 'when the class is sub-class' do
588
- subject!(:sc_instance) { Class.new(described_class).new(root, {}) }
589
-
590
- it 'returns an instance of the child_class' do
591
- expect(sc_instance.child).to be_an_instance_of(sc_instance.class)
592
- end
593
- end
594
-
595
- context 'block is given' do
596
- let!(:fields) { double('fields') }
597
-
598
- subject { described_class.new(root, {}) }
599
-
600
- it 'yields child logger' do
601
- subject.child(fields) do |cl|
602
- expect(cl.instance_variable_get(:@parent)).to eq(subject)
603
- expect(cl.instance_variable_get(:@with_fields)).to eq(fields)
604
- end
605
- end
606
- end
607
- end
608
- 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