Logging 明文中的Erlang错误日志记录
我目前有一个OTP应用程序,我正在使用SASL错误日志记录。日志将被转换为二进制编码文件,我必须使用rb库读取这些文件。这是一个巨大的痛苦,因为我真正想要的是能够将所有日志导出为纯文本,并以我的方式通过它们。我在一个旧邮件列表上找到了一条评论,它提供了一些指向其他错误记录者的链接: 但是代码已经过时了,当我试图编译它时会抛出大量错误。有没有更新的库可以用来将SASL消息发送到纯文本文件?您可以从basho的朋友那里尝试,它是登录erlang的好库 您好。您可以从basho的朋友那里尝试,这是一个很好的登录erlang的库Logging 明文中的Erlang错误日志记录,logging,erlang,Logging,Erlang,我目前有一个OTP应用程序,我正在使用SASL错误日志记录。日志将被转换为二进制编码文件,我必须使用rb库读取这些文件。这是一个巨大的痛苦,因为我真正想要的是能够将所有日志导出为纯文本,并以我的方式通过它们。我在一个旧邮件列表上找到了一条评论,它提供了一些指向其他错误记录者的链接: 但是代码已经过时了,当我试图编译它时会抛出大量错误。有没有更新的库可以用来将SASL消息发送到纯文本文件?您可以从basho的朋友那里尝试,它是登录erlang的好库 您好。您可以从basho的朋友那里尝试,这是一
注意。您可以使用sasl事件框架创建自己的事件处理程序。 详细解释见“Manning.Erlang和Otp在行动中,第2部分第7章,第170页(不同版本可能有所不同)”一书,记录和事件处理Erlang/Otp方式 对于实际代码,请阅读couchdb的源代码文件“coach_log.erl”和“coach_event_sup.erl”,第一个文件是事件处理程序,第二个文件是gen_server包装模块,用于“coach_log” 以下代码引用“coach_log.erl”文件进行解释
-behaviour(gen_event). %<======event handler OTP module behaviour
init([]) ->
% read config and register for configuration changes
% just stop if one of the config settings change. couch_server_sup
% will restart us and then we will pick up the new settings.
ok = couch_config:register(
fun("log", "file") ->
?MODULE:stop();
("log", "level") ->
?MODULE:stop();
("log", "include_sasl") ->
?MODULE:stop()
end),
Filename = couch_config:get("log", "file", "couchdb.log"),
Level = level_integer(list_to_atom(couch_config:get("log", "level", "info"))),
Sasl = list_to_atom(couch_config:get("log", "include_sasl", "true")),
case ets:info(?MODULE) of
undefined -> ets:new(?MODULE, [named_table]);
_ -> ok
end,
ets:insert(?MODULE, {level, Level}),
case file:open(Filename, [append]) of %<<========open customzied file for logging
{ok, Fd} ->
{ok, {Fd, Level, Sasl}}; <<=========save the log file deviceIO into module state
{error, eacces} ->
{stop, {file_permission_error, Filename}};
Error ->
{stop, Error}
end.
debug(Format, Args) ->
{ConsoleMsg, FileMsg} = get_log_messages(self(), debug, Format, Args),
gen_event:sync_notify(error_logger, {couch_debug, ConsoleMsg, FileMsg}). <=====generate customized logging message
handle_event({couch_debug, ConMsg, FileMsg}, {Fd, LogLevel, _Sasl}=State) <====handle the message
when LogLevel =< ?LEVEL_DEBUG ->
log(Fd, ConMsg, FileMsg),
{ok, State};
log(Fd, Pid, Level, Format, Args) ->
Msg = io_lib:format(Format, Args),
ok = io:format("[~s] [~p] ~s~n", [Level, Pid, Msg]), % dump to console too
Msg2 = re:replace(lists:flatten(Msg),"\\r\\n|\\r|\\n", "\r\n",
[global, {return, list}]),
ok = io:format(Fd, "[~s] [~s] [~p] ~s\r~n", [httpd_util:rfc1123_date(), Level, Pid, Msg2]).
log(Fd, ConsoleMsg, FileMsg) ->
ok = io:put_chars(ConsoleMsg),
ok = io:put_chars(Fd, FileMsg). <=====write the log into file
-行为(发电机事件)。%
%读取配置并注册配置更改
%如果其中一个配置设置发生更改,请停止。沙发服务
%将重新启动我们,然后我们将选择新设置。
ok=coach\u配置:寄存器(
乐趣(“日志”、“文件”)->
?模块:停止();
(“日志”、“级别”)->
?模块:停止();
(“日志”、“包含”)->
?模块:停止()
(完),,
Filename=coach\u config:get(“log”、“file”、“couchdb.log”),
级别=级别整数(从列表到原子(coach\u配置:get(“log”、“Level”、“info”)),
Sasl=list_to_atom(coach_配置:get(“log”,“include_Sasl”,“true”),
案例ets:信息(?模块)
未定义->ets:new(?MODULE,[named_table]);
_->好的
完,,
ets:插入(?模块,{level,level}),
案例文件:打开(文件名,[append]),共%
{ConsoleMsg,FileMsg}=get_log_消息(self(),debug,Format,Args),
gen_事件:sync_notify(错误记录程序,{coach_debug,ConsoleMsg,FileMsg})。
Msg=io_lib:format(格式,Args),
ok=io:format(“[~s][~p]~s~n”,[Level,Pid,Msg]),%dump-to-console-too
Msg2=re:replace(列表:展平(Msg),“\\r\\n |\\r |\\n”,“\r\n”,
[global,{return,list}],
ok=io:format(Fd,“[~s][~s][~p]~s\r~n”,[httpd_util:rfc1123_date(),Level,Pid,Msg2])。
日志(Fd、ConsoleMsg、FileMsg)->
ok=io:put_字符(控制台符号),
ok=io:put_chars(Fd,FileMsg) 您可以使用sasl事件框架创建自己的事件处理程序。
详细解释见“Manning.Erlang和Otp在行动中,第2部分第7章,第170页(不同版本可能有所不同)”一书,记录和事件处理Erlang/Otp方式
对于实际代码,请阅读couchdb的源代码文件“coach_log.erl”和“coach_event_sup.erl”,第一个文件是事件处理程序,第二个文件是gen_server包装模块,用于“coach_log”
以下代码引用“coach_log.erl”文件进行解释
-behaviour(gen_event). %<======event handler OTP module behaviour
init([]) ->
% read config and register for configuration changes
% just stop if one of the config settings change. couch_server_sup
% will restart us and then we will pick up the new settings.
ok = couch_config:register(
fun("log", "file") ->
?MODULE:stop();
("log", "level") ->
?MODULE:stop();
("log", "include_sasl") ->
?MODULE:stop()
end),
Filename = couch_config:get("log", "file", "couchdb.log"),
Level = level_integer(list_to_atom(couch_config:get("log", "level", "info"))),
Sasl = list_to_atom(couch_config:get("log", "include_sasl", "true")),
case ets:info(?MODULE) of
undefined -> ets:new(?MODULE, [named_table]);
_ -> ok
end,
ets:insert(?MODULE, {level, Level}),
case file:open(Filename, [append]) of %<<========open customzied file for logging
{ok, Fd} ->
{ok, {Fd, Level, Sasl}}; <<=========save the log file deviceIO into module state
{error, eacces} ->
{stop, {file_permission_error, Filename}};
Error ->
{stop, Error}
end.
debug(Format, Args) ->
{ConsoleMsg, FileMsg} = get_log_messages(self(), debug, Format, Args),
gen_event:sync_notify(error_logger, {couch_debug, ConsoleMsg, FileMsg}). <=====generate customized logging message
handle_event({couch_debug, ConMsg, FileMsg}, {Fd, LogLevel, _Sasl}=State) <====handle the message
when LogLevel =< ?LEVEL_DEBUG ->
log(Fd, ConMsg, FileMsg),
{ok, State};
log(Fd, Pid, Level, Format, Args) ->
Msg = io_lib:format(Format, Args),
ok = io:format("[~s] [~p] ~s~n", [Level, Pid, Msg]), % dump to console too
Msg2 = re:replace(lists:flatten(Msg),"\\r\\n|\\r|\\n", "\r\n",
[global, {return, list}]),
ok = io:format(Fd, "[~s] [~s] [~p] ~s\r~n", [httpd_util:rfc1123_date(), Level, Pid, Msg2]).
log(Fd, ConsoleMsg, FileMsg) ->
ok = io:put_chars(ConsoleMsg),
ok = io:put_chars(Fd, FileMsg). <=====write the log into file
-行为(发电机事件)。%
%读取配置并注册配置更改
%如果其中一个配置设置发生更改,请停止。沙发服务
%将重新启动我们,然后我们将选择新设置。
ok=coach\u配置:寄存器(
乐趣(“日志”、“文件”)->
?模块:停止();
(“日志”、“级别”)->
?模块:停止();
(“日志”、“包含”)->
?模块:停止()
(完),,
Filename=coach\u config:get(“log”、“file”、“couchdb.log”),
级别=级别整数(从列表到原子(coach\u配置:get(“log”、“Level”、“info”)),
Sasl=list_to_atom(coach_配置:get(“log”,“include_Sasl”,“true”),
案例ets:信息(?模块)
未定义->ets:new(?MODULE,[named_table]);
_->好的
完,,
ets:插入(?模块,{level,level}),
案例文件:打开(文件名,[append]),共%
{ConsoleMsg,FileMsg}=get_log_消息(self(),debug,Format,Args),
gen_事件:sync_notify(错误记录程序,{coach_debug,ConsoleMsg,FileMsg})。
Msg=io_lib:format(格式,Args),
ok=io:format(“[~s][~p]~s~n”,[Level,Pid,Msg]),%dump-to-console-too
Msg2=re:replace(列表:展平(Msg),“\\r\\n |\\r |\\n”,“\r\n”,
[global,{return,list}],
ok=io:format(Fd,“[~s][~s][~p]~s\r~n”,[httpd_util:rfc1123_date(),Level,Pid,Msg2])。
日志(Fd、ConsoleMsg、FileMsg)->
ok=io:put_字符(控制台符号),
ok=io:put_chars(Fd,FileMsg) 你可以试着做得更好 你可以试着做点好事