/
translator.ex
595 lines (471 loc) · 18.7 KB
/
translator.ex
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
defmodule Logger.Translator do
@moduledoc """
Default translation for Erlang log messages.
Logger allows developers to rewrite log messages provided by
OTP applications into a format more compatible with Elixir
log messages by providing a translator.
A translator is simply a tuple containing a module and a function
that can be added and removed via the `Logger.add_translator/1` and
`Logger.remove_translator/1` functions and is invoked for every Erlang
message above the minimum log level with four arguments:
* `min_level` - the current Logger level
* `level` - the level of the message being translated
* `kind` - if the message is a `:report` or `:format`
* `message` - the message to format. If it is `:report`, it is a tuple
with `{report_type, report_data}`, if it is `:format`, it is a
tuple with `{format_message, format_args}`.
The function must return:
* `{:ok, chardata, metadata}` - if the message translation with its metadata
* `{:ok, chardata}` - the translated message
* `:skip` - if the message is not meant to be translated nor logged
* `:none` - if there is no translation, which triggers the next translator
See the function `translate/4` in this module for an example implementation
and the default messages translated by Logger.
"""
@doc """
Built-in translation function.
"""
def translate(min_level, level, kind, message)
## Erlang/OTP 21 and after
def translate(min_level, _level, :report, {:logger, %{label: label} = report}) do
case label do
{:gen_server, :terminate} ->
report_gen_server_terminate(min_level, report)
{:gen_event, :terminate} ->
report_gen_event_terminate(min_level, report)
_ ->
:skip
end
end
def translate(min_level, _level, :report, {{:proc_lib, :crash}, data}) do
report_crash(min_level, data)
end
def translate(min_level, _level, :report, {{:supervisor, :progress}, data}) do
report_supervisor_progress(min_level, data)
end
def translate(min_level, _level, :report, {{:supervisor, _}, data}) do
report_supervisor(min_level, data)
end
def translate(
_min_level,
_level,
:report,
{{:application_controller, :progress}, [application: app, started_at: node]}
) do
{:ok, ["Application ", Atom.to_string(app), " started at " | inspect(node)]}
end
def translate(
_min_level,
_level,
:report,
{{:application_controller, :exit}, [application: app, exited: reason, type: _type]}
) do
{:ok, ["Application ", Atom.to_string(app), " exited: " | Application.format_error(reason)]}
end
## Erlang/OTP 20 and before
# TODO: This clauses can be removed when we support only Erlang/OTP 21+.
def translate(min_level, :error, :format, message) do
opts = Application.get_env(:logger, :translator_inspect_opts)
case message do
{'** Generic server ' ++ _, [name, last, state, reason | client]} ->
{formatted, reason} = format_reason(reason)
metadata = [crash_reason: reason] ++ registered_name(name)
msg =
["GenServer #{inspect(name)} terminating", formatted] ++
["\nLast message#{format_from(client)}: #{inspect(last, opts)}"]
if min_level == :debug do
msg = [msg, "\nState: #{inspect(state, opts)}" | format_client(client)]
{:ok, msg, metadata}
else
{:ok, msg, metadata}
end
{'** gen_event handler ' ++ _, [name, manager, last, state, reason]} ->
{formatted, reason} = format_reason(reason)
metadata = [crash_reason: reason] ++ registered_name(manager)
msg =
[":gen_event handler #{inspect(name)} installed in #{inspect(manager)} terminating"] ++
[formatted, "\nLast message: #{inspect(last, opts)}"]
if min_level == :debug do
{:ok, [msg | "\nState: #{inspect(state, opts)}"], metadata}
else
{:ok, msg, metadata}
end
{'** Task ' ++ _, [name, starter, function, args, reason]} ->
{formatted, reason} = format_reason(reason)
metadata = [crash_reason: reason] ++ registered_name(name)
msg =
["Task #{inspect(name)} started from #{inspect(starter)} terminating"] ++
[formatted, "\nFunction: #{inspect(function, opts)}"] ++
["\n Args: #{inspect(args, opts)}"]
{:ok, msg, metadata}
{'Error in process ' ++ _, [pid, {reason, stack}]} ->
reason = Exception.normalize(:error, reason, stack)
msg = ["Process ", inspect(pid), " raised an exception" | format(:error, reason, stack)]
{:ok, msg, [crash_reason: exit_reason(:error, reason, stack)]}
_ ->
:none
end
end
def translate(_min_level, :info, :report, {
:std_info,
[application: app, exited: reason, type: _type]
}) do
{:ok, ["Application ", Atom.to_string(app), " exited: " | Application.format_error(reason)]}
end
def translate(min_level, :error, :report, {{:error_logger, :error_report}, data}) do
report_supervisor(min_level, data)
end
def translate(min_level, :error, :report, {:supervisor_report, data}) do
report_supervisor(min_level, data)
end
def translate(min_level, :error, :report, {:crash_report, data}) do
report_crash(min_level, data)
end
def translate(min_level, :info, :report, {:progress, [{:supervisor, _} | _] = data}) do
report_supervisor_progress(min_level, data)
end
def translate(_min_level, :info, :report, {:progress, [application: app, started_at: node]}) do
{:ok, ["Application ", Atom.to_string(app), " started at " | inspect(node)]}
end
## Helpers
def translate(_min_level, _level, _kind, _message) do
:none
end
defp report_gen_server_terminate(min_level, report) do
inspect_opts = Application.get_env(:logger, :translator_inspect_opts)
%{
client_info: client,
last_message: last,
name: name,
reason: reason,
state: state
} = report
{formatted, reason} = format_reason(reason)
metadata = [crash_reason: reason] ++ registered_name(name)
msg =
["GenServer ", inspect(name), " terminating", formatted] ++
["\nLast message", format_last_message_from(client), ": ", inspect(last, inspect_opts)]
if min_level == :debug do
msg = [msg, "\nState: ", inspect(state, inspect_opts) | format_client_info(client)]
{:ok, msg, metadata}
else
{:ok, msg, metadata}
end
end
defp report_gen_event_terminate(min_level, report) do
inspect_opts = Application.get_env(:logger, :translator_inspect_opts)
%{
handler: handler,
last_message: last,
name: name,
reason: reason,
state: state
} = report
reason =
case reason do
{:EXIT, why} -> why
_ -> reason
end
{formatted, reason} = format_reason(reason)
metadata = [crash_reason: reason] ++ registered_name(name)
msg =
[":gen_event handler ", inspect(handler), " installed in ", inspect(name), " terminating"] ++
[formatted, "\nLast message: ", inspect(last, inspect_opts)]
if min_level == :debug do
{:ok, [msg, "\nState: ", inspect(state, inspect_opts)], metadata}
else
{:ok, msg, metadata}
end
end
defp report_supervisor_progress(
min_level,
supervisor: sup,
started: [{:pid, pid}, {:id, id} | started]
) do
msg =
["Child ", inspect(id), " of Supervisor ", sup_name(sup), " started"] ++
["\nPid: ", inspect(pid)] ++ child_info(min_level, started)
{:ok, msg}
end
defp report_supervisor_progress(
min_level,
supervisor: sup,
started: [{:pid, pid} | started]
) do
msg =
["Child of Supervisor ", sup_name(sup), " started", "\nPid: ", inspect(pid)] ++
child_info(min_level, started)
{:ok, msg}
end
defp report_supervisor_progress(_min_level, _other), do: :none
defp report_supervisor(
min_level,
supervisor: sup,
errorContext: context,
reason: reason,
offender: [{:pid, pid}, {:id, id} | offender]
) do
pid_info =
if is_pid(pid) and context != :shutdown do
["\nPid: ", inspect(pid)]
else
[]
end
msg =
["Child ", inspect(id), " of Supervisor ", sup_name(sup)] ++
[?\s, sup_context(context), "\n** (exit) ", offender_reason(reason, context)] ++
pid_info ++ child_info(min_level, offender)
{:ok, msg}
end
defp report_supervisor(
min_level,
supervisor: sup,
errorContext: context,
reason: reason,
offender: [{:nb_children, n}, {:id, id} | offender]
) do
msg =
["Children ", inspect(id), " of Supervisor ", sup_name(sup), ?\s, sup_context(context)] ++
["\n** (exit) ", offender_reason(reason, context), "\nNumber: ", Integer.to_string(n)] ++
child_info(min_level, offender)
{:ok, msg}
end
defp report_supervisor(
min_level,
supervisor: sup,
errorContext: context,
reason: reason,
offender: [{:pid, pid} | offender]
) do
msg =
["Child of Supervisor ", sup_name(sup), ?\s, sup_context(context)] ++
["\n** (exit) ", offender_reason(reason, context), "\nPid: ", inspect(pid)] ++
child_info(min_level, offender)
{:ok, msg}
end
defp report_supervisor(_min_level, _other), do: :none
# If start call raises reason will be of form {:EXIT, reason}
defp offender_reason({:EXIT, reason}, :start_error) do
Exception.format_exit(reason)
end
defp offender_reason(reason, _context) do
Exception.format_exit(reason)
end
defp sup_name({:local, name}), do: inspect(name)
defp sup_name({:global, name}), do: inspect(name)
defp sup_name({:via, _mod, name}), do: inspect(name)
defp sup_name({pid, mod}), do: [inspect(pid), " (", inspect(mod), ?)]
defp sup_context(:start_error), do: "failed to start"
defp sup_context(:child_terminated), do: "terminated"
defp sup_context(:shutdown), do: "caused shutdown"
defp sup_context(:shutdown_error), do: "shutdown abnormally"
defp child_info(min_level, [{:mfargs, {mod, fun, args}} | debug]) do
["\nStart Call: ", format_mfa(mod, fun, args) | child_debug(min_level, debug)]
end
# Comes from bridge with MFA
defp child_info(min_level, [{:mfa, {mod, fun, args}} | debug]) do
["\nStart Call: ", format_mfa(mod, fun, args) | child_debug(min_level, debug)]
end
# Comes from bridge with Mod
defp child_info(min_level, [{:mod, mod} | debug]) do
["\nStart Module: ", inspect(mod) | child_debug(min_level, debug)]
end
defp child_info(_min_level, _child) do
[]
end
defp child_debug(:debug, restart_type: restart, shutdown: shutdown, child_type: type) do
["\nRestart: ", inspect(restart), "\nShutdown: ", inspect(shutdown)] ++
["\nType: ", inspect(type)]
end
defp child_debug(_min_level, _child) do
[]
end
defp report_crash(min_level, [[{:initial_call, initial_call} | crashed], linked]) do
mfa = initial_call_to_mfa(initial_call)
report_crash(min_level, crashed, [{:initial_call, mfa}], linked)
end
defp report_crash(min_level, [crashed, linked]) do
report_crash(min_level, crashed, [], linked)
end
defp report_crash(min_level, crashed, extra, linked) do
[
{:pid, pid},
{:registered_name, name},
{:error_info, {kind, reason, stack}} | crashed
] = crashed
reason = Exception.normalize(kind, reason, stack)
case crashed[:dictionary][:logger_metadata] || {true, []} do
{false, _} ->
:skip
{true, user_metadata} ->
msg =
["Process ", crash_name(pid, name), " terminating", format(kind, reason, stack)] ++
[crash_info(min_level, extra ++ crashed, [?\n]), crash_linked(min_level, linked)]
crash_metadata = [crash_reason: exit_reason(kind, reason, stack)] ++ registered_name(name)
{:ok, msg, crash_metadata ++ extra ++ user_metadata}
end
end
defp initial_call_to_mfa({:supervisor, module, _}), do: {module, :init, 1}
defp initial_call_to_mfa({:supervisor_bridge, module, _}), do: {module, :init, 1}
defp initial_call_to_mfa({mod, fun, args}) when is_list(args), do: {mod, fun, length(args)}
defp initial_call_to_mfa(mfa), do: mfa
defp crash_name(pid, []), do: inspect(pid)
defp crash_name(pid, name), do: [inspect(name), " (", inspect(pid), ?)]
defp crash_info(min_level, [{:initial_call, {mod, fun, args}} | info], prefix) do
[prefix, "Initial Call: ", crash_call(mod, fun, args) | crash_info(min_level, info, prefix)]
end
defp crash_info(min_level, [{:current_function, {mod, fun, args}} | info], prefix) do
[prefix, "Current Call: ", crash_call(mod, fun, args) | crash_info(min_level, info, prefix)]
end
defp crash_info(min_level, [{:current_function, []} | info], prefix) do
crash_info(min_level, info, prefix)
end
defp crash_info(min_level, [{:ancestors, ancestors} | debug], prefix) do
[prefix, "Ancestors: ", inspect(ancestors) | crash_info(min_level, debug, prefix)]
end
defp crash_info(:debug, debug, prefix) do
for {key, value} <- debug do
crash_debug(key, value, prefix)
end
end
defp crash_info(_, _, _) do
[]
end
defp crash_call(mod, fun, arity) when is_integer(arity) do
format_mfa(mod, fun, arity)
end
defp crash_call(mod, fun, args) do
format_mfa(mod, fun, length(args))
end
defp crash_debug(:current_stacktrace, stack, prefix) do
stack_prefix = [prefix | " "]
stacktrace = Enum.map(stack, &[stack_prefix | Exception.format_stacktrace_entry(&1)])
[prefix, "Current Stacktrace:" | stacktrace]
end
defp crash_debug(key, value, prefix) do
[prefix, crash_debug_key(key), ?:, ?\s, inspect(value)]
end
defp crash_debug_key(key) do
case key do
:message_queue_len -> "Message Queue Length"
:messages -> "Messages"
:links -> "Links"
:dictionary -> "Dictionary"
:trap_exit -> "Trapping Exits"
:status -> "Status"
:heap_size -> "Heap Size"
:stack_size -> "Stack Size"
:reductions -> "Reductions"
end
end
defp crash_linked(_min_level, []), do: []
defp crash_linked(min_level, neighbours) do
Enum.reduce(neighbours, "\nNeighbours:", fn {:neighbour, info}, acc ->
[acc | crash_neighbour(min_level, info)]
end)
end
@indent " "
defp crash_neighbour(min_level, [{:pid, pid}, {:registered_name, []} | info]) do
[?\n, @indent, inspect(pid) | crash_info(min_level, info, [?\n, @indent | @indent])]
end
defp crash_neighbour(min_level, [{:pid, pid}, {:registered_name, name} | info]) do
[?\n, @indent, inspect(name), " (", inspect(pid), ")"] ++
crash_info(min_level, info, [?\n, @indent | @indent])
end
defp format_last_message_from({_, {name, _}}), do: [" (from ", inspect(name), ")"]
defp format_last_message_from({from, _}), do: [" (from ", inspect(from), ")"]
defp format_last_message_from(_), do: []
defp format_client_info({from, :dead}),
do: ["\nClient ", inspect(from), " is dead"]
defp format_client_info({from, :remote}),
do: ["\nClient ", inspect(from), " is remote on node ", inspect(node(from))]
defp format_client_info({_, {name, stacktrace}}),
do: ["\nClient ", inspect(name), " is alive\n" | format_stacktrace(stacktrace)]
defp format_client_info(_),
do: []
defp format_reason({maybe_exception, [_ | _] = maybe_stacktrace} = reason) do
try do
format_stacktrace(maybe_stacktrace)
catch
:error, _ ->
{format_stop(reason), {reason, []}}
else
formatted_stacktrace ->
{formatted, reason} = maybe_normalize(maybe_exception, maybe_stacktrace)
{[formatted | formatted_stacktrace], {reason, maybe_stacktrace}}
end
end
defp format_reason(reason) do
{format_stop(reason), {reason, []}}
end
defp format_stop(reason) do
["\n** (stop) " | Exception.format_exit(reason)]
end
# Erlang processes rewrite the :undef error to these reasons when logging
@gen_undef [:"module could not be loaded", :"function not exported"]
defp maybe_normalize(undef, [{mod, fun, args, _info} | _] = stacktrace)
when undef in @gen_undef and is_atom(mod) and is_atom(fun) do
cond do
is_list(args) ->
format_undef(mod, fun, length(args), undef, stacktrace)
is_integer(args) ->
format_undef(mod, fun, args, undef, stacktrace)
true ->
{format_stop(undef), undef}
end
end
defp maybe_normalize(reason, stacktrace) do
# If this is already an exception (even an ErlangError), we format it as an
# exception. Otherwise, we try to normalize it, and if it's normalized as an
# ErlangError we instead format it as an exit.
if Exception.exception?(reason) do
{[?\n | Exception.format_banner(:error, reason, stacktrace)], reason}
else
case Exception.normalize(:error, reason, stacktrace) do
%ErlangError{} ->
{format_stop(reason), reason}
exception ->
{[?\n | Exception.format_banner(:error, exception, stacktrace)], exception}
end
end
end
defp format(kind, payload, stacktrace) do
[?\n, Exception.format_banner(kind, payload, stacktrace) | format_stacktrace(stacktrace)]
end
defp format_stacktrace(stacktrace) do
for entry <- stacktrace do
["\n " | Exception.format_stacktrace_entry(entry)]
end
end
defp registered_name(name) when is_atom(name), do: [registered_name: name]
defp registered_name(_name), do: []
defp format_mfa(mod, fun, :undefined),
do: [inspect(mod), ?., Code.Identifier.inspect_as_function(fun) | "/?"]
defp format_mfa(mod, fun, args),
do: Exception.format_mfa(mod, fun, args)
defp exit_reason(:exit, reason, stack) do
if :erlang.system_info(:otp_release) >= '20', do: {reason, stack}, else: nil
end
defp exit_reason(:error, reason, stack), do: {reason, stack}
defp exit_reason(:throw, value, stack), do: {{:nocatch, value}, stack}
## Deprecated helpers
defp format_from([]), do: ""
defp format_from([from]), do: " (from #{inspect(from)})"
defp format_from([from, stacktrace]) when is_list(stacktrace), do: " (from #{inspect(from)})"
defp format_from([from, node_name]) when is_atom(node_name),
do: " (from #{inspect(from)} on #{inspect(node_name)})"
defp format_client([from]) do
"\nClient #{inspect(from)} is dead"
end
defp format_client([from, stacktrace]) when is_list(stacktrace) do
["\nClient #{inspect(from)} is alive\n" | format_stacktrace(stacktrace)]
end
defp format_client(_) do
[]
end
defp format_undef(mod, fun, arity, undef, stacktrace) do
opts = [module: mod, function: fun, arity: arity, reason: undef]
exception = UndefinedFunctionError.exception(opts)
{[?\n | Exception.format_banner(:error, exception, stacktrace)], exception}
end
end