Skip to content

Commit 1f725b0

Browse files
author
José Valim
committed
Merge pull request #2637 from fishcakez/improve_sasl_reports
Improve Logger's SASL report translation
2 parents 3fc07ef + 38f895c commit 1f725b0

File tree

2 files changed

+277
-101
lines changed

2 files changed

+277
-101
lines changed

lib/logger/lib/logger/translator.ex

Lines changed: 150 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -64,68 +64,93 @@ defmodule Logger.Translator do
6464
end
6565

6666
def translate(min_level, :error, :report, {:supervisor_report, data}) do
67-
{:ok, translate_supervisor(min_level, data)}
67+
translate_supervisor(min_level, data)
6868
end
6969

7070
def translate(min_level, :error, :report, {:crash_report, data}) do
71-
{:ok, translate_crash(min_level, data)}
71+
translate_crash(min_level, data)
7272
end
7373

7474
def translate(min_level, :info, :report, {:progress, data}) do
75-
{:ok, translate_progress(min_level, data)}
75+
translate_progress(min_level, data)
7676
end
7777

7878
def translate(_min_level, _level, _kind, _message) do
7979
:none
8080
end
8181

82-
def translate_supervisor(min_level, data) do
83-
sup = Keyword.fetch!(data, :supervisor)
84-
context = Keyword.fetch!(data, :errorContext)
85-
offender = Keyword.fetch!(data, :offender)
86-
reason = Keyword.fetch!(data, :reason)
87-
case Keyword.fetch(offender, :pid) do
88-
{:ok, pid} when is_pid(pid) and context !== :shutdown ->
89-
["Child ", child_name(offender), " of Supervisor ",
90-
sup_name(sup), ?\s, sup_context(context), ?\n,
91-
"Pid: ", inspect(pid), ?\n,
92-
child_info(min_level, offender), ?\n,
93-
"** (exit) " | offender_reason(reason, context)]
94-
{:ok, _} ->
95-
["Child ", child_name(offender), " of Supervisor ",
96-
sup_name(sup), ?\s, sup_context(context), ?\n,
97-
child_info(min_level, offender), ?\n,
98-
"** (exit) " | offender_reason(reason, context)]
99-
:error ->
100-
number = Keyword.fetch!(offender, :nb_children)
101-
["Children ", child_name(offender), " of Supervisor ",
102-
sup_name(sup), ?\s, sup_context(context), ?\n,
103-
"Number: ", inspect(number), ?\n,
104-
child_info(min_level, offender), ?\n,
105-
"** (exit) " | offender_reason(reason, context)]
106-
end
82+
defp translate_supervisor(min_level,
83+
[supervisor: sup, errorContext: context,
84+
reason: reason,
85+
offender: [{:pid, pid}, {:name, name} | offender]])
86+
when is_pid(pid) and context !== :shutdown do
87+
{:ok, ["Child ", inspect(name), " of Supervisor ",
88+
sup_name(sup), ?\s, sup_context(context), ?\n,
89+
"Pid: ", inspect(pid), ?\n,
90+
child_info(min_level, offender), ?\n,
91+
"** (exit) " | offender_reason(reason, context)]}
10792
end
10893

109-
defp translate_progress(min_level, data) do
110-
case Keyword.fetch(data, :application) do
111-
{:ok, app} ->
112-
node_name = Keyword.fetch!(data, :started_at)
113-
["Application ", to_string(app), " started at " | inspect(node_name)]
114-
:error ->
115-
translate_sup_progress(min_level, data)
116-
end
94+
defp translate_supervisor(min_level,
95+
[supervisor: sup, errorContext: context,
96+
reason: reason,
97+
offender: [{:pid, _pid},
98+
{:name, name} | offender]]) do
99+
{:ok, ["Child ", inspect(name), " of Supervisor ",
100+
sup_name(sup), ?\s, sup_context(context), ?\n,
101+
child_info(min_level, offender), ?\n,
102+
"** (exit) " | offender_reason(reason, context)]}
103+
end
104+
105+
defp translate_supervisor(min_level,
106+
[supervisor: sup, errorContext: context,
107+
reason: reason,
108+
offender: [{:pid, pid} | offender]]) do
109+
{:ok, ["Child of Supervisor ",
110+
sup_name(sup), ?\s, sup_context(context), ?\n,
111+
"Pid: ", inspect(pid), ?\n,
112+
child_info(min_level, offender), ?\n,
113+
"** (exit) " | offender_reason(reason, context)]}
114+
end
115+
116+
defp translate_supervisor(min_level,
117+
[supervisor: sup, errorContext: context,
118+
reason: reason,
119+
offender: [{:nb_children, n},
120+
{:name, name} | offender]]) do
121+
{:ok, ["Children ", inspect(name), " of Supervisor ",
122+
sup_name(sup), ?\s, sup_context(context), ?\n,
123+
"Number: ", inspect(n), ?\n,
124+
child_info(min_level, offender), ?\n,
125+
"** (exit) " | offender_reason(reason, context)]}
126+
end
127+
128+
defp translate_supervisor(_min_level, _other), do: :none
129+
130+
defp translate_progress(_min_level,
131+
[application: app, started_at: node_name]) do
132+
{:ok, ["Application ", to_string(app), " started at " | inspect(node_name)]}
133+
end
134+
135+
defp translate_progress(min_level,
136+
[supervisor: sup,
137+
started: [{:pid, pid}, {:name, name} | started]]) do
138+
{:ok, ["Child ", inspect(name), " of Supervisor ",
139+
sup_name(sup), " started\n",
140+
"Pid: ", inspect(pid), ?\n |
141+
child_info(min_level, started)]}
117142
end
118143

119-
defp translate_sup_progress(min_level, data) do
120-
sup = Keyword.fetch!(data, :supervisor)
121-
started = Keyword.fetch!(data, :started)
122-
pid = Keyword.fetch!(started, :pid)
123-
["Child ", child_name(started), " of Supervisor ",
124-
sup_name(sup), " started\n",
125-
"Pid: ", inspect(pid), ?\n |
126-
child_info(min_level, started)]
144+
defp translate_progress(min_level,
145+
[supervisor: sup,
146+
started: [{:pid, pid} | started]]) do
147+
{:ok, ["Child of Supervisor ", sup_name(sup), " started\n",
148+
"Pid: ", inspect(pid), ?\n |
149+
child_info(min_level, started)]}
127150
end
128151

152+
defp translate_progress(_min_level, _other), do: :none
153+
129154
defp sup_name({:local, name}), do: inspect(name)
130155
defp sup_name({:global, name}), do: inspect(name)
131156
defp sup_name({:via, _mod, name}), do: inspect(name)
@@ -136,20 +161,23 @@ defmodule Logger.Translator do
136161
defp sup_context(:shutdown), do: "caused shutdown"
137162
defp sup_context(:shutdown_error), do: "shutdown abnormally"
138163

139-
defp child_name(offender) do
140-
inspect(Keyword.fetch!(offender, :name))
164+
defp child_info(min_level, [{:mfargs, {mod, fun, args}} | debug]) do
165+
["Start Call: ", Exception.format_mfa(mod, fun, args) |
166+
child_debug(min_level, debug)]
141167
end
142168

143-
defp child_info(min_level, child) do
144-
{mod, fun, args} = Keyword.fetch!(child, :mfargs)
169+
defp child_info(min_level, [{:mfa, {mod, fun, args}} | debug]) do
145170
["Start Call: ", Exception.format_mfa(mod, fun, args) |
146-
child_debug(min_level, child)]
171+
child_debug(min_level, debug)]
172+
end
173+
174+
defp child_info(min_level, [{:mod, mod} | debug]) do
175+
["Start Module: ", inspect(mod) |
176+
child_debug(min_level, debug)]
147177
end
148178

149-
defp child_debug(:debug, child) do
150-
restart = Keyword.fetch!(child, :restart_type)
151-
shutdown = Keyword.fetch!(child, :shutdown)
152-
type = Keyword.fetch!(child, :child_type)
179+
defp child_debug(:debug,
180+
[restart_type: restart, shutdown: shutdown, child_type: type]) do
153181
[?\n,
154182
"Restart: ", inspect(restart), ?\n,
155183
"Shutdown: ", inspect(shutdown), ?\n,
@@ -169,57 +197,76 @@ defmodule Logger.Translator do
169197
Exception.format_exit(reason)
170198
end
171199

172-
defp translate_crash(min_level, [crashed, linked]) do
173-
pid = Keyword.fetch!(crashed, :pid)
174-
{kind, exception, stack} = Keyword.fetch!(crashed, :error_info)
175-
["Process ", inspect(pid) , " terminating\n",
176-
crash_info(min_level, crashed),
177-
crash_linked(min_level, linked),
178-
Exception.format_banner(kind, exception, stack)]
200+
defp translate_crash(min_level,
201+
[[{:initial_call, _} = initial_call,
202+
{:pid, pid},
203+
{:registered_name, name},
204+
{:error_info, {kind, exception, stack}} | crashed],
205+
linked]) do
206+
{:ok, ["Process ", crash_name(pid, name) , " terminating\n",
207+
crash_info(min_level, [initial_call | crashed]),
208+
crash_linked(min_level, linked) |
209+
Exception.format_banner(kind, exception, stack)]}
179210
end
180211

181-
defp crash_info(min_level, info, prefix \\ []) do
182-
ancestors = Keyword.fetch!(info, :ancestors)
183-
[crash_name(info, prefix),
184-
prefix, "Initial Call: ", initial_call(info), ?\n,
185-
crash_current(info, prefix),
186-
prefix, "Ancestors: ", inspect(ancestors), ?\n |
187-
crash_debug(min_level, info, prefix)]
212+
defp translate_crash(min_level,
213+
[[{:pid, pid},
214+
{:registered_name, name},
215+
{:error_info, {kind, exception, stack}} | crashed],
216+
linked]) do
217+
{:ok, ["Process ", crash_name(pid, name) , " terminating\n",
218+
crash_info(min_level, crashed),
219+
crash_linked(min_level, linked) |
220+
Exception.format_banner(kind, exception, stack)]}
188221
end
189222

190-
defp crash_name(info, prefix) do
191-
case Keyword.fetch!(info, :registered_name) do
192-
[] -> []
193-
name -> [prefix, "Name: ", inspect(name), ?\n]
194-
end
223+
defp crash_name(pid, []), do: inspect(pid)
224+
defp crash_name(pid, name), do: [inspect(name), " (", inspect(pid), ?)]
225+
226+
defp crash_info(min_level, info, prefix \\ [])
227+
228+
defp crash_info(min_level,
229+
[{:initial_call, {mod, fun, args}} | info], prefix) do
230+
[prefix, "Initial Call: ", crash_call(mod, fun, args), ?\n |
231+
crash_info(min_level, info, prefix)]
195232
end
196233

197-
defp initial_call(info) do
198-
case Keyword.fetch!(info, :initial_call) do
199-
{mod, fun, arity} when is_integer(arity) ->
200-
Exception.format_mfa(mod, fun, arity)
201-
{mod, fun, args} ->
202-
# args are fake list
203-
Exception.format_mfa(mod, fun, length(args))
204-
end
234+
defp crash_info(min_level,
235+
[{:current_function, {mod, fun, args}} | info], prefix) do
236+
[prefix, "Current Call: ", crash_call(mod, fun, args), ?\n |
237+
crash_info(min_level, info, prefix)]
205238
end
206239

207-
defp crash_current(info, prefix) do
208-
case Keyword.fetch(info, :current_function) do
209-
{:ok, {mod, fun, arity}} ->
210-
[prefix, "Current Call: ", Exception.format_mfa(mod, fun, arity), ?\n]
211-
:error ->
212-
[]
213-
end
240+
defp crash_info(min_level, [{:current_function, []} | info], prefix) do
241+
crash_info(min_level, info, prefix)
214242
end
215243

216-
defp crash_debug(:debug, info, prefix) do
217-
[messages: "Messages", links: "Links", dictionary: "Dictionary",
218-
trap_exit: "Trapping Exits", status: "Status", heap_size: "Heap Size",
219-
stack_size: "Stack Size", reductions: "Reductions"]
220-
|> Enum.reduce([], fn({key, text}, acc) ->
221-
[acc, prefix, text, ": ", inspect(Keyword.fetch!(info, key)), ?\n]
222-
end)
244+
defp crash_info(min_level,
245+
[{:ancestors, ancestors} | debug], prefix) do
246+
[prefix, "Ancestors: ", inspect(ancestors), ?\n |
247+
crash_debug(min_level, debug, prefix)]
248+
end
249+
250+
defp crash_call(mod, fun, arity) when is_integer(arity) do
251+
Exception.format_mfa(mod, fun, arity)
252+
end
253+
254+
defp crash_call(mod, fun, args) do
255+
Exception.format_mfa(mod, fun, length(args))
256+
end
257+
258+
defp crash_debug(:debug,
259+
[messages: msgs, links: links, dictionary: dict,
260+
trap_exit: trap, status: status, heap_size: heap_size,
261+
stack_size: stack_size, reductions: reductions], prefix) do
262+
[prefix, "Messages: ", inspect(msgs), ?\n,
263+
prefix, "Links: ", inspect(links), ?\n,
264+
prefix, "Dictionary: ", inspect(dict), ?\n,
265+
prefix, "Trapping Exits: ", inspect(trap), ?\n,
266+
prefix, "Status: ", inspect(status), ?\n,
267+
prefix, "Heap Size: ", inspect(heap_size), ?\n,
268+
prefix, "Stack Size: ", inspect(stack_size), ?\n,
269+
prefix, "Reductions: ", inspect(reductions), ?\n]
223270
end
224271

225272
defp crash_debug(_min_level, _info, _prefix) do
@@ -234,11 +281,18 @@ defmodule Logger.Translator do
234281
end)
235282
end
236283

237-
defp crash_neighbour(min_level, info) do
238-
pid = Keyword.fetch!(info, :pid)
284+
defp crash_neighbour(min_level,
285+
[{:pid, pid}, {:registered_name, []} | info]) do
239286
prefix = " "
240287
[prefix, inspect(pid), ?\n |
241288
crash_info(min_level, info, [prefix | prefix])]
242289
end
243290

291+
defp crash_neighbour(min_level,
292+
[{:pid, pid}, {:registered_name, name} | info]) do
293+
prefix = " "
294+
[prefix, inspect(name), " (", inspect(pid), ")\n" |
295+
crash_info(min_level, info, [prefix | prefix])]
296+
end
297+
244298
end

0 commit comments

Comments
 (0)