summaryrefslogtreecommitdiff
path: root/lib/logger/test/logger/translator_test.exs
blob: 0cb368edc5846140891cdbfb9b8d928f1c11b02b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
defmodule Logger.TranslatorTest do
  use Logger.Case

  defmodule MyGenServer do
    use GenServer

    def init(args) do
      {:ok, args}
    end

    def handle_cast(:error, _) do
      raise "oops"
    end

    def handle_call(:exit, _, _) do
      exit(:oops)
    end

    def handle_call(:error, _, _) do
      raise "oops"
    end

    def handle_call(:error_on_down, {pid, _}, _) do
      mon = Process.monitor(pid)
      assert_receive {:DOWN, ^mon, _, _, _}
      raise "oops"
    end
  end

  defmodule MyGenEvent do
    @behaviour :gen_event

    def init(args) do
      {:ok, args}
    end

    def handle_event(_event, state) do
      {:ok, state}
    end

    def handle_call(:error, _) do
      raise "oops"
    end

    def handle_info(_msg, state) do
      {:ok, state}
    end

    def code_change(_old_vsn, state, _extra) do
      {:ok, state}
    end

    def terminate(_reason, _state) do
      :ok
    end
  end

  defmodule MyBridge do
    @behaviour :supervisor_bridge

    def init(reason) do
      {:ok, pid} = Task.start_link(Kernel, :exit, [reason])
      {:ok, pid, pid}
    end

    def terminate(_reason, pid) do
      Process.exit(pid, :shutdown)
    end
  end

  setup_all do
    backends = Application.get_env(:logger, :backends)
    sasl_reports? = Application.get_env(:logger, :handle_sasl_reports, false)

    # Configure backend specific for tests to assert on metadata
    # We could rely exclusively on this backend and skip the console one
    # but using capture_log+console is desired as an integration test
    Application.put_env(:logger, :backends, [Logger.TestBackend | backends])
    Application.put_env(:logger, :handle_sasl_reports, true)

    # Shutdown the application
    Logger.App.stop()

    # And start it without warnings
    Application.put_env(:logger, :level, :error)
    Application.start(:logger)
    Application.delete_env(:logger, :level)
    Logger.configure(level: :debug)

    on_exit(fn ->
      Application.put_env(:logger, :backends, backends)
      Application.put_env(:logger, :handle_sasl_reports, sasl_reports?)
      Logger.App.stop()
      Application.start(:logger)
    end)
  end

  setup do
    Logger.configure_backend(Logger.TestBackend, callback_pid: self())
  end

  test "translates GenServer crashes" do
    {:ok, pid} = GenServer.start(MyGenServer, :ok)

    assert capture_log(:info, fn ->
             catch_exit(GenServer.call(pid, :error))
           end) =~ """
           [error] GenServer #{inspect(pid)} terminating
           ** (RuntimeError) oops
           """

    assert_receive {:error, _pid, {Logger, ["GenServer " <> _ | _], _ts, gen_server_metadata}}
    assert_receive {:error, _pid, {Logger, ["Process " | _], _ts, process_metadata}}

    assert {%RuntimeError{message: "oops"}, [_ | _]} = gen_server_metadata[:crash_reason]
    assert {%RuntimeError{message: "oops"}, [_ | _]} = process_metadata[:crash_reason]

    refute Keyword.has_key?(gen_server_metadata, :initial_call)
    assert process_metadata[:initial_call] == {Logger.TranslatorTest.MyGenServer, :init, 1}

    refute Keyword.has_key?(gen_server_metadata, :registered_name)
    refute Keyword.has_key?(process_metadata, :registered_name)
  end

  test "translates GenServer crashes with local name", config do
    {:ok, pid} = GenServer.start(MyGenServer, :ok, name: config.test)
    capture_log(:info, fn -> catch_exit(GenServer.call(pid, :error)) end)

    assert_receive {:error, _pid, {Logger, ["GenServer " <> _ | _], _ts, gen_server_metadata}}
    assert_receive {:error, _pid, {Logger, ["Process " | _], _ts, process_metadata}}

    assert gen_server_metadata[:registered_name] == config.test
    assert process_metadata[:registered_name] == config.test
  end

  test "translates GenServer crashes with global name", config do
    {:ok, pid} = GenServer.start(MyGenServer, :ok, name: {:global, config.test})
    capture_log(:info, fn -> catch_exit(GenServer.call(pid, :error)) end)

    assert_receive {:error, _pid, {Logger, ["GenServer " <> _ | _], _ts, gen_server_metadata}}
    assert_receive {:error, _pid, {Logger, ["Process " | _], _ts, process_metadata}}

    assert gen_server_metadata[:registered_name] == config.test
    refute Keyword.has_key?(process_metadata, :registered_name)
  end

  test "translates GenServer crashes with custom inspect options" do
    {:ok, pid} = GenServer.start(MyGenServer, List.duplicate(:ok, 1000))
    Application.put_env(:logger, :translator_inspect_opts, limit: 3)

    assert capture_log(:debug, fn ->
             catch_exit(GenServer.call(pid, :error))
           end) =~ """
           [:ok, :ok, :ok, ...]
           """
  after
    Application.put_env(:logger, :translator_inspect_opts, [])
  end

  test "translates GenServer crashes on debug" do
    {:ok, pid} = GenServer.start(MyGenServer, :ok)

    assert capture_log(:debug, fn ->
             catch_exit(GenServer.call(pid, :error))
           end) =~ ~r"""
           \[error\] GenServer #PID<\d+\.\d+\.\d+> terminating
           \*\* \(RuntimeError\) oops
           .*
           Last message \(from #PID<\d+\.\d+\.\d+>\): :error
           State: :ok
           Client #PID<\d+\.\d+\.\d+> is alive
           .*
           """s

    assert_receive {:error, _pid,
                    {Logger, [["GenServer " <> _ | _] | _], _ts, gen_server_metadata}}

    assert_receive {:error, _pid, {Logger, ["Process " | _], _ts, process_metadata}}

    assert {%RuntimeError{message: "oops"}, [_ | _]} = gen_server_metadata[:crash_reason]

    assert {%RuntimeError{message: "oops"}, [_ | _]} = process_metadata[:crash_reason]
  end

  test "translates GenServer crashes with named client on debug" do
    {:ok, pid} = GenServer.start(MyGenServer, :ok)

    assert capture_log(:debug, fn ->
             Process.register(self(), :named_client)
             catch_exit(GenServer.call(pid, :error))
           end) =~ ~r"""
           \[error\] GenServer #PID<\d+\.\d+\.\d+> terminating
           \*\* \(RuntimeError\) oops
           .*
           Last message \(from :named_client\): :error
           State: :ok
           Client :named_client is alive
           .*
           """s
  end

  test "translates GenServer crashes with dead client on debug" do
    {:ok, pid} = GenServer.start(MyGenServer, :ok)

    assert capture_log(:debug, fn ->
             mon = Process.monitor(pid)

             spawn_link(fn ->
               catch_exit(GenServer.call(pid, :error_on_down, 0))
             end)

             assert_receive {:DOWN, ^mon, _, _, _}
           end) =~ ~r"""
           \[error\] GenServer #PID<\d+\.\d+\.\d+> terminating
           \*\* \(RuntimeError\) oops
           .*
           Last message \(from #PID<\d+\.\d+\.\d+>\): :error_on_down
           State: :ok
           Client #PID<\d+\.\d+\.\d+> is dead
           """s
  end

  test "translates GenServer crashes with no client" do
    {:ok, pid} = GenServer.start(MyGenServer, :ok)

    assert capture_log(:debug, fn ->
             mon = Process.monitor(pid)
             GenServer.cast(pid, :error)
             assert_receive {:DOWN, ^mon, _, _, _}
           end) =~ ~r"""
           \[error\] GenServer #PID<\d+\.\d+\.\d+> terminating
           \*\* \(RuntimeError\) oops
           .*
           Last message: {:"\$gen_cast", :error}
           State: :ok
           """s
  end

  test "translates GenServer crashes with no client on debug" do
    {:ok, pid} = GenServer.start(MyGenServer, :ok)

    refute capture_log(:debug, fn ->
             mon = Process.monitor(pid)
             GenServer.cast(pid, :error)
             assert_receive {:DOWN, ^mon, _, _, _}
           end) =~ "Client"

    assert_receive {:error, _pid,
                    {Logger, [["GenServer " <> _ | _] | _], _ts, _gen_server_metadata}}

    assert_receive {:error, _pid, {Logger, ["Process " | _], _ts, _process_metadata}}
  end

  test "translates :gen_event crashes" do
    {:ok, pid} = :gen_event.start()
    :ok = :gen_event.add_handler(pid, MyGenEvent, :ok)

    assert capture_log(:info, fn ->
             :gen_event.call(pid, MyGenEvent, :error)
           end) =~ """
           [error] :gen_event handler Logger.TranslatorTest.MyGenEvent \
           installed in #{inspect(pid)} terminating
           ** (RuntimeError) oops
           """

    assert_receive {:error, _pid, {Logger, [":gen_event handler " <> _ | _], _ts, metadata}}
    assert {%RuntimeError{message: "oops"}, [_ | _]} = metadata[:crash_reason]
    refute Keyword.has_key?(metadata, :initial_call)
    refute Keyword.has_key?(metadata, :registered_name)
  end

  test "translates :gen_event crashes with name", config do
    {:ok, pid} = :gen_event.start({:local, config.test})
    :ok = :gen_event.add_handler(pid, MyGenEvent, :ok)

    assert capture_log(:info, fn ->
             :gen_event.call(pid, MyGenEvent, :error)
           end) =~ """
           [error] :gen_event handler Logger.TranslatorTest.MyGenEvent \
           installed in #{inspect(config.test)} terminating
           ** (RuntimeError) oops
           """

    assert_receive {:error, _pid, {Logger, [":gen_event handler " <> _ | _], _ts, metadata}}
    assert {%RuntimeError{message: "oops"}, [_ | _]} = metadata[:crash_reason]
    assert metadata[:registered_name] == config.test
  end

  test "translates :gen_event crashes on debug" do
    {:ok, pid} = :gen_event.start()
    :ok = :gen_event.add_handler(pid, MyGenEvent, :ok)

    assert capture_log(:debug, fn ->
             :gen_event.call(pid, MyGenEvent, :error)
           end) =~ ~r"""
           \[error\] :gen_event handler Logger.TranslatorTest.MyGenEvent installed in #PID<\d+\.\d+\.\d+> terminating
           \*\* \(RuntimeError\) oops
           .*
           Last message: :error
           State: :ok
           """s

    assert_receive {:error, _pid, {Logger, [[":gen_event handler " <> _ | _] | _], _ts, metadata}}
    assert {%RuntimeError{message: "oops"}, [_ | _]} = metadata[:crash_reason]
  end

  test "translates Task crashes" do
    {:ok, pid} = Task.start_link(__MODULE__, :task, [self()])
    parent = self()

    assert capture_log(fn ->
             ref = Process.monitor(pid)
             send(pid, :go)
             receive do: ({:DOWN, ^ref, _, _, _} -> :ok)
           end) =~ ~r"""
           \[error\] Task #{inspect(pid)} started from #{inspect(self())} terminating
           \*\* \(RuntimeError\) oops
           .*
           Function: &Logger.TranslatorTest.task\/1
               Args: \[#PID<\d+\.\d+\.\d+>\]
           """s

    assert_receive {:error, _pid, {Logger, ["Task " <> _ | _], _ts, task_metadata}}
    assert_receive {:error, _pid, {Logger, ["Process " | _], _ts, process_metadata}}

    assert {%RuntimeError{message: "oops"}, [_ | _]} = task_metadata[:crash_reason]
    assert {%RuntimeError{message: "oops"}, [_ | _]} = process_metadata[:crash_reason]
    assert [parent] == task_metadata[:callers]

    refute Keyword.has_key?(task_metadata, :initial_call)
    assert process_metadata[:initial_call] == {Logger.TranslatorTest, :task, 1}
  end

  test "translates Task async_stream crashes with neighbour" do
    fun = fn -> Task.async_stream([:oops], :erlang, :error, []) |> Enum.to_list() end
    {:ok, pid} = Task.start(__MODULE__, :task, [self(), fun])
    parent = self()

    assert capture_log(:debug, fn ->
             ref = Process.monitor(pid)
             send(pid, :go)
             receive do: ({:DOWN, ^ref, _, _, _} -> :ok)
           end) =~ ~r"""
           Neighbours:
               #{inspect(pid)}
                   Initial Call: Logger\.TranslatorTest\.task/2
           """

    assert_receive {:error, _pid, {Logger, ["Task " <> _ | _], _ts, task_metadata}}
    assert_receive {:error, _pid, {Logger, ["Process " | _], _ts, process_metadata}}
    assert [pid, parent] == task_metadata[:callers]

    assert {:oops, [_ | _]} = task_metadata[:crash_reason]
    assert {%ErlangError{original: :oops}, [_ | _]} = process_metadata[:crash_reason]
  end

  test "translates Task undef module crash" do
    parent = self()

    assert capture_log(fn ->
             {:ok, pid} = Task.start(:module_does_not_exist, :undef, [])
             ref = Process.monitor(pid)
             receive do: ({:DOWN, ^ref, _, _, _} -> :ok)
           end) =~ ~r"""
           \[error\] Task #PID<\d+\.\d+\.\d+> started from #PID<\d+\.\d+\.\d+> terminating
           \*\* \(UndefinedFunctionError\) function :module_does_not_exist.undef/0 is undefined \(module :module_does_not_exist is not available\)
           .*
           Function: &:module_does_not_exist.undef/0
               Args: \[\]
           """s

    assert_receive {:error, _pid, {Logger, ["Task " <> _ | _], _ts, task_metadata}}
    assert_receive {:error, _pid, {Logger, ["Process " | _], _ts, process_metadata}}
    assert [parent] == task_metadata[:callers]

    assert {%UndefinedFunctionError{function: :undef}, [_ | _]} = task_metadata[:crash_reason]
    assert {%UndefinedFunctionError{function: :undef}, [_ | _]} = process_metadata[:crash_reason]
  end

  test "translates Task undef function crash" do
    parent = self()

    assert capture_log(fn ->
             {:ok, pid} = Task.start(__MODULE__, :undef, [])
             ref = Process.monitor(pid)
             receive do: ({:DOWN, ^ref, _, _, _} -> :ok)
           end) =~ ~r"""
           \[error\] Task #PID<\d+\.\d+\.\d+> started from #PID<\d+\.\d+\.\d+> terminating
           \*\* \(UndefinedFunctionError\) function Logger.TranslatorTest.undef/0 is undefined or private
           .*
           Function: &Logger.TranslatorTest.undef/0
               Args: \[\]
           """s

    assert_receive {:error, _pid, {Logger, ["Task " <> _ | _], _ts, task_metadata}}
    assert_receive {:error, _pid, {Logger, ["Process " | _], _ts, process_metadata}}
    assert [parent] == task_metadata[:callers]

    assert {%UndefinedFunctionError{function: :undef}, [_ | _]} = task_metadata[:crash_reason]
    assert {%UndefinedFunctionError{function: :undef}, [_ | _]} = process_metadata[:crash_reason]
  end

  test "translates Task raising ErlangError" do
    parent = self()

    assert capture_log(fn ->
             exception =
               try do
                 :erlang.error(:foo)
               rescue
                 x ->
                   x
               end

             {:ok, pid} = Task.start(:erlang, :error, [exception])
             ref = Process.monitor(pid)
             receive do: ({:DOWN, ^ref, _, _, _} -> :ok)
           end) =~ ~r"""
           \[error\] Task #PID<\d+\.\d+\.\d+> started from #PID<\d+\.\d+\.\d+> terminating
           \*\* \(ErlangError\) Erlang error: :foo
           .*
           Function: &:erlang\.error/1
               Args: \[%ErlangError{.*}\]
           """s

    assert_receive {:error, _pid, {Logger, ["Task " <> _ | _], _ts, task_metadata}}
    assert_receive {:error, _pid, {Logger, ["Process " | _], _ts, process_metadata}}
    assert [parent] == task_metadata[:callers]

    assert {%ErlangError{original: :foo}, [_ | _]} = task_metadata[:crash_reason]
    assert {%ErlangError{original: :foo}, [_ | _]} = process_metadata[:crash_reason]
  end

  test "translates Task raising Erlang badarg error" do
    parent = self()

    assert capture_log(fn ->
             {:ok, pid} = Task.start(:erlang, :error, [:badarg])
             ref = Process.monitor(pid)
             receive do: ({:DOWN, ^ref, _, _, _} -> :ok)
           end) =~ ~r"""
           \[error\] Task #PID<\d+\.\d+\.\d+> started from #PID<\d+\.\d+\.\d+> terminating
           \*\* \(ArgumentError\) argument error
           .*
           Function: &:erlang\.error/1
               Args: \[:badarg\]
           """s

    assert_receive {:error, _pid, {Logger, ["Task " <> _ | _], _ts, task_metadata}}
    assert_receive {:error, _pid, {Logger, ["Process " | _], _ts, process_metadata}}
    assert [parent] == task_metadata[:callers]

    assert {%ArgumentError{message: "argument error"}, [_ | _]} = task_metadata[:crash_reason]
    assert {%ArgumentError{message: "argument error"}, [_ | _]} = process_metadata[:crash_reason]
  end

  test "translates Task exiting abnormally" do
    parent = self()

    assert capture_log(fn ->
             {:ok, pid} = Task.start(:erlang, :exit, [:abnormal])
             ref = Process.monitor(pid)
             receive do: ({:DOWN, ^ref, _, _, _} -> :ok)
           end) =~ ~r"""
           \[error\] Task #PID<\d+\.\d+\.\d+> started from #PID<\d+\.\d+\.\d+> terminating
           \*\* \(stop\) :abnormal
           .*
           Function: &:erlang\.exit/1
               Args: \[:abnormal\]
           """s

    assert_receive {:error, _pid, {Logger, ["Task " <> _ | _], _ts, task_metadata}}
    assert_receive {:error, _pid, {Logger, ["Process " | _], _ts, process_metadata}}
    assert [parent] == task_metadata[:callers]

    assert {:abnormal, [_ | _]} = task_metadata[:crash_reason]
    assert {:abnormal, [_ | _]} = process_metadata[:crash_reason]
  end

  test "translates application start" do
    assert capture_log(fn ->
             Application.start(:eex)
             Application.stop(:eex)
           end) =~ """
           Application eex started at #{inspect(node())}
           """
  end

  test "translates application stop" do
    assert capture_log(fn ->
             :ok = Application.start(:eex)
             Application.stop(:eex)
           end) =~ """
           Application eex exited: :stopped
           """
  end

  test "translates bare process crashes" do
    assert capture_log(:info, fn ->
             {_, ref} = spawn_monitor(fn -> raise "oops" end)
             receive do: ({:DOWN, ^ref, _, _, _} -> :ok)
             # Even though the monitor has been received the emulator may not have
             # sent the message to the error logger
             Process.sleep(200)
           end) =~ ~r"""
           \[error\] Process #PID<\d+\.\d+\.\d+>\ raised an exception
           \*\* \(RuntimeError\) oops
           """

    assert_receive {:error, _pid, {Logger, ["Process " | _], _ts, process_metadata}}
    assert {%RuntimeError{message: "oops"}, [_ | _]} = process_metadata[:crash_reason]
  end

  test "translates :proc_lib crashes" do
    fun = fn ->
      Logger.metadata(foo: :bar)
      raise "oops"
    end

    pid = :proc_lib.spawn_link(__MODULE__, :task, [self(), fun])

    assert capture_log(:info, fn ->
             ref = Process.monitor(pid)
             send(pid, :go)
             receive do: ({:DOWN, ^ref, _, _, _} -> :ok)
           end) =~ ~r"""
           \[error\] Process #PID<\d+\.\d+\.\d+> terminating
           \*\* \(RuntimeError\) oops
           .*
           Initial Call: Logger.TranslatorTest.task/2
           Ancestors: \[#PID<\d+\.\d+\.\d+>\]
           """s

    assert_receive {:error, _pid, {Logger, ["Process " | _], _ts, process_metadata}}

    assert is_pid(process_metadata[:pid])
    assert is_list(process_metadata[:ancestors])
    assert process_metadata[:foo] == :bar
    assert {%RuntimeError{message: "oops"}, [_ | _]} = process_metadata[:crash_reason]
  end

  test "skips :proc_lib crashes with disabled metadata" do
    fun = fn ->
      Logger.disable(self())
      raise "oops"
    end

    pid = :proc_lib.spawn_link(__MODULE__, :task, [self(), fun])

    assert capture_log(:info, fn ->
             ref = Process.monitor(pid)
             send(pid, :go)
             receive do: ({:DOWN, ^ref, _, _, _} -> :ok)
           end) == ""
  end

  test "translates :proc_lib crashes with name" do
    fun = fn ->
      Process.register(self(), __MODULE__)
      raise "oops"
    end

    pid = :proc_lib.spawn_link(__MODULE__, :task, [self(), fun])

    assert capture_log(:info, fn ->
             ref = Process.monitor(pid)
             send(pid, :message)
             send(pid, :go)
             receive do: ({:DOWN, ^ref, _, _, _} -> :ok)
           end) =~ ~r"""
           \[error\] Process Logger.TranslatorTest \(#PID<\d+\.\d+\.\d+>\) terminating
           \*\* \(RuntimeError\) oops
           .*
           Initial Call: Logger.TranslatorTest.task/2
           Ancestors: \[#PID<\d+\.\d+\.\d+>\]
           """s

    assert_receive {:error, _pid, {Logger, ["Process " | _], _ts, process_metadata}}
    assert {%RuntimeError{message: "oops"}, [_ | _]} = process_metadata[:crash_reason]
  end

  test "translates :proc_lib crashes without initial call" do
    fun = fn ->
      Process.delete(:"$initial_call")
      raise "oops"
    end

    pid = :proc_lib.spawn_link(__MODULE__, :task, [self(), fun])

    assert capture_log(:info, fn ->
             ref = Process.monitor(pid)
             send(pid, :message)
             send(pid, :go)
             receive do: ({:DOWN, ^ref, _, _, _} -> :ok)
           end) =~ ~r"""
           \[error\] Process #PID<\d+\.\d+\.\d+> terminating
           \*\* \(RuntimeError\) oops
           .*
           Ancestors: \[#PID<\d+\.\d+\.\d+>\]
           """s

    assert_receive {:error, _pid, {Logger, ["Process " | _], _ts, process_metadata}}
    assert {%RuntimeError{message: "oops"}, [_ | _]} = process_metadata[:crash_reason]
  end

  test "translates :proc_lib crashes with neighbour" do
    {:ok, pid} = Task.start_link(__MODULE__, :sub_task, [self()])

    assert capture_log(:info, fn ->
             ref = Process.monitor(pid)
             send(pid, :message)
             send(pid, :go)
             receive do: ({:DOWN, ^ref, _, _, _} -> :ok)
           end) =~ ~r"""
           Ancestors: \[#PID<\d+\.\d+\.\d+>\]
           Neighbours:
               #PID<\d+\.\d+\.\d+>
                   Initial Call: Logger.TranslatorTest.sleep/1
                   Current Call: Logger.TranslatorTest.sleep/1
                   Ancestors: \[#PID<\d+\.\d+\.\d+>, #PID<\d+\.\d+\.\d+>\]
           """
  end

  test "translates :proc_lib crashes with neighbour with name" do
    fun = fn pid2 ->
      Process.register(pid2, __MODULE__)
      raise "oops"
    end

    {:ok, pid} = Task.start_link(__MODULE__, :sub_task, [self(), fun])

    assert capture_log(:info, fn ->
             ref = Process.monitor(pid)
             send(pid, :message)
             send(pid, :go)
             receive do: ({:DOWN, ^ref, _, _, _} -> :ok)
           end) =~ ~r"""
           Ancestors: \[#PID<\d+\.\d+\.\d+>\]
           Neighbours:
               Logger.TranslatorTest \(#PID<\d+\.\d+\.\d+>\)
                   Initial Call: Logger.TranslatorTest.sleep/1
                   Current Call: Logger.TranslatorTest.sleep/1
                   Ancestors: \[#PID<\d+\.\d+\.\d+>, #PID<\d+\.\d+\.\d+>\]
           """
  end

  test "translates :proc_lib+Task crashes on debug" do
    {:ok, pid} = Task.start_link(__MODULE__, :task, [self()])
    parent = self()

    assert capture_log(:debug, fn ->
             ref = Process.monitor(pid)
             send(pid, :message)
             send(pid, :go)
             receive do: ({:DOWN, ^ref, _, _, _} -> :ok)
           end) =~ ~r"""
           Ancestors: \[#PID<\d+\.\d+\.\d+>\]
           Message Queue Length: 1
           Messages: \[:message\]
           Links: \[\]
           Dictionary: \["\$callers": \[#PID<\d+\.\d+\.\d+>\]\]
           Trapping Exits: false
           Status: :running
           Heap Size: \d+
           Stack Size: \d+
           Reductions: \d+
           """

    assert_receive {:error, _pid, {Logger, ["Task " <> _ | _], _ts, task_metadata}}
    assert_receive {:error, _pid, {Logger, ["Process " | _], _ts, process_metadata}}

    assert process_metadata[:pid] == task_metadata[:pid]
    assert is_list(process_metadata[:callers])
    assert is_list(process_metadata[:ancestors])
    assert [parent] == task_metadata[:callers]

    assert {%RuntimeError{message: "oops"}, [_ | _]} = task_metadata[:crash_reason]
    assert {%RuntimeError{message: "oops"}, [_ | _]} = process_metadata[:crash_reason]
  end

  test "translates :proc_lib+Task crashes with neighbour on debug" do
    {:ok, pid} = Task.start_link(__MODULE__, :sub_task, [self()])
    parent = self()

    assert capture_log(:debug, fn ->
             ref = Process.monitor(pid)
             send(pid, :message)
             send(pid, :go)
             receive do: ({:DOWN, ^ref, _, _, _} -> :ok)
           end) =~ ~r"""
                   Ancestors: \[#PID<\d+\.\d+\.\d+>, #PID<\d+\.\d+\.\d+>\]
                   Message Queue Length: 0
                   Links: \[#PID<\d+\.\d+\.\d+>\]
                   Trapping Exits: false
                   Status: :waiting
                   Heap Size: \d+
                   Stack Size: \d+
                   Reductions: \d+
                   Current Stacktrace:
                       (lib/logger/)?test/logger/translator_test.exs:\d+: Logger.TranslatorTest.sleep/1
           """

    assert_receive {:error, _pid, {Logger, ["Task " <> _ | _], _ts, task_metadata}}
    assert [parent] == task_metadata[:callers]
  end

  test "translates Supervisor progress" do
    {:ok, pid} = Supervisor.start_link([], strategy: :one_for_one)

    assert capture_log(:info, fn ->
             ref = Process.monitor(pid)
             Supervisor.start_child(pid, worker(Task, [__MODULE__, :sleep, [self()]]))
             Process.exit(pid, :normal)
             receive do: ({:DOWN, ^ref, _, _, _} -> :ok)
           end) =~ ~r"""
           \[info\] Child Task of Supervisor #PID<\d+\.\d+\.\d+> \(Supervisor\.Default\) started
           Pid: #PID<\d+\.\d+\.\d+>
           Start Call: Task.start_link\(Logger.TranslatorTest, :sleep, \[#PID<\d+\.\d+\.\d+>\]\)
           """
  end

  test "translates Supervisor progress with name" do
    {:ok, pid} = Supervisor.start_link([], strategy: :one_for_one, name: __MODULE__)

    assert capture_log(:info, fn ->
             ref = Process.monitor(pid)
             Supervisor.start_child(pid, worker(Task, [__MODULE__, :sleep, [self()]]))
             Process.exit(pid, :normal)
             receive do: ({:DOWN, ^ref, _, _, _} -> :ok)
           end) =~ ~r"""
           \[info\] Child Task of Supervisor Logger.TranslatorTest started
           """

    {:ok, pid} = Supervisor.start_link([], strategy: :one_for_one, name: {:global, __MODULE__})

    assert capture_log(:info, fn ->
             ref = Process.monitor(pid)
             Supervisor.start_child(pid, worker(Task, [__MODULE__, :sleep, [self()]]))
             Process.exit(pid, :normal)
             receive do: ({:DOWN, ^ref, _, _, _} -> :ok)
           end) =~ ~r"""
           \[info\] Child Task of Supervisor Logger.TranslatorTest started
           """

    {:ok, pid} =
      Supervisor.start_link([], strategy: :one_for_one, name: {:via, :global, __MODULE__})

    assert capture_log(:info, fn ->
             ref = Process.monitor(pid)
             Supervisor.start_child(pid, worker(Task, [__MODULE__, :sleep, [self()]]))
             Process.exit(pid, :normal)
             receive do: ({:DOWN, ^ref, _, _, _} -> :ok)
           end) =~ ~r"""
           \[info\] Child Task of Supervisor Logger.TranslatorTest started
           """
  end

  test "translates Supervisor progress on debug" do
    {:ok, pid} = Supervisor.start_link([], strategy: :one_for_one)

    assert capture_log(:debug, fn ->
             ref = Process.monitor(pid)
             Supervisor.start_child(pid, worker(Task, [__MODULE__, :sleep, [self()]]))
             Process.exit(pid, :normal)
             receive do: ({:DOWN, ^ref, _, _, _} -> :ok)
           end) =~ ~r"""
           Start Call: Task.start_link\(Logger.TranslatorTest, :sleep, \[#PID<\d+\.\d+\.\d+>\]\)
           Restart: :permanent
           Shutdown: 5000
           Type: :worker
           """
  end

  test "translates Supervisor reports start error" do
    assert capture_log(:info, fn ->
             trap = Process.flag(:trap_exit, true)
             children = [worker(__MODULE__, [], function: :error)]
             Supervisor.start_link(children, strategy: :one_for_one)
             receive do: ({:EXIT, _, {:shutdown, {:failed_to_start_child, _, _}}} -> :ok)
             Process.flag(:trap_exit, trap)
           end) =~ ~r"""
           \[error\] Child Logger.TranslatorTest of Supervisor #PID<\d+\.\d+\.\d+> \(Supervisor\.Default\) failed to start
           \*\* \(exit\) :stop
           Start Call: Logger.TranslatorTest.error\(\)
           """
  end

  test "translates Supervisor reports start error with raise" do
    assert capture_log(:info, fn ->
             trap = Process.flag(:trap_exit, true)
             children = [worker(__MODULE__, [], function: :undef)]
             Supervisor.start_link(children, strategy: :one_for_one)
             receive do: ({:EXIT, _, {:shutdown, {:failed_to_start_child, _, _}}} -> :ok)
             Process.flag(:trap_exit, trap)
           end) =~ ~r"""
           \[error\] Child Logger.TranslatorTest of Supervisor #PID<\d+\.\d+\.\d+> \(Supervisor\.Default\) failed to start
           \*\* \(exit\) an exception was raised:
               \*\* \(UndefinedFunctionError\) function Logger.TranslatorTest.undef/0 is undefined or private
               .*
           Start Call: Logger.TranslatorTest.undef\(\)
           """s

    assert_receive {:error, _pid, {Logger, ["Child " | _], _ts, _child_metadata}}
  end

  test "translates Supervisor reports terminated" do
    assert capture_log(:info, fn ->
             trap = Process.flag(:trap_exit, true)
             children = [worker(Task, [Kernel, :exit, [:stop]])]
             {:ok, pid} = Supervisor.start_link(children, strategy: :one_for_one, max_restarts: 0)
             receive do: ({:EXIT, ^pid, _} -> :ok)
             Process.flag(:trap_exit, trap)
           end) =~ ~r"""
           \[error\] Child Task of Supervisor #PID<\d+\.\d+\.\d+> \(Supervisor\.Default\) terminated
           \*\* \(exit\) :stop
           Pid: #PID<\d+\.\d+\.\d+>
           Start Call: Task.start_link\(Kernel, :exit, \[:stop\]\)
           """

    assert_receive {:error, _pid, {Logger, ["Task " <> _ | _], _ts, task_metadata}}
    assert_receive {:error, _pid, {Logger, ["Process " | _], _ts, process_metadata}}
    assert_receive {:error, _pid, {Logger, ["Child " | _], _ts, _child_metadata}}
    assert_receive {:error, _pid, {Logger, ["Child ", "Task" | _], _ts, _child_task_metadata}}

    assert {:stop, [_ | _]} = task_metadata[:crash_reason]
    assert {:stop, [_ | _]} = process_metadata[:crash_reason]
  end

  test "translates Supervisor reports max restarts shutdown" do
    assert capture_log(:info, fn ->
             trap = Process.flag(:trap_exit, true)
             children = [worker(Task, [Kernel, :exit, [:stop]])]
             {:ok, pid} = Supervisor.start_link(children, strategy: :one_for_one, max_restarts: 0)
             receive do: ({:EXIT, ^pid, _} -> :ok)
             Process.flag(:trap_exit, trap)
           end) =~ ~r"""
           \[error\] Child Task of Supervisor #PID<\d+\.\d+\.\d+> \(Supervisor\.Default\) caused shutdown
           \*\* \(exit\) :reached_max_restart_intensity
           Start Call: Task.start_link\(Kernel, :exit, \[:stop\]\)
           """

    assert_receive {:error, _pid, {Logger, ["Task " <> _ | _], _ts, task_metadata}}
    assert_receive {:error, _pid, {Logger, ["Process " | _], _ts, process_metadata}}
    assert_receive {:error, _pid, {Logger, ["Child " | _], _ts, _child_metadata}}
    assert_receive {:error, _pid, {Logger, ["Child ", "Task" | _], _ts, _child_task_metadata}}

    assert {:stop, [_ | _]} = task_metadata[:crash_reason]
    assert {:stop, [_ | _]} = process_metadata[:crash_reason]
  end

  test "translates Supervisor reports abnormal shutdown" do
    assert capture_log(:info, fn ->
             children = [worker(__MODULE__, [], function: :abnormal)]
             {:ok, pid} = Supervisor.start_link(children, strategy: :one_for_one)
             :ok = Supervisor.terminate_child(pid, __MODULE__)
           end) =~ ~r"""
           \[error\] Child Logger.TranslatorTest of Supervisor #PID<\d+\.\d+\.\d+> \(Supervisor\.Default\) shut down abnormally
           \*\* \(exit\) :stop
           Pid: #PID<\d+\.\d+\.\d+>
           Start Call: Logger.TranslatorTest.abnormal\(\)
           """

    assert_receive {:error, _pid, {Logger, ["Process " | _], _ts, process_metadata}}
    assert_receive {:error, _pid, {Logger, ["Child " | _], _ts, _child_metadata}}
    assert {:stop, [_ | _]} = process_metadata[:crash_reason]
  end

  test "translates Supervisor reports abnormal shutdown on debug" do
    assert capture_log(:debug, fn ->
             children = [
               worker(__MODULE__, [], function: :abnormal, restart: :permanent, shutdown: 5000)
             ]

             {:ok, pid} = Supervisor.start_link(children, strategy: :one_for_one)
             :ok = Supervisor.terminate_child(pid, __MODULE__)
           end) =~ ~r"""
           \*\* \(exit\) :stop
           Pid: #PID<\d+\.\d+\.\d+>
           Start Call: Logger.TranslatorTest.abnormal\(\)
           Restart: :permanent
           Shutdown: 5000
           Type: :worker
           """

    assert_receive {:error, _pid, {Logger, ["Process " | _], _ts, process_metadata}}
    assert_receive {:error, _pid, {Logger, ["Child " | _], _ts, _child_metadata}}
    assert {:stop, [_ | _]} = process_metadata[:crash_reason]
  end

  test "translates DynamicSupervisor reports abnormal shutdown" do
    assert capture_log(:info, fn ->
             trap = Process.flag(:trap_exit, true)
             child = %{id: __MODULE__, start: {__MODULE__, :abnormal, []}}
             {:ok, pid} = DynamicSupervisor.start_link(strategy: :one_for_one)
             {:ok, _pid2} = DynamicSupervisor.start_child(pid, child)
             Process.exit(pid, :normal)
             receive do: ({:EXIT, ^pid, _} -> :ok)
             Process.flag(:trap_exit, trap)
           end) =~ ~r"""
           \[error\] Child :undefined of Supervisor #PID<\d+\.\d+\.\d+> \(Supervisor\.Default\) shut down abnormally
           \*\* \(exit\) :stop
           Pid: #PID<\d+\.\d+\.\d+>
           Start Call: Logger.TranslatorTest.abnormal\(\)
           """

    assert_receive {:error, _pid, {Logger, ["Process " | _], _ts, process_metadata}}
    assert_receive {:error, _pid, {Logger, ["Child " | _], _ts, _child_metadata}}
    assert {:stop, [_ | _]} = process_metadata[:crash_reason]
  end

  test "translates DynamicSupervisor reports abnormal shutdown including extra_arguments" do
    assert capture_log(:info, fn ->
             trap = Process.flag(:trap_exit, true)

             {:ok, pid} =
               DynamicSupervisor.start_link(strategy: :one_for_one, extra_arguments: [:extra])

             child = %{id: __MODULE__, start: {__MODULE__, :abnormal, [:args]}}
             {:ok, _pid2} = DynamicSupervisor.start_child(pid, child)
             Process.exit(pid, :normal)
             receive do: ({:EXIT, ^pid, _} -> :ok)
             Process.flag(:trap_exit, trap)
           end) =~ ~r"""
           \[error\] Child :undefined of Supervisor #PID<\d+\.\d+\.\d+> \(Supervisor\.Default\) shut down abnormally
           \*\* \(exit\) :stop
           Pid: #PID<\d+\.\d+\.\d+>
           Start Call: Logger.TranslatorTest.abnormal\(:extra, :args\)
           """

    assert_receive {:error, _pid, {Logger, ["Process " | _], _ts, process_metadata}}
    assert_receive {:error, _pid, {Logger, ["Child " | _], _ts, _child_metadata}}
    assert {:stop, [_ | _]} = process_metadata[:crash_reason]
  end

  test "translates named DynamicSupervisor reports abnormal shutdown" do
    assert capture_log(:info, fn ->
             trap = Process.flag(:trap_exit, true)
             child = %{id: __MODULE__, start: {__MODULE__, :abnormal, []}}
             {:ok, pid} = DynamicSupervisor.start_link(strategy: :one_for_one, name: __MODULE__)
             {:ok, _pid2} = DynamicSupervisor.start_child(pid, child)
             Process.exit(pid, :normal)
             receive do: ({:EXIT, ^pid, _} -> :ok)
             Process.flag(:trap_exit, trap)
           end) =~ ~r"""
           \[error\] Child :undefined of Supervisor Logger.TranslatorTest shut down abnormally
           \*\* \(exit\) :stop
           Pid: #PID<\d+\.\d+\.\d+>
           Start Call: Logger.TranslatorTest.abnormal\(\)
           """

    assert_receive {:error, _pid, {Logger, ["Process " | _], _ts, process_metadata}}
    assert_receive {:error, _pid, {Logger, ["Child " | _], _ts, _child_metadata}}
    assert {:stop, [_ | _]} = process_metadata[:crash_reason]
  end

  test "translates :supervisor_bridge progress" do
    assert capture_log(:info, fn ->
             trap = Process.flag(:trap_exit, true)
             {:ok, pid} = :supervisor_bridge.start_link(MyBridge, :normal)
             receive do: ({:EXIT, ^pid, _} -> :ok)
             Process.flag(:trap_exit, trap)
           end) =~ ~r"""
           \[info\] Child of Supervisor #PID<\d+\.\d+\.\d+> \(Logger\.TranslatorTest\.MyBridge\) started
           Pid: #PID<\d+\.\d+\.\d+>
           Start Call: Logger.TranslatorTest.MyBridge.init\(:normal\)
           """
  end

  test "translates :supervisor_bridge reports" do
    assert capture_log(:info, fn ->
             trap = Process.flag(:trap_exit, true)
             {:ok, pid} = :supervisor_bridge.start_link(MyBridge, :stop)
             receive do: ({:EXIT, ^pid, _} -> :ok)
             Process.flag(:trap_exit, trap)
           end) =~ ~r"""
           \[error\] Child of Supervisor #PID<\d+\.\d+\.\d+> \(Logger\.TranslatorTest\.MyBridge\) terminated
           \*\* \(exit\) :stop
           Pid: #PID<\d+\.\d+\.\d+>
           Start Module: Logger.TranslatorTest.MyBridge
           """

    assert_receive {:error, _pid, {Logger, ["Task " <> _ | _], _ts, task_metadata}}
    assert_receive {:error, _pid, {Logger, ["Process " | _], _ts, process_metadata}}
    assert_receive {:error, _pid, {Logger, ["Child of Supervisor " | _], _ts, _child_metadata}}

    assert {:stop, [_ | _]} = task_metadata[:crash_reason]
    assert {:stop, [_ | _]} = process_metadata[:crash_reason]
  end

  test "translates process crash with ERTS" do
    assert {:ok, _msg, meta} =
             Logger.Translator.translate(
               :error,
               :error,
               :format,
               {~c"Error in process ~p on node ~p with exit value:~n~p~n",
                [self(), :"name@127.0.0.1", {:badarith, [{:erlang, :/, [1, 0], []}]}]}
             )

    assert Keyword.get(meta, :crash_reason)
  end

  test "reports :undefined MFA properly" do
    defmodule WeirdFunctionNamesGenServer do
      use GenServer

      def unquote(:"start link")(), do: GenServer.start_link(__MODULE__, [])
      def init(args), do: {:ok, args}
      def handle_call(_call, _from, _state), do: raise("oops")
    end

    start = {WeirdFunctionNamesGenServer, :"start link", []}
    child = %{id: :weird, start: start, restart: :temporary}
    {:ok, sup} = DynamicSupervisor.start_link(strategy: :one_for_one)

    log =
      capture_log(:info, fn ->
        {:ok, pid} = DynamicSupervisor.start_child(sup, child)
        catch_exit(GenServer.call(pid, :error))
        [] = DynamicSupervisor.which_children(sup)
      end)

    assert log =~ ~s(Start Call: Logger.TranslatorTest.WeirdFunctionNamesGenServer."start link"/?)
    assert_receive {:error, _pid, {Logger, ["GenServer " <> _ | _], _ts, server_metadata}}
    assert_receive {:error, _pid, {Logger, ["Process " | _], _ts, process_metadata}}
    assert_receive {:error, _pid, {Logger, ["Child " | _], _ts, _child_metadata}}

    assert {%RuntimeError{message: "oops"}, [_ | _]} = server_metadata[:crash_reason]
    assert {%RuntimeError{message: "oops"}, [_ | _]} = process_metadata[:crash_reason]
  after
    :code.purge(WeirdFunctionNamesGenServer)
    :code.delete(WeirdFunctionNamesGenServer)
  end

  def task(parent, fun \\ fn -> raise "oops" end) do
    mon = Process.monitor(parent)
    Process.unlink(parent)

    receive do
      :go ->
        fun.()

      {:DOWN, ^mon, _, _, _} ->
        exit(:shutdown)
    end
  end

  def sub_task(parent, fun \\ fn _ -> raise "oops" end) do
    mon = Process.monitor(parent)
    Process.unlink(parent)
    {:ok, pid} = Task.start_link(__MODULE__, :sleep, [self()])
    receive do: (:sleeping -> :ok)

    receive do
      :go ->
        fun.(pid)

      {:DOWN, ^mon, _, _, _} ->
        exit(:shutdown)
    end
  end

  def sleep(pid) do
    mon = Process.monitor(pid)
    send(pid, :sleeping)
    receive do: ({:DOWN, ^mon, _, _, _} -> exit(:shutdown))
  end

  def error(), do: {:error, :stop}

  def abnormal() do
    :proc_lib.start_link(__MODULE__, :abnormal_init, [])
  end

  def abnormal(:extra, :args) do
    :proc_lib.start_link(__MODULE__, :abnormal_init, [])
  end

  def abnormal_init() do
    Process.flag(:trap_exit, true)
    :proc_lib.init_ack({:ok, self()})
    receive do: ({:EXIT, _, _} -> exit(:stop))
  end

  defp worker(name, args, opts \\ []) do
    Enum.into(opts, %{id: name, start: {name, opts[:function] || :start_link, args}})
  end
end