我在控制台上看到Elixir崩溃日志,但是没有将它们记录到日志文件中。
我尝试设置配置
config :logger,
handle_otp_reports: true,
handle_sasl_reports: true,
compile_time_purge_level: :info,
backends: [:console, LogToLager],
level: :info,
utc_log: true
我希望崩溃报告也会与控制台一起进入日志文件。
注意:我正在使用Lager进行记录。
答案 0 :(得分:0)
鉴于较大的配置已使用自定义extra_sinks
接收器名称指定了dispatcher_lager_event
,我假设LogToLager
使用dispatcher_lager_event
来调度lager
日志。
也就是说,我已经根据logger_lager_backend
软件包中的LoggerLagerBackend
模块的启发,使用自定义的大型后端模块复制了您的设置:
defmodule MyLagerBackend do
@behaviour :gen_event
def init(__MODULE__), do: {:ok, nil}
def handle_call({:configure, _opts}, state), do: {:ok, :ok, state}
def handle_event({:error, _groupleader, {Logger, message, _timestamp, metadata}}, state) do
:lager.dispatch_log(:dispatcher_lager_event, :error, metadata, '~ts', [message], 4096, :safe)
{:ok, state}
end
def handle_event({level, _groupleader, {Logger, message, _timestamp, metadata}}, state) do
:lager.dispatch_log(:lager_event, to_lager_level(level), metadata, '~ts', [message], 4096, :safe)
{:ok, state}
end
def handle_event(:flush, state), do: {:ok, state}
# gen_event boilerplate
def handle_info(_msg, state), do: {:ok, state}
def terminate(_reason, _state), do: :ok
def code_change(_old, state, _extra), do: {:ok, state}
# Private functions
defp to_lager_level(:warn), do: :warning
defp to_lager_level(level), do: level
end
config/config.exs
中包含以下内容:
use Mix.Config
config :logger,
handle_otp_reports: true,
handle_sasl_reports: true,
compile_time_purge_level: :info,
backends: [MyLagerBackend],
level: :info,
utc_log: true
config :lager,
handlers: [
lager_console_backend: [level: :info]
],
extra_sinks: [
dispatcher_lager_event: [
handlers: [
lager_file_backend: [
file: "my_custom_file.log",
level: :info,
size: 100 * 1024 * 1024,
date: '$D0',
count: 10
]
]
]
]
我打开iex
并运行Task.async(fn -> 1 / 0 end)
,这会在控制台中生成崩溃报告:
$ iex -S mix
Erlang/OTP 22 [erts-10.4.4] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1] [hipe] [dtrace]
...a bunch of auxiliary messages removed for brevity...
Interactive Elixir (1.9.0) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> Task.async(fn -> 1 / 0 end)
11:11:52.580 [error] ** Task <0.226.0> terminating
** Started from <0.224.0>
** When function == #Fun<erl_eval.21.91303403>
** arguments == []
** Reason for termination ==
** {badarith,[{erlang,'/',[1,0],[]},{'Elixir.Task.Supervised',invoke_mfa,2,[{file,"lib/task/supervised.ex"},{line,90}]},{'Elixir.Task.Supervised',reply,5,[{file,"lib/task/supervised.ex"},{line,35}]},{proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,249}]}]}
%Task{
owner: #PID<0.224.0>,
pid: #PID<0.226.0>,
ref: #Reference<0.265415656.4091019275.172412>
}
iex(2)> 11:11:52.615 [error] CRASH REPORT Process <0.226.0> with 1 neighbours crashed with reason: bad arithmetic expression in erlang:'/'(1, 0)
** (EXIT from #PID<0.224.0>) shell process exited with reason: an exception was raised:
** (ArithmeticError) bad argument in arithmetic expression
:erlang./(1, 0)
(elixir) lib/task/supervised.ex:90: Task.Supervised.invoke_mfa/2
(elixir) lib/task/supervised.ex:35: Task.Supervised.reply/5
(stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Interactive Elixir (1.9.0) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)>
...以及我的my_custom_file.log
:
$ cat my_custom_file.log
2019-07-11 11:11:52.615 [error] <0.226.0> Task #PID<0.226.0> started from #PID<0.224.0> terminating
** (ArithmeticError) bad argument in arithmetic expression
:erlang./(1, 0)
(elixir) lib/task/supervised.ex:90: Task.Supervised.invoke_mfa/2
(elixir) lib/task/supervised.ex:35: Task.Supervised.reply/5
(stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Function: #Function<21.91303403/0 in :erl_eval.expr/5>
Args: []
2019-07-11 11:11:52.623 [error] <0.226.0>@proc_lib:crash_report/4:508 Process #PID<0.226.0> terminating
** (ArithmeticError) bad argument in arithmetic expression
:erlang./(1, 0)
(elixir) lib/task/supervised.ex:90: Task.Supervised.invoke_mfa/2
(elixir) lib/task/supervised.ex:35: Task.Supervised.reply/5
(stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Initial Call: anonymous fn/0 in :erl_eval.expr/5
Ancestors: [#PID<0.224.0>, #PID<0.81.0>]
Neighbours:
#PID<0.224.0>
Initial Call: IEx.Evaluator.init/4
Current Call: IEx.Evaluator.loop/1
Ancestors: [#PID<0.81.0>]
$