【问题标题】:Erlang error logging in plaintext以纯文本形式记录 Erlang 错误
【发布时间】:2012-02-25 21:40:49
【问题描述】:

我目前有一个使用 SASL 错误日志记录的 OTP 应用程序。日志转到我必须使用 rb 库读取的二进制编码文件。这是一个很大的痛苦,因为我真正想要的是能够将所有日志导出为纯文本并通过它们猛烈抨击。我在一个旧邮件列表上发现了一条评论,其中提供了一些指向备用 error_loggers 的链接:

http://erlang.2086793.n4.nabble.com/Lots-of-questions-about-error-logger-td2109935.html

但是代码已经过时并且当我尝试编译它时会抛出大量错误。是否有任何更新的库可用于将 SASL 消息发送到纯文本文件?

【问题讨论】:

    标签: logging erlang


    【解决方案1】:

    你可以试试 basho 的lager,这是一个很好的 erlang 登录库。

    问候。

    【讨论】:

      【解决方案2】:

      您可以使用 sasl 事件框架来创建自己的事件处理程序。 详细说明在《Manning.Erlang and Otp in action, part 2 Chapter 7, page 170(可能因版本不同)》一书中以 erlang/otp 方式记录和事件处理

      实际代码请阅读couchdb的http://couchdb.apache.org/downloads.html源代码文件“couch_log.erl”和“couch_event_sup.erl”,第一个文件是事件处理程序,第二个文件是“couch_log”的gen_server包装模块。

      以下代码是从“couch_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
      

      【讨论】:

        【解决方案3】:

        您可以试试 alogger 以获得更好的体验!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-04-26
          • 2020-12-19
          • 1970-01-01
          • 1970-01-01
          • 2013-12-31
          • 2015-12-08
          相关资源
          最近更新 更多