appsignal 3.9.2 → 3.9.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ci.yml +3135 -0
  3. data/.rubocop.yml +28 -20
  4. data/.rubocop_todo.yml +7 -33
  5. data/CHANGELOG.md +38 -0
  6. data/Rakefile +79 -64
  7. data/appsignal.gemspec +1 -1
  8. data/build_matrix.yml +109 -179
  9. data/ext/base.rb +1 -1
  10. data/gemfiles/hanami-2.1.gemfile +7 -0
  11. data/lib/appsignal/cli/diagnose.rb +1 -1
  12. data/lib/appsignal/config.rb +1 -1
  13. data/lib/appsignal/demo.rb +0 -1
  14. data/lib/appsignal/environment.rb +5 -1
  15. data/lib/appsignal/extension/jruby.rb +1 -1
  16. data/lib/appsignal/helpers/instrumentation.rb +1 -1
  17. data/lib/appsignal/integrations/grape.rb +19 -47
  18. data/lib/appsignal/integrations/hanami.rb +8 -7
  19. data/lib/appsignal/integrations/padrino.rb +46 -43
  20. data/lib/appsignal/integrations/railtie.rb +0 -3
  21. data/lib/appsignal/integrations/sinatra.rb +0 -1
  22. data/lib/appsignal/probes/gvl.rb +24 -2
  23. data/lib/appsignal/probes/sidekiq.rb +1 -1
  24. data/lib/appsignal/probes.rb +1 -1
  25. data/lib/appsignal/rack/abstract_middleware.rb +62 -28
  26. data/lib/appsignal/rack/event_handler.rb +12 -3
  27. data/lib/appsignal/rack/grape_middleware.rb +40 -0
  28. data/lib/appsignal/rack/hanami_middleware.rb +1 -11
  29. data/lib/appsignal/rack/rails_instrumentation.rb +14 -55
  30. data/lib/appsignal/utils/integration_memory_logger.rb +78 -0
  31. data/lib/appsignal/utils.rb +1 -0
  32. data/lib/appsignal/version.rb +1 -1
  33. data/lib/appsignal.rb +34 -33
  34. data/spec/.rubocop.yml +1 -1
  35. data/spec/lib/appsignal/cli/diagnose_spec.rb +1 -1
  36. data/spec/lib/appsignal/cli/install_spec.rb +3 -3
  37. data/spec/lib/appsignal/config_spec.rb +7 -5
  38. data/spec/lib/appsignal/demo_spec.rb +38 -41
  39. data/spec/lib/appsignal/hooks/action_cable_spec.rb +86 -167
  40. data/spec/lib/appsignal/hooks/active_support_notifications/finish_with_state_shared_examples.rb +8 -20
  41. data/spec/lib/appsignal/hooks/active_support_notifications/instrument_shared_examples.rb +38 -84
  42. data/spec/lib/appsignal/hooks/active_support_notifications/start_finish_shared_examples.rb +16 -37
  43. data/spec/lib/appsignal/hooks/active_support_notifications_spec.rb +4 -4
  44. data/spec/lib/appsignal/hooks/activejob_spec.rb +111 -200
  45. data/spec/lib/appsignal/hooks/delayed_job_spec.rb +54 -91
  46. data/spec/lib/appsignal/hooks/dry_monitor_spec.rb +14 -32
  47. data/spec/lib/appsignal/hooks/excon_spec.rb +8 -12
  48. data/spec/lib/appsignal/hooks/net_http_spec.rb +7 -42
  49. data/spec/lib/appsignal/hooks/rake_spec.rb +9 -19
  50. data/spec/lib/appsignal/hooks/redis_client_spec.rb +18 -30
  51. data/spec/lib/appsignal/hooks/redis_spec.rb +10 -16
  52. data/spec/lib/appsignal/hooks/resque_spec.rb +42 -62
  53. data/spec/lib/appsignal/hooks/shoryuken_spec.rb +33 -74
  54. data/spec/lib/appsignal/integrations/hanami_spec.rb +79 -21
  55. data/spec/lib/appsignal/integrations/http_spec.rb +12 -20
  56. data/spec/lib/appsignal/integrations/net_http_spec.rb +33 -0
  57. data/spec/lib/appsignal/integrations/object_spec.rb +29 -36
  58. data/spec/lib/appsignal/integrations/padrino_spec.rb +47 -70
  59. data/spec/lib/appsignal/integrations/que_spec.rb +43 -70
  60. data/spec/lib/appsignal/integrations/railtie_spec.rb +26 -67
  61. data/spec/lib/appsignal/integrations/sidekiq_spec.rb +86 -160
  62. data/spec/lib/appsignal/integrations/sinatra_spec.rb +0 -1
  63. data/spec/lib/appsignal/integrations/webmachine_spec.rb +28 -39
  64. data/spec/lib/appsignal/probes/gvl_spec.rb +80 -3
  65. data/spec/lib/appsignal/probes_spec.rb +7 -4
  66. data/spec/lib/appsignal/rack/abstract_middleware_spec.rb +215 -106
  67. data/spec/lib/appsignal/rack/event_handler_spec.rb +81 -78
  68. data/spec/lib/appsignal/rack/generic_instrumentation_spec.rb +2 -12
  69. data/spec/lib/appsignal/rack/grape_middleware_spec.rb +234 -0
  70. data/spec/lib/appsignal/rack/hanami_middleware_spec.rb +2 -16
  71. data/spec/lib/appsignal/rack/rails_instrumentation_spec.rb +67 -131
  72. data/spec/lib/appsignal/rack/sinatra_instrumentation_spec.rb +36 -44
  73. data/spec/lib/appsignal/rack/streaming_listener_spec.rb +68 -86
  74. data/spec/lib/appsignal/transaction_spec.rb +76 -90
  75. data/spec/lib/appsignal/utils/integration_memory_logger_spec.rb +163 -0
  76. data/spec/lib/appsignal_spec.rb +363 -342
  77. data/spec/support/helpers/dependency_helper.rb +6 -1
  78. data/spec/support/helpers/std_streams_helper.rb +1 -1
  79. data/spec/support/helpers/transaction_helpers.rb +8 -0
  80. data/spec/support/matchers/transaction.rb +185 -0
  81. data/spec/support/mocks/dummy_app.rb +20 -0
  82. data/spec/support/shared_examples/instrument.rb +17 -12
  83. data/spec/support/testing.rb +18 -9
  84. metadata +15 -10
  85. data/.semaphore/semaphore.yml +0 -2347
  86. data/script/lint_git +0 -22
  87. data/spec/lib/appsignal/integrations/grape_spec.rb +0 -239
  88. data/spec/support/matchers/be_completed.rb +0 -5
  89. /data/gemfiles/{hanami.gemfile → hanami-2.0.gemfile} +0 -0
@@ -40,10 +40,8 @@ describe Appsignal::Transaction do
40
40
  expect(transaction.namespace).to eq namespace
41
41
  expect(transaction.request).to eq request
42
42
 
43
- expect(transaction.to_h).to include(
44
- "id" => transaction_id,
45
- "namespace" => namespace
46
- )
43
+ expect(transaction).to have_id(transaction_id)
44
+ expect(transaction).to have_namespace(namespace)
47
45
  end
48
46
 
49
47
  it "assigns the transaction to current" do
@@ -85,10 +83,6 @@ describe Appsignal::Transaction do
85
83
  end
86
84
 
87
85
  describe ".current" do
88
- def current_transaction
89
- Appsignal::Transaction.current
90
- end
91
-
92
86
  context "when there is a current transaction" do
93
87
  let!(:transaction) do
94
88
  Appsignal::Transaction.create(transaction_id, namespace, request, options)
@@ -166,9 +160,7 @@ describe Appsignal::Transaction do
166
160
  it "samples data" do
167
161
  transaction.set_tags(:foo => "bar")
168
162
  keep_transactions { transaction.complete }
169
- expect(transaction.to_h["sample_data"]).to include(
170
- "tags" => { "foo" => "bar" }
171
- )
163
+ expect(transaction).to include_tags("foo" => "bar")
172
164
  end
173
165
  end
174
166
 
@@ -181,13 +173,13 @@ describe Appsignal::Transaction do
181
173
 
182
174
  context "when a transaction is marked as discarded" do
183
175
  it "does not complete the transaction" do
184
- expect(transaction.ext).to_not receive(:complete)
185
-
186
176
  expect do
187
177
  transaction.discard!
188
178
  end.to change { transaction.discarded? }.from(false).to(true)
189
179
 
190
180
  transaction.complete
181
+
182
+ expect(transaction).to_not be_completed
191
183
  end
192
184
 
193
185
  it "logs a debug message" do
@@ -202,13 +194,13 @@ describe Appsignal::Transaction do
202
194
  before { transaction.discard! }
203
195
 
204
196
  it "completes the transaction" do
205
- expect(transaction.ext).to receive(:complete).and_call_original
206
-
207
197
  expect do
208
198
  transaction.restore!
209
199
  end.to change { transaction.discarded? }.from(true).to(false)
210
200
 
211
201
  transaction.complete
202
+
203
+ expect(transaction).to be_completed
212
204
  end
213
205
  end
214
206
  end
@@ -354,9 +346,9 @@ describe Appsignal::Transaction do
354
346
  params = { "foo" => "bar" }
355
347
  silence { transaction.params = params }
356
348
 
357
- transaction.complete # Sample the data
349
+ transaction._sample
358
350
  expect(transaction.params).to eq(params)
359
- expect(transaction.to_h.dig("sample_data", "params")).to eq(params)
351
+ expect(transaction).to include_params(params)
360
352
  end
361
353
 
362
354
  it "logs a deprecation warning" do
@@ -378,9 +370,9 @@ describe Appsignal::Transaction do
378
370
  params = { "key" => "value" }
379
371
  transaction.set_params(params)
380
372
 
381
- transaction.complete # Sample the data
373
+ transaction._sample
382
374
  expect(transaction.params).to eq(params)
383
- expect(transaction.to_h.dig("sample_data", "params")).to eq(params)
375
+ expect(transaction).to include_params(params)
384
376
  end
385
377
  end
386
378
 
@@ -390,9 +382,9 @@ describe Appsignal::Transaction do
390
382
  transaction.set_params(params)
391
383
  transaction.set_params(nil)
392
384
 
393
- transaction.complete # Sample the data
385
+ transaction._sample
394
386
  expect(transaction.params).to eq(params)
395
- expect(transaction.to_h.dig("sample_data", "params")).to eq(params)
387
+ expect(transaction).to include_params(params)
396
388
  end
397
389
  end
398
390
  end
@@ -405,9 +397,9 @@ describe Appsignal::Transaction do
405
397
  params = { "key" => "value" }
406
398
  transaction.set_params_if_nil(params)
407
399
 
408
- transaction.complete # Sample the data
400
+ transaction._sample
409
401
  expect(transaction.params).to eq(params)
410
- expect(transaction.to_h.dig("sample_data", "params")).to eq(params)
402
+ expect(transaction).to include_params(params)
411
403
  end
412
404
 
413
405
  context "when the given params is nil" do
@@ -416,9 +408,9 @@ describe Appsignal::Transaction do
416
408
  transaction.set_params(params)
417
409
  transaction.set_params_if_nil(nil)
418
410
 
419
- transaction.complete # Sample the data
411
+ transaction._sample
420
412
  expect(transaction.params).to eq(params)
421
- expect(transaction.to_h.dig("sample_data", "params")).to eq(params)
413
+ expect(transaction).to include_params(params)
422
414
  end
423
415
  end
424
416
  end
@@ -430,9 +422,9 @@ describe Appsignal::Transaction do
430
422
  transaction.set_params(preset_params)
431
423
  transaction.set_params_if_nil(params)
432
424
 
433
- transaction.complete # Sample the data
425
+ transaction._sample
434
426
  expect(transaction.params).to eq(preset_params)
435
- expect(transaction.to_h.dig("sample_data", "params")).to eq(preset_params)
427
+ expect(transaction).to include_params(preset_params)
436
428
  end
437
429
  end
438
430
  end
@@ -455,7 +447,7 @@ describe Appsignal::Transaction do
455
447
  end
456
448
 
457
449
  it "stores tags on the transaction" do
458
- expect(transaction.to_h["sample_data"]["tags"]).to eq(
450
+ expect(transaction).to include_tags(
459
451
  "valid_key" => "valid_value",
460
452
  "valid_string_key" => "valid_value",
461
453
  "both_symbols" => "valid_value",
@@ -507,12 +499,13 @@ describe Appsignal::Transaction do
507
499
  transaction.sample_data
508
500
  timeframe_end = Time.now.utc.to_i
509
501
 
510
- breadcrumb = transaction.to_h["sample_data"]["breadcrumbs"][0]
511
- expect(breadcrumb["category"]).to eq("user_action")
512
- expect(breadcrumb["action"]).to eq("clicked HOME")
513
- expect(breadcrumb["message"]).to eq("")
514
- expect(breadcrumb["time"]).to be_between(timeframe_start, timeframe_end)
515
- expect(breadcrumb["metadata"]).to eq({})
502
+ expect(transaction).to include_breadcrumb(
503
+ "clicked HOME",
504
+ "user_action",
505
+ "",
506
+ {},
507
+ be_between(timeframe_start, timeframe_end)
508
+ )
516
509
  end
517
510
  end
518
511
 
@@ -521,7 +514,7 @@ describe Appsignal::Transaction do
521
514
  transaction.add_breadcrumb("category", "action", "message", "invalid metadata")
522
515
  transaction.sample_data
523
516
 
524
- expect(transaction.to_h["sample_data"]["breadcrumbs"]).to be_empty
517
+ expect(transaction).to_not include_breadcrumbs
525
518
  expect(log_contents(log)).to contains_log(
526
519
  :error,
527
520
  "add_breadcrumb: Cannot add breadcrumb. The given metadata argument is not a Hash."
@@ -537,7 +530,7 @@ describe Appsignal::Transaction do
537
530
  transaction.set_action(action_name)
538
531
 
539
532
  expect(transaction.action).to eq(action_name)
540
- expect(transaction.to_h["action"]).to eq(action_name)
533
+ expect(transaction).to have_action(action_name)
541
534
  end
542
535
  end
543
536
 
@@ -548,7 +541,7 @@ describe Appsignal::Transaction do
548
541
  transaction.set_action(nil)
549
542
 
550
543
  expect(transaction.action).to eq(action_name)
551
- expect(transaction.to_h["action"]).to eq(action_name)
544
+ expect(transaction).to have_action(action_name)
552
545
  end
553
546
  end
554
547
  end
@@ -557,13 +550,13 @@ describe Appsignal::Transaction do
557
550
  context "when the action is not set" do
558
551
  it "updates the action name on the transaction" do
559
552
  expect(transaction.action).to eq(nil)
560
- expect(transaction.to_h["action"]).to eq(nil)
553
+ expect(transaction).to_not have_action
561
554
 
562
555
  action_name = "PagesController#show"
563
556
  transaction.set_action_if_nil(action_name)
564
557
 
565
558
  expect(transaction.action).to eq(action_name)
566
- expect(transaction.to_h["action"]).to eq(action_name)
559
+ expect(transaction).to have_action(action_name)
567
560
  end
568
561
 
569
562
  context "when the given action is nil" do
@@ -573,7 +566,7 @@ describe Appsignal::Transaction do
573
566
  transaction.set_action_if_nil(nil)
574
567
 
575
568
  expect(transaction.action).to eq(action_name)
576
- expect(transaction.to_h["action"]).to eq(action_name)
569
+ expect(transaction).to have_action(action_name)
577
570
  end
578
571
  end
579
572
  end
@@ -585,7 +578,7 @@ describe Appsignal::Transaction do
585
578
  transaction.set_action_if_nil("something else")
586
579
 
587
580
  expect(transaction.action).to eq(action_name)
588
- expect(transaction.to_h["action"]).to eq(action_name)
581
+ expect(transaction).to have_action(action_name)
589
582
  end
590
583
  end
591
584
  end
@@ -597,7 +590,7 @@ describe Appsignal::Transaction do
597
590
  transaction.set_namespace(namespace)
598
591
 
599
592
  expect(transaction.namespace).to eq namespace
600
- expect(transaction.to_h["namespace"]).to eq(namespace)
593
+ expect(transaction).to have_namespace(namespace)
601
594
  end
602
595
  end
603
596
 
@@ -608,7 +601,7 @@ describe Appsignal::Transaction do
608
601
  transaction.set_namespace(nil)
609
602
 
610
603
  expect(transaction.namespace).to eq(namespace)
611
- expect(transaction.to_h["namespace"]).to eq(namespace)
604
+ expect(transaction).to have_namespace(namespace)
612
605
  end
613
606
  end
614
607
  end
@@ -620,21 +613,21 @@ describe Appsignal::Transaction do
620
613
  :controller => "HomeController",
621
614
  :action => "show"
622
615
  )
623
- expect(transaction.to_h["action"]).to eql("HomeController#show")
616
+ expect(transaction).to have_action("HomeController#show")
624
617
  end
625
618
  end
626
619
 
627
620
  context "for a hash with just action" do
628
621
  it "sets the action" do
629
622
  transaction.set_http_or_background_action(:action => "show")
630
- expect(transaction.to_h["action"]).to eql("show")
623
+ expect(transaction).to have_action("show")
631
624
  end
632
625
  end
633
626
 
634
627
  context "for a hash with class and method" do
635
628
  it "sets the action" do
636
629
  transaction.set_http_or_background_action(:class => "Worker", :method => "perform")
637
- expect(transaction.to_h["action"]).to eql("Worker#perform")
630
+ expect(transaction).to have_action("Worker#perform")
638
631
  end
639
632
  end
640
633
 
@@ -642,7 +635,7 @@ describe Appsignal::Transaction do
642
635
  it "does not overwrite the set action" do
643
636
  transaction.set_action("MyCustomAction#perform")
644
637
  transaction.set_http_or_background_action(:class => "Worker", :method => "perform")
645
- expect(transaction.to_h["action"]).to eql("MyCustomAction#perform")
638
+ expect(transaction).to have_action("MyCustomAction#perform")
646
639
  end
647
640
  end
648
641
  end
@@ -665,9 +658,7 @@ describe Appsignal::Transaction do
665
658
 
666
659
  expect(Appsignal.internal_logger).to receive(:warn).with("Queue start value 10 is too big")
667
660
 
668
- expect do
669
- transaction.set_queue_start(10)
670
- end.to_not raise_error
661
+ transaction.set_queue_start(10)
671
662
  end
672
663
  end
673
664
 
@@ -716,7 +707,7 @@ describe Appsignal::Transaction do
716
707
  it "updates the metadata on the transaction" do
717
708
  transaction.set_metadata("request_method", "GET")
718
709
 
719
- expect(transaction.to_h["metadata"]).to eq("request_method" => "GET")
710
+ expect(transaction).to include_metadata("request_method" => "GET")
720
711
  end
721
712
 
722
713
  context "when filter_metadata includes metadata key" do
@@ -727,7 +718,7 @@ describe Appsignal::Transaction do
727
718
  transaction.set_metadata(:filter_key, "filtered value")
728
719
  transaction.set_metadata("filter_key", "filtered value")
729
720
 
730
- expect(transaction.to_h["metadata"].keys).to_not include("filter_key")
721
+ expect(transaction).to_not include_metadata("filter_key" => anything)
731
722
  end
732
723
  end
733
724
 
@@ -735,7 +726,7 @@ describe Appsignal::Transaction do
735
726
  it "does not update the metadata on the transaction" do
736
727
  transaction.set_metadata(nil, "GET")
737
728
 
738
- expect(transaction.to_h["metadata"]).to eq({})
729
+ expect(transaction).to_not include_metadata
739
730
  end
740
731
  end
741
732
 
@@ -743,7 +734,7 @@ describe Appsignal::Transaction do
743
734
  it "does not update the metadata on the transaction" do
744
735
  transaction.set_metadata("request_method", nil)
745
736
 
746
- expect(transaction.to_h["metadata"]).to eq({})
737
+ expect(transaction).to_not include_metadata
747
738
  end
748
739
  end
749
740
  end
@@ -757,12 +748,10 @@ describe Appsignal::Transaction do
757
748
  :id => "1"
758
749
  )
759
750
 
760
- expect(transaction.to_h["sample_data"]).to eq(
761
- "params" => {
762
- "action" => "show",
763
- "controller" => "blog_posts",
764
- "id" => "1"
765
- }
751
+ expect(transaction).to include_params(
752
+ "action" => "show",
753
+ "controller" => "blog_posts",
754
+ "id" => "1"
766
755
  )
767
756
  end
768
757
 
@@ -785,7 +774,7 @@ describe Appsignal::Transaction do
785
774
  end
786
775
  transaction.set_sample_data("params", klass.new => 1)
787
776
 
788
- expect(transaction.to_h["sample_data"]).to eq({})
777
+ expect(transaction).to_not include_params
789
778
  expect(log_contents(log)).to contains_log :error,
790
779
  "Error generating data (RuntimeError: foo) for"
791
780
  end
@@ -800,27 +789,26 @@ describe Appsignal::Transaction do
800
789
  transaction.add_breadcrumb "category", "action", "message", "key" => "value"
801
790
  transaction.sample_data
802
791
 
803
- sample_data = transaction.to_h["sample_data"]
804
- expect(sample_data["environment"]).to include(
792
+ expect(transaction).to include_environment(
805
793
  "REQUEST_METHOD" => "GET",
806
794
  "SERVER_NAME" => "example.org",
807
795
  "SERVER_PORT" => "80",
808
796
  "PATH_INFO" => "/blog"
809
797
  )
810
- expect(sample_data["session_data"]).to eq("session" => "value")
811
- expect(sample_data["params"]).to eq(
798
+ expect(transaction).to include_session_data("session" => "value")
799
+ expect(transaction).to include_params(
812
800
  "controller" => "blog_posts",
813
801
  "action" => "show",
814
802
  "id" => "1"
815
803
  )
816
- expect(sample_data["metadata"]).to eq("key" => "value")
817
- expect(sample_data["tags"]).to eq("tag" => "value")
818
- expect(sample_data["breadcrumbs"]).to contain_exactly(
819
- "action" => "action",
820
- "category" => "category",
821
- "message" => "message",
822
- "metadata" => { "key" => "value" },
823
- "time" => kind_of(Integer)
804
+ expect(transaction).to include_sample_metadata("key" => "value")
805
+ expect(transaction).to include_tags("tag" => "value")
806
+ expect(transaction).to include_breadcrumb(
807
+ "action",
808
+ "category",
809
+ "message",
810
+ { "key" => "value" },
811
+ kind_of(Integer)
824
812
  )
825
813
  end
826
814
  end
@@ -972,7 +960,7 @@ describe Appsignal::Transaction do
972
960
  end
973
961
 
974
962
  it "should not raise an error" do
975
- expect { transaction.set_error(error) }.to_not raise_error
963
+ transaction.set_error(error)
976
964
  end
977
965
 
978
966
  it "should set an error in the extension" do
@@ -1613,22 +1601,20 @@ describe Appsignal::Transaction do
1613
1601
  subject { Appsignal::Transaction::NilTransaction.new }
1614
1602
 
1615
1603
  it "should have method stubs" do
1616
- expect do
1617
- subject.complete
1618
- subject.pause!
1619
- subject.resume!
1620
- subject.paused?
1621
- subject.store(:key)
1622
- subject.set_tags(:tag => 1)
1623
- subject.set_action("action")
1624
- subject.set_http_or_background_action
1625
- subject.set_queue_start(1)
1626
- subject.set_http_or_background_queue_start
1627
- subject.set_metadata("key", "value")
1628
- subject.set_sample_data("key", "data")
1629
- subject.sample_data
1630
- subject.set_error("a")
1631
- end.to_not raise_error
1604
+ subject.complete
1605
+ subject.pause!
1606
+ subject.resume!
1607
+ subject.paused?
1608
+ subject.store(:key)
1609
+ subject.set_tags(:tag => 1)
1610
+ subject.set_action("action")
1611
+ subject.set_http_or_background_action
1612
+ subject.set_queue_start(1)
1613
+ subject.set_http_or_background_queue_start
1614
+ subject.set_metadata("key", "value")
1615
+ subject.set_sample_data("key", "data")
1616
+ subject.sample_data
1617
+ subject.set_error("a")
1632
1618
  end
1633
1619
  end
1634
1620
  end
@@ -0,0 +1,163 @@
1
+ describe Appsignal::Utils::IntegrationLogger do
2
+ let(:formatter) { nil }
3
+ let(:logger) do
4
+ Appsignal::Utils::IntegrationMemoryLogger.new.tap do |l|
5
+ l.formatter = formatter if formatter
6
+ end
7
+ end
8
+
9
+ describe "#add" do
10
+ it "adds a log message with the severity" do
11
+ logger.add(:DEBUG, "debug message")
12
+ logger.add(:INFO, "info message")
13
+ logger.add(:WARN, "warn message")
14
+ logger.add(:ERROR, "error message")
15
+ logger.add(:FATAL, "fatal message")
16
+ logger.add(:UNKNOWN, "unknown message")
17
+
18
+ expect(logger.messages[:DEBUG]).to eq(["debug message"])
19
+ expect(logger.messages[:INFO]).to eq(["info message"])
20
+ expect(logger.messages[:WARN]).to eq(["warn message"])
21
+ expect(logger.messages[:ERROR]).to eq(["error message"])
22
+ expect(logger.messages[:FATAL]).to eq(["fatal message"])
23
+ expect(logger.messages[:UNKNOWN]).to eq(["unknown message"])
24
+ end
25
+
26
+ context "without formatter" do
27
+ it "logs in the default format" do
28
+ logger.add(:DEBUG, "debug message")
29
+ expect(logger.messages[:DEBUG]).to eq(["debug message"])
30
+ end
31
+ end
32
+
33
+ context "with formatter" do
34
+ let(:formatter) do
35
+ proc do |severity, _datetime, _progname, msg|
36
+ "[TIME (process) #PID][#{severity}] #{msg}\n"
37
+ end
38
+ end
39
+
40
+ it "formats the logs using the formatter" do
41
+ logger.add(:DEBUG, "debug message")
42
+ expect(logger.messages[:DEBUG]).to eq(["[TIME (process) #PID][DEBUG] debug message\n"])
43
+ end
44
+ end
45
+ end
46
+
47
+ describe "#debug" do
48
+ it "adds a log message with the debug severity" do
49
+ logger.debug("debug message")
50
+
51
+ expect(logger.messages[:DEBUG]).to eq(["debug message"])
52
+ end
53
+ end
54
+
55
+ describe "#info" do
56
+ it "adds a log message with the info severity" do
57
+ logger.info("info message")
58
+
59
+ expect(logger.messages[:INFO]).to eq(["info message"])
60
+ end
61
+ end
62
+
63
+ describe "#warn" do
64
+ it "adds a log message with the warn severity" do
65
+ logger.warn("warn message")
66
+
67
+ expect(logger.messages[:WARN]).to eq(["warn message"])
68
+ end
69
+ end
70
+
71
+ describe "#warn_once_then_debug" do
72
+ it "only warns once, then uses debug" do
73
+ message = "This is a log line"
74
+ 3.times { logger.warn_once_then_debug(:key, message) }
75
+
76
+ expect(logger.messages[:WARN]).to eq([message])
77
+ expect(logger.messages[:DEBUG]).to eq([message, message])
78
+ end
79
+ end
80
+
81
+ describe "#error" do
82
+ it "adds a log message with the error severity" do
83
+ logger.error("error message")
84
+
85
+ expect(logger.messages[:ERROR]).to eq(["error message"])
86
+ end
87
+ end
88
+
89
+ describe "#fatal" do
90
+ it "adds a log message with the fatal severity" do
91
+ logger.fatal("fatal message")
92
+
93
+ expect(logger.messages[:FATAL]).to eq(["fatal message"])
94
+ end
95
+ end
96
+
97
+ describe "#unknown" do
98
+ it "adds a log message with the unknown severity" do
99
+ logger.unknown("unknown message")
100
+
101
+ expect(logger.messages[:UNKNOWN]).to eq(["unknown message"])
102
+ end
103
+ end
104
+
105
+ describe "#clear" do
106
+ it "clears all log messages" do
107
+ logger.add(:DEBUG, "debug message")
108
+ logger.add(:INFO, "info message")
109
+ logger.add(:WARN, "warn message")
110
+ logger.add(:ERROR, "error message")
111
+ logger.add(:FATAL, "fatal message")
112
+ logger.add(:UNKNOWN, "unknown message")
113
+ logger.clear
114
+
115
+ expect(logger.messages).to be_empty
116
+ end
117
+ end
118
+
119
+ describe "#messages_for_level" do
120
+ it "returns only log messages for level and higher" do
121
+ logger.add(:DEBUG, "debug message")
122
+ logger.add(:INFO, "info message")
123
+ logger.add(:WARN, "warn message")
124
+ logger.add(:ERROR, "error message")
125
+ logger.add(:FATAL, "fatal message")
126
+ logger.add(:UNKNOWN, "unknown message")
127
+
128
+ expect(logger.messages_for_level(Logger::DEBUG)).to eq([
129
+ "debug message",
130
+ "info message",
131
+ "warn message",
132
+ "error message",
133
+ "fatal message",
134
+ "unknown message"
135
+ ])
136
+ expect(logger.messages_for_level(Logger::INFO)).to eq([
137
+ "info message",
138
+ "warn message",
139
+ "error message",
140
+ "fatal message",
141
+ "unknown message"
142
+ ])
143
+ expect(logger.messages_for_level(Logger::WARN)).to eq([
144
+ "warn message",
145
+ "error message",
146
+ "fatal message",
147
+ "unknown message"
148
+ ])
149
+ expect(logger.messages_for_level(Logger::ERROR)).to eq([
150
+ "error message",
151
+ "fatal message",
152
+ "unknown message"
153
+ ])
154
+ expect(logger.messages_for_level(Logger::FATAL)).to eq([
155
+ "fatal message",
156
+ "unknown message"
157
+ ])
158
+ expect(logger.messages_for_level(Logger::UNKNOWN)).to eq([
159
+ "unknown message"
160
+ ])
161
+ end
162
+ end
163
+ end