From 65cd0e43158e38a3c42b00e7f591723226d235fb Mon Sep 17 00:00:00 2001
From: Lukas Larsson A simple handler which prints to file could be implemented like
@@ -562,18 +558,13 @@ removing_handler(Id,#{myhandler_fd:=Fd}) ->
_ = file:close(Fd),
ok.
-log(#{msg:={report,R}},#{myhandler_fd:=Fd}) ->
- io:format(Fd,"~p~n",[R]);
-log(#{msg:={string,S}},#{myhandler_fd:=Fd}) ->
- io:put_chars(Fd,S);
-log(#{msg:={F,A}},#{myhandler_fd:=Fd}) ->
- io:format(Fd,F,A).
+log(Log,#{myhandler_fd:=Fd,formatter:={FModule,FConfig}}) ->
+ io:put_chars(Fd,FModule:format(Log,FConfig)).
- Note that none of the above handlers have any overload
+ The above handlers do not have any overload
protection, and all log events are printed directly from the
- client process. Neither do the handlers use the formatter or
- in any way add time or other metadata to the printed events. For examples of overload protection, please refer to the
implementation
@@ -582,14 +573,7 @@ log(#{msg:={F,A}},#{myhandler_fd:=Fd}) ->
. Below is a simpler example of a handler which logs through one
- single process, and uses the default formatter to gain a common
- look of the log events. It also uses the metadata field
-module(myhandler).
-export([adding_handler/2, removing_handler/1, log/2]).
@@ -620,16 +604,9 @@ terminate(Reason,#{fd:=Fd}) ->
_ = file:close(Fd),
ok.
-do_log(Fd,#{msg:={report,R}} = Log, Config) ->
- Fun = maps:get(report_cb,Config,fun my_report_cb/1,
- {F,A} = Fun(R),
- do_log(Fd,Log#{msg=>{F,A},Config);
do_log(Fd,Log,#{formatter:={FModule,FConfig}}) ->
String = FModule:format(Log,FConfig),
io:put_chars(Fd,String).
-
-my_report_cb(R) ->
- {"~p",[R]}.
--
cgit v1.2.3