From 2248a2aad12bc1f270d9c6d8978877dedfb492b3 Mon Sep 17 00:00:00 2001 From: Daniil Fedotov Date: Wed, 9 Dec 2015 15:30:02 +0000 Subject: [PATCH 1/2] Support for separate sing for error_logger messages --- README.md | 17 +++++++++ include/lager.hrl | 2 + src/error_logger_lager_h.erl | 72 ++++++++++++++++++++---------------- 3 files changed, 60 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index eed11b5..47c4c7b 100644 --- a/README.md +++ b/README.md @@ -231,6 +231,23 @@ application variable. If set to `undefined` it is not written at all. Messages in the crash log are subject to a maximum message size which can be specified via the `crash_log_msg_size` application variable. +Messages from `error_logger` will be redirected to `error_logger_lager_event` sink +if it is defined so it can be redirected to another log file. +For example: + +``` +[{lager, [ + {extra_sinks, + [ + {error_logger_lager_event, + [{handlers, [ + {lager_file_backend, [{file, "error_logger.log"}, {level, info}]}] + }] + }] + }] +}]. +``` +Will send all `error_logger` messages to `error_logger.log` file. Overload Protection ------------------- diff --git a/include/lager.hrl b/include/lager.hrl index 82116f0..17ea117 100644 --- a/include/lager.hrl +++ b/include/lager.hrl @@ -18,6 +18,8 @@ -define(DEFAULT_TRUNCATION, 4096). -define(DEFAULT_TRACER, lager_default_tracer). -define(DEFAULT_SINK, lager_event). +-define(ERROR_LOGGER_SINK, error_logger_lager_event). + -define(LEVELS, [debug, info, notice, warning, error, critical, alert, emergency, none]). diff --git a/src/error_logger_lager_h.erl b/src/error_logger_lager_h.erl index 789ffab..45afa9a 100644 --- a/src/error_logger_lager_h.erl +++ b/src/error_logger_lager_h.erl @@ -34,23 +34,24 @@ -export([format_reason/1, format_mfa/1, format_args/3]). -record(state, { + sink :: atom(), shaper :: lager_shaper(), %% group leader strategy groupleader_strategy :: handle | ignore | mirror }). --define(LOGMSG(Level, Pid, Msg), +-define(LOGMSG(Sink, Level, Pid, Msg), case ?SHOULD_LOG(Level) of true -> - _ =lager:log(Level, Pid, Msg), + _ =lager:log(Sink, Level, Pid, Msg, []), ok; _ -> ok end). --define(LOGFMT(Level, Pid, Fmt, Args), +-define(LOGFMT(Sink, Level, Pid, Fmt, Args), case ?SHOULD_LOG(Level) of true -> - _ = lager:log(Level, Pid, Fmt, Args), + _ = lager:log(Sink, Level, Pid, Fmt, Args), ok; _ -> ok end). @@ -71,7 +72,8 @@ set_high_water(N) -> -spec init(any()) -> {ok, #state{}}. init([HighWaterMark, GlStrategy]) -> Shaper = #lager_shaper{hwm=HighWaterMark}, - {ok, #state{shaper=Shaper, groupleader_strategy=GlStrategy}}. + Sink = configured_sink(), + {ok, #state{sink=Sink, shaper=Shaper, groupleader_strategy=GlStrategy}}. handle_call({set_high_water, N}, #state{shaper=Shaper} = State) -> NewShaper = Shaper#lager_shaper{hwm=N}, @@ -79,12 +81,12 @@ handle_call({set_high_water, N}, #state{shaper=Shaper} = State) -> handle_call(_Request, State) -> {ok, unknown_call, State}. -handle_event(Event, #state{shaper=Shaper} = State) -> +handle_event(Event, #state{sink=Sink, shaper=Shaper} = State) -> case lager_util:check_hwm(Shaper) of {true, 0, NewShaper} -> eval_gl(Event, State#state{shaper=NewShaper}); {true, Drop, #lager_shaper{hwm=Hwm} = NewShaper} when Drop > 0 -> - ?LOGFMT(warning, self(), + ?LOGFMT(Sink, warning, self(), "lager_error_logger_h dropped ~p messages in the last second that exceeded the limit of ~p messages/sec", [Drop, Hwm]), eval_gl(Event, State#state{shaper=NewShaper}); @@ -98,11 +100,19 @@ handle_info(_Info, State) -> terminate(_Reason, _State) -> ok. +code_change(_OldVsn, {state, Shaper, GLStrategy}, _Extra) -> + {ok, #state{sink=configured_sink(), shaper=Shaper, groupleader_strategy=GLStrategy}}; code_change(_OldVsn, State, _Extra) -> {ok, State}. %% internal functions +configured_sink() -> + case proplists:get_value(?ERROR_LOGGER_SINK, application:get_env(lager, extra_sinks, [])) of + undefined -> ?DEFAULT_SINK; + _ -> ?ERROR_LOGGER_SINK + end. + eval_gl(Event, #state{groupleader_strategy=GlStrategy0}=State) when is_pid(element(2, Event)) -> case element(2, Event) of GL when node(GL) =/= node(), GlStrategy0 =:= ignore -> @@ -117,7 +127,7 @@ eval_gl(Event, #state{groupleader_strategy=GlStrategy0}=State) when is_pid(eleme eval_gl(Event, State) -> log_event(Event, State). -log_event(Event, State) -> +log_event(Event, #state{sink=Sink} = State) -> case Event of {error, _GL, {Pid, Fmt, Args}} -> case Fmt of @@ -125,19 +135,19 @@ log_event(Event, State) -> %% gen_server terminate [Name, _Msg, _State, Reason] = Args, ?CRASH_LOG(Event), - ?LOGFMT(error, Pid, "gen_server ~w terminated with reason: ~s", + ?LOGFMT(Sink, error, Pid, "gen_server ~w terminated with reason: ~s", [Name, format_reason(Reason)]); "** State machine "++_ -> %% gen_fsm terminate [Name, _Msg, StateName, _StateData, Reason] = Args, ?CRASH_LOG(Event), - ?LOGFMT(error, Pid, "gen_fsm ~w in state ~w terminated with reason: ~s", + ?LOGFMT(Sink, error, Pid, "gen_fsm ~w in state ~w terminated with reason: ~s", [Name, StateName, format_reason(Reason)]); "** gen_event handler"++_ -> %% gen_event handler terminate [ID, Name, _Msg, _State, Reason] = Args, ?CRASH_LOG(Event), - ?LOGFMT(error, Pid, "gen_event ~w installed in ~w terminated with reason: ~s", + ?LOGFMT(Sink, error, Pid, "gen_event ~w installed in ~w terminated with reason: ~s", [ID, Name, format_reason(Reason)]); "** Cowboy handler"++_ -> %% Cowboy HTTP server error @@ -145,13 +155,13 @@ log_event(Event, State) -> case Args of [Module, Function, Arity, _Request, _State] -> %% we only get the 5-element list when its a non-exported function - ?LOGFMT(error, Pid, + ?LOGFMT(Sink, error, Pid, "Cowboy handler ~p terminated with reason: call to undefined function ~p:~p/~p", [Module, Module, Function, Arity]); [Module, Function, Arity, _Class, Reason | Tail] -> %% any other cowboy error_format list *always* ends with the stacktrace StackTrace = lists:last(Tail), - ?LOGFMT(error, Pid, + ?LOGFMT(Sink, error, Pid, "Cowboy handler ~p terminated in ~p:~p/~p with reason: ~s", [Module, Module, Function, Arity, format_reason({Reason, StackTrace})]) end; @@ -160,11 +170,11 @@ log_event(Event, State) -> ?CRASH_LOG(Event), case Args of [Ref, _Protocol, Worker, {[{reason, Reason}, {mfa, {Module, Function, Arity}}, {stacktrace, StackTrace} | _], _}] -> - ?LOGFMT(error, Worker, + ?LOGFMT(Sink, error, Worker, "Ranch listener ~p terminated in ~p:~p/~p with reason: ~s", [Ref, Module, Function, Arity, format_reason({Reason, StackTrace})]); [Ref, _Protocol, Worker, Reason] -> - ?LOGFMT(error, Worker, + ?LOGFMT(Sink, error, Worker, "Ranch listener ~p terminated with reason: ~s", [Ref, format_reason(Reason)]) end; @@ -179,34 +189,34 @@ log_event(Event, State) -> _ -> Error end, - ?LOGFMT(error, Pid, "Webmachine error at path ~p : ~s", [Path, format_reason(StackTrace)]); + ?LOGFMT(Sink, error, Pid, "Webmachine error at path ~p : ~s", [Path, format_reason(StackTrace)]); _ -> ?CRASH_LOG(Event), - ?LOGFMT(error, Pid, Fmt, Args) + ?LOGFMT(Sink, error, Pid, Fmt, Args) end; {error_report, _GL, {Pid, std_error, D}} -> ?CRASH_LOG(Event), - ?LOGMSG(error, Pid, print_silly_list(D)); + ?LOGMSG(Sink, error, Pid, print_silly_list(D)); {error_report, _GL, {Pid, supervisor_report, D}} -> ?CRASH_LOG(Event), case lists:sort(D) of [{errorContext, Ctx}, {offender, Off}, {reason, Reason}, {supervisor, Name}] -> Offender = format_offender(Off), - ?LOGFMT(error, Pid, + ?LOGFMT(Sink, error, Pid, "Supervisor ~w had child ~s exit with reason ~s in context ~w", [supervisor_name(Name), Offender, format_reason(Reason), Ctx]); _ -> - ?LOGMSG(error, Pid, "SUPERVISOR REPORT " ++ print_silly_list(D)) + ?LOGMSG(Sink, error, Pid, "SUPERVISOR REPORT " ++ print_silly_list(D)) end; {error_report, _GL, {Pid, crash_report, [Self, Neighbours]}} -> ?CRASH_LOG(Event), - ?LOGMSG(error, Pid, "CRASH REPORT " ++ format_crash_report(Self, Neighbours)); + ?LOGMSG(Sink, error, Pid, "CRASH REPORT " ++ format_crash_report(Self, Neighbours)); {warning_msg, _GL, {Pid, Fmt, Args}} -> - ?LOGFMT(warning, Pid, Fmt, Args); + ?LOGFMT(Sink, warning, Pid, Fmt, Args); {warning_report, _GL, {Pid, std_warning, Report}} -> - ?LOGMSG(warning, Pid, print_silly_list(Report)); + ?LOGMSG(Sink, warning, Pid, print_silly_list(Report)); {info_msg, _GL, {Pid, Fmt, Args}} -> - ?LOGFMT(info, Pid, Fmt, Args); + ?LOGFMT(Sink, info, Pid, Fmt, Args); {info_report, _GL, {Pid, std_info, D}} when is_list(D) -> Details = lists:sort(D), case Details of @@ -215,14 +225,14 @@ log_event(Event, State) -> {ok, true} when Reason == stopped -> ok; _ -> - ?LOGFMT(info, Pid, "Application ~w exited with reason: ~s", + ?LOGFMT(Sink, info, Pid, "Application ~w exited with reason: ~s", [App, format_reason(Reason)]) end; _ -> - ?LOGMSG(info, Pid, print_silly_list(D)) + ?LOGMSG(Sink, info, Pid, print_silly_list(D)) end; {info_report, _GL, {Pid, std_info, D}} -> - ?LOGFMT(info, Pid, "~w", [D]); + ?LOGFMT(Sink, info, Pid, "~w", [D]); {info_report, _GL, {P, progress, D}} -> Details = lists:sort(D), case Details of @@ -231,19 +241,19 @@ log_event(Event, State) -> {ok, true} -> ok; _ -> - ?LOGFMT(info, P, "Application ~w started on node ~w", + ?LOGFMT(Sink, info, P, "Application ~w started on node ~w", [App, Node]) end; [{started, Started}, {supervisor, Name}] -> MFA = format_mfa(get_value(mfargs, Started)), Pid = get_value(pid, Started), - ?LOGFMT(debug, P, "Supervisor ~w started ~s at pid ~w", + ?LOGFMT(Sink, debug, P, "Supervisor ~w started ~s at pid ~w", [supervisor_name(Name), MFA, Pid]); _ -> - ?LOGMSG(info, P, "PROGRESS REPORT " ++ print_silly_list(D)) + ?LOGMSG(Sink, info, P, "PROGRESS REPORT " ++ print_silly_list(D)) end; _ -> - ?LOGFMT(warning, self(), "Unexpected error_logger event ~w", [Event]) + ?LOGFMT(Sink, warning, self(), "Unexpected error_logger event ~w", [Event]) end, {ok, State}. From 146b0461fd0391078504516d8f68085fda2512ce Mon Sep 17 00:00:00 2001 From: Daniil Fedotov Date: Thu, 10 Dec 2015 12:46:55 +0000 Subject: [PATCH 2/2] Tests and SHOULD_LOG fix --- include/lager.hrl | 3 + src/error_logger_lager_h.erl | 4 +- test/lager_test_backend.erl | 390 ++++++++++++++++++++--------------- 3 files changed, 229 insertions(+), 168 deletions(-) diff --git a/include/lager.hrl b/include/lager.hrl index 17ea117..c42330d 100644 --- a/include/lager.hrl +++ b/include/lager.hrl @@ -63,6 +63,9 @@ ?EMERGENCY -> emergency end). +-define(SHOULD_LOG(Sink, Level), + (lager_util:level_to_num(Level) band element(1, lager_config:get({Sink, loglevel}, {?LOG_NONE, []}))) /= 0). + -define(SHOULD_LOG(Level), (lager_util:level_to_num(Level) band element(1, lager_config:get(loglevel, {?LOG_NONE, []}))) /= 0). diff --git a/src/error_logger_lager_h.erl b/src/error_logger_lager_h.erl index 45afa9a..fc28934 100644 --- a/src/error_logger_lager_h.erl +++ b/src/error_logger_lager_h.erl @@ -41,7 +41,7 @@ }). -define(LOGMSG(Sink, Level, Pid, Msg), - case ?SHOULD_LOG(Level) of + case ?SHOULD_LOG(Sink, Level) of true -> _ =lager:log(Sink, Level, Pid, Msg, []), ok; @@ -49,7 +49,7 @@ end). -define(LOGFMT(Sink, Level, Pid, Fmt, Args), - case ?SHOULD_LOG(Level) of + case ?SHOULD_LOG(Sink, Level) of true -> _ = lager:log(Sink, Level, Pid, Fmt, Args), ok; diff --git a/test/lager_test_backend.erl b/test/lager_test_backend.erl index 3b41ca5..8e8fd48 100644 --- a/test/lager_test_backend.erl +++ b/test/lager_test_backend.erl @@ -785,90 +785,132 @@ test_body(Expected, Actual) -> ?assertEqual(Expected, Actual) end. +error_logger_redirect_crash_setup() -> + error_logger:tty(false), + application:load(lager), + application:set_env(lager, error_logger_redirect, true), + application:set_env(lager, handlers, [{?MODULE, error}]), + lager:start(), + crash:start(), + lager_event. + +error_logger_redirect_crash_setup_sink() -> + error_logger:tty(false), + application:load(lager), + application:set_env(lager, error_logger_redirect, true), + application:unset_env(lager, handlers), + application:set_env(lager, extra_sinks, [ + {error_logger_lager_event, [ + {handlers, [{?MODULE, error}]}]}]), + lager:start(), + crash:start(), + error_logger_lager_event. + +error_logger_redirect_crash_cleanup(_Sink) -> + application:stop(lager), + application:stop(goldrush), + application:unset_env(lager, extra_sinks), + case whereis(crash) of + undefined -> ok; + Pid -> exit(Pid, kill) + end, + error_logger:tty(true). error_logger_redirect_crash_test_() -> - TestBody=fun(Name,CrashReason,Expected) -> {Name, + TestBody=fun(Name,CrashReason,Expected) -> + fun(Sink) -> + {Name, fun() -> Pid = whereis(crash), crash(CrashReason), - {Level, _, Msg,Metadata} = pop(), + {Level, _, Msg,Metadata} = pop(Sink), test_body(Expected, lists:flatten(Msg)), ?assertEqual(Pid,proplists:get_value(pid,Metadata)), ?assertEqual(lager_util:level_to_num(error),Level) end } + end end, - {foreach, - fun() -> - error_logger:tty(false), - application:load(lager), - application:set_env(lager, error_logger_redirect, true), - application:set_env(lager, handlers, [{?MODULE, error}]), - lager:start(), - crash:start() - end, - - fun(_) -> - application:stop(lager), - application:stop(goldrush), - case whereis(crash) of - undefined -> ok; - Pid -> exit(Pid, kill) - end, - error_logger:tty(true) - end, - [ + Tests = [ + fun(Sink) -> {"again, there is nothing up my sleeve", fun() -> - ?assertEqual(undefined, pop()), - ?assertEqual(0, count()) + ?assertEqual(undefined, pop(Sink)), + ?assertEqual(0, count(Sink)) end - }, + } + end, - TestBody("bad return value",bad_return,"gen_server crash terminated with reason: bad return value: bleh"), - TestBody("bad return value with string",bad_return_string,"gen_server crash terminated with reason: bad return value: {tuple,{tuple,\"string\"}}"), - TestBody("bad return uncaught throw",throw,"gen_server crash terminated with reason: bad return value: a_ball"), - TestBody("case clause",case_clause,"gen_server crash terminated with reason: no case clause matching {} in crash:handle_call/3"), - TestBody("case clause string",case_clause_string,"gen_server crash terminated with reason: no case clause matching \"crash\" in crash:handle_call/3"), - TestBody("function clause",function_clause,"gen_server crash terminated with reason: no function clause matching crash:function({})"), - TestBody("if clause",if_clause,"gen_server crash terminated with reason: no true branch found while evaluating if expression in crash:handle_call/3"), - TestBody("try clause",try_clause,"gen_server crash terminated with reason: no try clause matching [] in crash:handle_call/3"), - TestBody("undefined function",undef,"gen_server crash terminated with reason: call to undefined function crash:booger/0 from crash:handle_call/3"), - TestBody("bad math",badarith,"gen_server crash terminated with reason: bad arithmetic expression in crash:handle_call/3"), - TestBody("bad match",badmatch,"gen_server crash terminated with reason: no match of right hand value {} in crash:handle_call/3"), - TestBody("bad arity",badarity,"gen_server crash terminated with reason: fun called with wrong arity of 1 instead of 3 in crash:handle_call/3"), - TestBody("bad arg1",badarg1,"gen_server crash terminated with reason: bad argument in crash:handle_call/3"), - TestBody("bad arg2",badarg2,"gen_server crash terminated with reason: bad argument in call to erlang:iolist_to_binary([\"foo\",bar]) in crash:handle_call/3"), - TestBody("bad record",badrecord,"gen_server crash terminated with reason: bad record state in crash:handle_call/3"), - TestBody("noproc",noproc,"gen_server crash terminated with reason: no such process or port in call to gen_event:call(foo, bar, baz)"), - TestBody("badfun",badfun,"gen_server crash terminated with reason: bad function booger in crash:handle_call/3") - ] - }. + TestBody("bad return value",bad_return,"gen_server crash terminated with reason: bad return value: bleh"), + TestBody("bad return value with string",bad_return_string,"gen_server crash terminated with reason: bad return value: {tuple,{tuple,\"string\"}}"), + TestBody("bad return uncaught throw",throw,"gen_server crash terminated with reason: bad return value: a_ball"), + TestBody("case clause",case_clause,"gen_server crash terminated with reason: no case clause matching {} in crash:handle_call/3"), + TestBody("case clause string",case_clause_string,"gen_server crash terminated with reason: no case clause matching \"crash\" in crash:handle_call/3"), + TestBody("function clause",function_clause,"gen_server crash terminated with reason: no function clause matching crash:function({})"), + TestBody("if clause",if_clause,"gen_server crash terminated with reason: no true branch found while evaluating if expression in crash:handle_call/3"), + TestBody("try clause",try_clause,"gen_server crash terminated with reason: no try clause matching [] in crash:handle_call/3"), + TestBody("undefined function",undef,"gen_server crash terminated with reason: call to undefined function crash:booger/0 from crash:handle_call/3"), + TestBody("bad math",badarith,"gen_server crash terminated with reason: bad arithmetic expression in crash:handle_call/3"), + TestBody("bad match",badmatch,"gen_server crash terminated with reason: no match of right hand value {} in crash:handle_call/3"), + TestBody("bad arity",badarity,"gen_server crash terminated with reason: fun called with wrong arity of 1 instead of 3 in crash:handle_call/3"), + TestBody("bad arg1",badarg1,"gen_server crash terminated with reason: bad argument in crash:handle_call/3"), + TestBody("bad arg2",badarg2,"gen_server crash terminated with reason: bad argument in call to erlang:iolist_to_binary([\"foo\",bar]) in crash:handle_call/3"), + TestBody("bad record",badrecord,"gen_server crash terminated with reason: bad record state in crash:handle_call/3"), + TestBody("noproc",noproc,"gen_server crash terminated with reason: no such process or port in call to gen_event:call(foo, bar, baz)"), + TestBody("badfun",badfun,"gen_server crash terminated with reason: bad function booger in crash:handle_call/3") + ], + {"Error logger redirect crash", [ + {"Redirect to default sink", + {foreach, + fun error_logger_redirect_crash_setup/0, + fun error_logger_redirect_crash_cleanup/1, + Tests}}, + {"Redirect to error_logger_lager_event sink", + {foreach, + fun error_logger_redirect_crash_setup_sink/0, + fun error_logger_redirect_crash_cleanup/1, + Tests}} + ]}. + + +error_logger_redirect_setup() -> + error_logger:tty(false), + application:load(lager), + application:set_env(lager, error_logger_redirect, true), + application:set_env(lager, handlers, [{?MODULE, info}]), + lager:start(), + lager:log(error, self(), "flush flush"), + timer:sleep(100), + gen_event:call(lager_event, ?MODULE, flush), + lager_event. -error_logger_redirect_test_() -> - {foreach, - fun() -> - error_logger:tty(false), - application:load(lager), - application:set_env(lager, error_logger_redirect, true), - application:set_env(lager, handlers, [{?MODULE, info}]), - lager:start(), - lager:log(error, self(), "flush flush"), - timer:sleep(100), - gen_event:call(lager_event, ?MODULE, flush) - end, +error_logger_redirect_setup_sink() -> + error_logger:tty(false), + application:load(lager), + application:set_env(lager, error_logger_redirect, true), + application:unset_env(lager, handlers), + application:set_env(lager, extra_sinks, [ + {error_logger_lager_event, [ + {handlers, [{?MODULE, info}]}]}]), + lager:start(), + lager:log(error_logger_lager_event, error, self(), "flush flush", []), + timer:sleep(100), + gen_event:call(error_logger_lager_event, ?MODULE, flush), + error_logger_lager_event. - fun(_) -> - application:stop(lager), - application:stop(goldrush), - error_logger:tty(true) - end, - [ +error_logger_redirect_cleanup(_) -> + application:stop(lager), + application:stop(goldrush), + application:unset_env(lager, extra_sinks), + error_logger:tty(true). + +error_logger_redirect_test_() -> + Tests = [ {"error reports are printed", - fun() -> + fun(Sink) -> sync_error_logger:error_report([{this, is}, a, {silly, format}]), _ = gen_event:which_handlers(error_logger), - {Level, _, Msg,Metadata} = pop(), + {Level, _, Msg,Metadata} = pop(Sink), ?assertEqual(lager_util:level_to_num(error),Level), ?assertEqual(self(),proplists:get_value(pid,Metadata)), Expected = "this: is, a, silly: format", @@ -877,10 +919,10 @@ error_logger_redirect_test_() -> end }, {"string error reports are printed", - fun() -> + fun(Sink) -> sync_error_logger:error_report("this is less silly"), _ = gen_event:which_handlers(error_logger), - {Level, _, Msg,Metadata} = pop(), + {Level, _, Msg,Metadata} = pop(Sink), ?assertEqual(lager_util:level_to_num(error),Level), ?assertEqual(self(),proplists:get_value(pid,Metadata)), Expected = "this is less silly", @@ -888,10 +930,10 @@ error_logger_redirect_test_() -> end }, {"error messages are printed", - fun() -> + fun(Sink) -> sync_error_logger:error_msg("doom, doom has come upon you all"), _ = gen_event:which_handlers(error_logger), - {Level, _, Msg,Metadata} = pop(), + {Level, _, Msg,Metadata} = pop(Sink), ?assertEqual(lager_util:level_to_num(error),Level), ?assertEqual(self(),proplists:get_value(pid,Metadata)), Expected = "doom, doom has come upon you all", @@ -899,29 +941,29 @@ error_logger_redirect_test_() -> end }, {"error messages with unicode characters in Args are printed", - fun() -> + fun(Sink) -> sync_error_logger:error_msg("~ts", ["Привет!"]), _ = gen_event:which_handlers(error_logger), - {Level, _, Msg,Metadata} = pop(), + {Level, _, Msg,Metadata} = pop(Sink), ?assertEqual(lager_util:level_to_num(error),Level), ?assertEqual(self(),proplists:get_value(pid,Metadata)), ?assertEqual("Привет!", lists:flatten(Msg)) end }, {"error messages are truncated at 4096 characters", - fun() -> + fun(Sink) -> sync_error_logger:error_msg("doom, doom has come upon you all ~p", [string:copies("doom", 10000)]), _ = gen_event:which_handlers(error_logger), - {_, _, Msg,_Metadata} = pop(), + {_, _, Msg,_Metadata} = pop(Sink), ?assert(length(lists:flatten(Msg)) < 5100) end }, {"info reports are printed", - fun() -> + fun(Sink) -> sync_error_logger:info_report([{this, is}, a, {silly, format}]), _ = gen_event:which_handlers(error_logger), - {Level, _, Msg,Metadata} = pop(), + {Level, _, Msg,Metadata} = pop(Sink), ?assertEqual(lager_util:level_to_num(info),Level), ?assertEqual(self(),proplists:get_value(pid,Metadata)), Expected = "this: is, a, silly: format", @@ -929,101 +971,101 @@ error_logger_redirect_test_() -> end }, {"info reports are truncated at 4096 characters", - fun() -> + fun(Sink) -> sync_error_logger:info_report([[{this, is}, a, {silly, format}] || _ <- lists:seq(0, 600)]), _ = gen_event:which_handlers(error_logger), - {Level, _, Msg,Metadata} = pop(), + {Level, _, Msg,Metadata} = pop(Sink), ?assertEqual(lager_util:level_to_num(info),Level), ?assertEqual(self(),proplists:get_value(pid,Metadata)), ?assert(length(lists:flatten(Msg)) < 5000) end }, {"single term info reports are printed", - fun() -> + fun(Sink) -> sync_error_logger:info_report({foolish, bees}), _ = gen_event:which_handlers(error_logger), - {Level, _, Msg,Metadata} = pop(), + {Level, _, Msg,Metadata} = pop(Sink), ?assertEqual(lager_util:level_to_num(info),Level), ?assertEqual(self(),proplists:get_value(pid,Metadata)), ?assertEqual("{foolish,bees}", lists:flatten(Msg)) end }, {"single term error reports are printed", - fun() -> + fun(Sink) -> sync_error_logger:error_report({foolish, bees}), _ = gen_event:which_handlers(error_logger), - {Level, _, Msg,Metadata} = pop(), + {Level, _, Msg,Metadata} = pop(Sink), ?assertEqual(lager_util:level_to_num(error),Level), ?assertEqual(self(),proplists:get_value(pid,Metadata)), ?assertEqual("{foolish,bees}", lists:flatten(Msg)) end }, {"string info reports are printed", - fun() -> + fun(Sink) -> sync_error_logger:info_report("this is less silly"), _ = gen_event:which_handlers(error_logger), - {Level, _, Msg,Metadata} = pop(), + {Level, _, Msg,Metadata} = pop(Sink), ?assertEqual(lager_util:level_to_num(info),Level), ?assertEqual(self(),proplists:get_value(pid,Metadata)), ?assertEqual("this is less silly", lists:flatten(Msg)) end }, {"string info reports are truncated at 4096 characters", - fun() -> + fun(Sink) -> sync_error_logger:info_report(string:copies("this is less silly", 1000)), _ = gen_event:which_handlers(error_logger), - {Level, _, Msg,Metadata} = pop(), + {Level, _, Msg,Metadata} = pop(Sink), ?assertEqual(lager_util:level_to_num(info),Level), ?assertEqual(self(),proplists:get_value(pid,Metadata)), ?assert(length(lists:flatten(Msg)) < 5100) end }, {"strings in a mixed report are printed as strings", - fun() -> + fun(Sink) -> sync_error_logger:info_report(["this is less silly", {than, "this"}]), _ = gen_event:which_handlers(error_logger), - {Level, _, Msg,Metadata} = pop(), + {Level, _, Msg,Metadata} = pop(Sink), ?assertEqual(lager_util:level_to_num(info),Level), ?assertEqual(self(),proplists:get_value(pid,Metadata)), ?assertEqual("\"this is less silly\", than: \"this\"", lists:flatten(Msg)) end }, {"info messages are printed", - fun() -> + fun(Sink) -> sync_error_logger:info_msg("doom, doom has come upon you all"), _ = gen_event:which_handlers(error_logger), - {Level, _, Msg,Metadata} = pop(), + {Level, _, Msg,Metadata} = pop(Sink), ?assertEqual(lager_util:level_to_num(info),Level), ?assertEqual(self(),proplists:get_value(pid,Metadata)), ?assertEqual("doom, doom has come upon you all", lists:flatten(Msg)) end }, {"info messages are truncated at 4096 characters", - fun() -> + fun(Sink) -> sync_error_logger:info_msg("doom, doom has come upon you all ~p", [string:copies("doom", 10000)]), _ = gen_event:which_handlers(error_logger), - {Level, _, Msg,Metadata} = pop(), + {Level, _, Msg,Metadata} = pop(Sink), ?assertEqual(lager_util:level_to_num(info),Level), ?assertEqual(self(),proplists:get_value(pid,Metadata)), ?assert(length(lists:flatten(Msg)) < 5100) end }, {"info messages with unicode characters in Args are printed", - fun() -> + fun(Sink) -> sync_error_logger:info_msg("~ts", ["Привет!"]), _ = gen_event:which_handlers(error_logger), - {Level, _, Msg,Metadata} = pop(), + {Level, _, Msg,Metadata} = pop(Sink), ?assertEqual(lager_util:level_to_num(info),Level), ?assertEqual(self(),proplists:get_value(pid,Metadata)), ?assertEqual("Привет!", lists:flatten(Msg)) end }, {"warning messages with unicode characters in Args are printed", - fun() -> + fun(Sink) -> sync_error_logger:warning_msg("~ts", ["Привет!"]), Map = error_logger:warning_map(), _ = gen_event:which_handlers(error_logger), - {Level, _, Msg,Metadata} = pop(), + {Level, _, Msg,Metadata} = pop(Sink), ?assertEqual(lager_util:level_to_num(Map),Level), ?assertEqual(self(),proplists:get_value(pid,Metadata)), ?assertEqual("Привет!", lists:flatten(Msg)) @@ -1031,63 +1073,63 @@ error_logger_redirect_test_() -> }, {"warning messages are printed at the correct level", - fun() -> + fun(Sink) -> sync_error_logger:warning_msg("doom, doom has come upon you all"), Map = error_logger:warning_map(), _ = gen_event:which_handlers(error_logger), - {Level, _, Msg,Metadata} = pop(), + {Level, _, Msg,Metadata} = pop(Sink), ?assertEqual(lager_util:level_to_num(Map),Level), ?assertEqual(self(),proplists:get_value(pid,Metadata)), ?assertEqual("doom, doom has come upon you all", lists:flatten(Msg)) end }, {"warning reports are printed at the correct level", - fun() -> + fun(Sink) -> sync_error_logger:warning_report([{i, like}, pie]), Map = error_logger:warning_map(), _ = gen_event:which_handlers(error_logger), - {Level, _, Msg,Metadata} = pop(), + {Level, _, Msg,Metadata} = pop(Sink), ?assertEqual(lager_util:level_to_num(Map),Level), ?assertEqual(self(),proplists:get_value(pid,Metadata)), ?assertEqual("i: like, pie", lists:flatten(Msg)) end }, {"single term warning reports are printed at the correct level", - fun() -> + fun(Sink) -> sync_error_logger:warning_report({foolish, bees}), Map = error_logger:warning_map(), _ = gen_event:which_handlers(error_logger), - {Level, _, Msg,Metadata} = pop(), + {Level, _, Msg,Metadata} = pop(Sink), ?assertEqual(lager_util:level_to_num(Map),Level), ?assertEqual(self(),proplists:get_value(pid,Metadata)), ?assertEqual("{foolish,bees}", lists:flatten(Msg)) end }, {"application stop reports", - fun() -> + fun(Sink) -> sync_error_logger:info_report([{application, foo}, {exited, quittin_time}, {type, lazy}]), _ = gen_event:which_handlers(error_logger), - {Level, _, Msg,Metadata} = pop(), + {Level, _, Msg,Metadata} = pop(Sink), ?assertEqual(lager_util:level_to_num(info),Level), ?assertEqual(self(),proplists:get_value(pid,Metadata)), ?assertEqual("Application foo exited with reason: quittin_time", lists:flatten(Msg)) end }, {"supervisor reports", - fun() -> + fun(Sink) -> sync_error_logger:error_report(supervisor_report, [{errorContext, france}, {offender, [{name, mini_steve}, {mfargs, {a, b, [c]}}, {pid, bleh}]}, {reason, fired}, {supervisor, {local, steve}}]), _ = gen_event:which_handlers(error_logger), - {Level, _, Msg,Metadata} = pop(), + {Level, _, Msg,Metadata} = pop(Sink), ?assertEqual(lager_util:level_to_num(error),Level), ?assertEqual(self(),proplists:get_value(pid,Metadata)), ?assertEqual("Supervisor steve had child mini_steve started with a:b(c) at bleh exit with reason fired in context france", lists:flatten(Msg)) end }, {"supervisor reports with real error", - fun() -> + fun(Sink) -> sync_error_logger:error_report(supervisor_report, [{errorContext, france}, {offender, [{name, mini_steve}, {mfargs, {a, b, [c]}}, {pid, bleh}]}, {reason, {function_clause,[{crash,handle_info,[foo]}]}}, {supervisor, {local, steve}}]), _ = gen_event:which_handlers(error_logger), - {Level, _, Msg,Metadata} = pop(), + {Level, _, Msg,Metadata} = pop(Sink), ?assertEqual(lager_util:level_to_num(error),Level), ?assertEqual(self(),proplists:get_value(pid,Metadata)), ?assertEqual("Supervisor steve had child mini_steve started with a:b(c) at bleh exit with reason no function clause matching crash:handle_info(foo) in context france", lists:flatten(Msg)) @@ -1095,10 +1137,10 @@ error_logger_redirect_test_() -> }, {"supervisor reports with real error and pid", - fun() -> + fun(Sink) -> sync_error_logger:error_report(supervisor_report, [{errorContext, france}, {offender, [{name, mini_steve}, {mfargs, {a, b, [c]}}, {pid, bleh}]}, {reason, {function_clause,[{crash,handle_info,[foo]}]}}, {supervisor, somepid}]), _ = gen_event:which_handlers(error_logger), - {Level, _, Msg,Metadata} = pop(), + {Level, _, Msg,Metadata} = pop(Sink), ?assertEqual(lager_util:level_to_num(error),Level), ?assertEqual(self(),proplists:get_value(pid,Metadata)), ?assertEqual("Supervisor somepid had child mini_steve started with a:b(c) at bleh exit with reason no function clause matching crash:handle_info(foo) in context france", lists:flatten(Msg)) @@ -1106,20 +1148,20 @@ error_logger_redirect_test_() -> }, {"supervisor_bridge reports", - fun() -> + fun(Sink) -> sync_error_logger:error_report(supervisor_report, [{errorContext, france}, {offender, [{mod, mini_steve}, {pid, bleh}]}, {reason, fired}, {supervisor, {local, steve}}]), _ = gen_event:which_handlers(error_logger), - {Level, _, Msg,Metadata} = pop(), + {Level, _, Msg,Metadata} = pop(Sink), ?assertEqual(lager_util:level_to_num(error),Level), ?assertEqual(self(),proplists:get_value(pid,Metadata)), ?assertEqual("Supervisor steve had child at module mini_steve at bleh exit with reason fired in context france", lists:flatten(Msg)) end }, {"application progress report", - fun() -> + fun(Sink) -> sync_error_logger:info_report(progress, [{application, foo}, {started_at, node()}]), _ = gen_event:which_handlers(error_logger), - {Level, _, Msg,Metadata} = pop(), + {Level, _, Msg,Metadata} = pop(Sink), ?assertEqual(lager_util:level_to_num(info),Level), ?assertEqual(self(),proplists:get_value(pid,Metadata)), Expected = lists:flatten(io_lib:format("Application foo started on node ~w", [node()])), @@ -1127,34 +1169,34 @@ error_logger_redirect_test_() -> end }, {"supervisor progress report", - fun() -> - lager:set_loglevel(?MODULE, debug), - ?assertEqual({?DEBUG bor ?INFO bor ?NOTICE bor ?WARNING bor ?ERROR bor ?CRITICAL bor ?ALERT bor ?EMERGENCY, []}, lager_config:get(loglevel)), + fun(Sink) -> + lager:set_loglevel(Sink, ?MODULE, undefined, debug), + ?assertEqual({?DEBUG bor ?INFO bor ?NOTICE bor ?WARNING bor ?ERROR bor ?CRITICAL bor ?ALERT bor ?EMERGENCY, []}, lager_config:get({Sink, loglevel})), sync_error_logger:info_report(progress, [{supervisor, {local, foo}}, {started, [{mfargs, {foo, bar, 1}}, {pid, baz}]}]), _ = gen_event:which_handlers(error_logger), - {Level, _, Msg,Metadata} = pop(), + {Level, _, Msg,Metadata} = pop(Sink), ?assertEqual(lager_util:level_to_num(debug),Level), ?assertEqual(self(),proplists:get_value(pid,Metadata)), ?assertEqual("Supervisor foo started foo:bar/1 at pid baz", lists:flatten(Msg)) end }, {"supervisor progress report with pid", - fun() -> - lager:set_loglevel(?MODULE, debug), - ?assertEqual({?DEBUG bor ?INFO bor ?NOTICE bor ?WARNING bor ?ERROR bor ?CRITICAL bor ?ALERT bor ?EMERGENCY, []}, lager_config:get(loglevel)), + fun(Sink) -> + lager:set_loglevel(Sink, ?MODULE, undefined, debug), + ?assertEqual({?DEBUG bor ?INFO bor ?NOTICE bor ?WARNING bor ?ERROR bor ?CRITICAL bor ?ALERT bor ?EMERGENCY, []}, lager_config:get({Sink, loglevel})), sync_error_logger:info_report(progress, [{supervisor, somepid}, {started, [{mfargs, {foo, bar, 1}}, {pid, baz}]}]), _ = gen_event:which_handlers(error_logger), - {Level, _, Msg,Metadata} = pop(), + {Level, _, Msg,Metadata} = pop(Sink), ?assertEqual(lager_util:level_to_num(debug),Level), ?assertEqual(self(),proplists:get_value(pid,Metadata)), ?assertEqual("Supervisor somepid started foo:bar/1 at pid baz", lists:flatten(Msg)) end }, {"crash report for emfile", - fun() -> + fun(Sink) -> sync_error_logger:error_report(crash_report, [[{pid, self()}, {registered_name, []}, {error_info, {error, emfile, [{stack, trace, 1}]}}], []]), _ = gen_event:which_handlers(error_logger), - {Level, _, Msg,Metadata} = pop(), + {Level, _, Msg,Metadata} = pop(Sink), ?assertEqual(lager_util:level_to_num(error),Level), ?assertEqual(self(),proplists:get_value(pid,Metadata)), Expected = lists:flatten(io_lib:format("CRASH REPORT Process ~w with 0 neighbours crashed with reason: maximum number of file descriptors exhausted, check ulimit -n", [self()])), @@ -1162,10 +1204,10 @@ error_logger_redirect_test_() -> end }, {"crash report for system process limit", - fun() -> + fun(Sink) -> sync_error_logger:error_report(crash_report, [[{pid, self()}, {registered_name, []}, {error_info, {error, system_limit, [{erlang, spawn, 1}]}}], []]), _ = gen_event:which_handlers(error_logger), - {Level, _, Msg,Metadata} = pop(), + {Level, _, Msg,Metadata} = pop(Sink), ?assertEqual(lager_util:level_to_num(error),Level), ?assertEqual(self(),proplists:get_value(pid,Metadata)), Expected = lists:flatten(io_lib:format("CRASH REPORT Process ~w with 0 neighbours crashed with reason: system limit: maximum number of processes exceeded", [self()])), @@ -1173,10 +1215,10 @@ error_logger_redirect_test_() -> end }, {"crash report for system process limit2", - fun() -> + fun(Sink) -> sync_error_logger:error_report(crash_report, [[{pid, self()}, {registered_name, []}, {error_info, {error, system_limit, [{erlang, spawn_opt, 1}]}}], []]), _ = gen_event:which_handlers(error_logger), - {Level, _, Msg,Metadata} = pop(), + {Level, _, Msg,Metadata} = pop(Sink), ?assertEqual(lager_util:level_to_num(error),Level), ?assertEqual(self(),proplists:get_value(pid,Metadata)), Expected = lists:flatten(io_lib:format("CRASH REPORT Process ~w with 0 neighbours crashed with reason: system limit: maximum number of processes exceeded", [self()])), @@ -1184,10 +1226,10 @@ error_logger_redirect_test_() -> end }, {"crash report for system port limit", - fun() -> + fun(Sink) -> sync_error_logger:error_report(crash_report, [[{pid, self()}, {registered_name, []}, {error_info, {error, system_limit, [{erlang, open_port, 1}]}}], []]), _ = gen_event:which_handlers(error_logger), - {Level, _, Msg,Metadata} = pop(), + {Level, _, Msg,Metadata} = pop(Sink), ?assertEqual(lager_util:level_to_num(error),Level), ?assertEqual(self(),proplists:get_value(pid,Metadata)), Expected = lists:flatten(io_lib:format("CRASH REPORT Process ~w with 0 neighbours crashed with reason: system limit: maximum number of ports exceeded", [self()])), @@ -1195,10 +1237,10 @@ error_logger_redirect_test_() -> end }, {"crash report for system port limit", - fun() -> + fun(Sink) -> sync_error_logger:error_report(crash_report, [[{pid, self()}, {registered_name, []}, {error_info, {error, system_limit, [{erlang, list_to_atom, 1}]}}], []]), _ = gen_event:which_handlers(error_logger), - {Level, _, Msg,Metadata} = pop(), + {Level, _, Msg,Metadata} = pop(Sink), ?assertEqual(lager_util:level_to_num(error),Level), ?assertEqual(self(),proplists:get_value(pid,Metadata)), Expected = lists:flatten(io_lib:format("CRASH REPORT Process ~w with 0 neighbours crashed with reason: system limit: tried to create an atom larger than 255, or maximum atom count exceeded", [self()])), @@ -1206,10 +1248,10 @@ error_logger_redirect_test_() -> end }, {"crash report for system ets table limit", - fun() -> + fun(Sink) -> sync_error_logger:error_report(crash_report, [[{pid, self()}, {registered_name, test}, {error_info, {error, system_limit, [{ets,new,[segment_offsets,[ordered_set,public]]},{mi_segment,open_write,1},{mi_buffer_converter,handle_cast,2},{gen_server,handle_msg,5},{proc_lib,init_p_do_apply,3}]}}], []]), _ = gen_event:which_handlers(error_logger), - {Level, _, Msg,Metadata} = pop(), + {Level, _, Msg,Metadata} = pop(Sink), ?assertEqual(lager_util:level_to_num(error),Level), ?assertEqual(self(),proplists:get_value(pid,Metadata)), Expected = lists:flatten(io_lib:format("CRASH REPORT Process ~w with 0 neighbours crashed with reason: system limit: maximum number of ETS tables exceeded", [test])), @@ -1217,75 +1259,75 @@ error_logger_redirect_test_() -> end }, {"crash report for unknown system limit should be truncated at 500 characters", - fun() -> + fun(Sink) -> sync_error_logger:error_report(crash_report, [[{pid, self()}, {error_info, {error, system_limit, [{wtf,boom,[string:copies("aaaa", 4096)]}]}}], []]), _ = gen_event:which_handlers(error_logger), - {_, _, Msg,_Metadata} = pop(), + {_, _, Msg,_Metadata} = pop(Sink), ?assert(length(lists:flatten(Msg)) > 550), ?assert(length(lists:flatten(Msg)) < 600) end }, {"crash reports for 'special processes' should be handled right - function_clause", - fun() -> + fun(Sink) -> {ok, Pid} = special_process:start(), unlink(Pid), Pid ! function_clause, timer:sleep(500), _ = gen_event:which_handlers(error_logger), - {_, _, Msg, _Metadata} = pop(), + {_, _, Msg, _Metadata} = pop(Sink), Expected = lists:flatten(io_lib:format("CRASH REPORT Process ~p with 0 neighbours crashed with reason: no function clause matching special_process:foo(bar)", [Pid])), test_body(Expected, lists:flatten(Msg)) end }, {"crash reports for 'special processes' should be handled right - case_clause", - fun() -> + fun(Sink) -> {ok, Pid} = special_process:start(), unlink(Pid), Pid ! {case_clause, wtf}, timer:sleep(500), _ = gen_event:which_handlers(error_logger), - {_, _, Msg, _Metadata} = pop(), + {_, _, Msg, _Metadata} = pop(Sink), Expected = lists:flatten(io_lib:format("CRASH REPORT Process ~p with 0 neighbours crashed with reason: no case clause matching wtf in special_process:loop/0", [Pid])), test_body(Expected, lists:flatten(Msg)) end }, {"crash reports for 'special processes' should be handled right - exit", - fun() -> + fun(Sink) -> {ok, Pid} = special_process:start(), unlink(Pid), Pid ! exit, timer:sleep(500), _ = gen_event:which_handlers(error_logger), - {_, _, Msg, _Metadata} = pop(), + {_, _, Msg, _Metadata} = pop(Sink), Expected = lists:flatten(io_lib:format("CRASH REPORT Process ~p with 0 neighbours exited with reason: byebye in special_process:loop/0", [Pid])), test_body(Expected, lists:flatten(Msg)) end }, {"crash reports for 'special processes' should be handled right - error", - fun() -> + fun(Sink) -> {ok, Pid} = special_process:start(), unlink(Pid), Pid ! error, timer:sleep(500), _ = gen_event:which_handlers(error_logger), - {_, _, Msg, _Metadata} = pop(), + {_, _, Msg, _Metadata} = pop(Sink), Expected = lists:flatten(io_lib:format("CRASH REPORT Process ~p with 0 neighbours crashed with reason: mybad in special_process:loop/0", [Pid])), test_body(Expected, lists:flatten(Msg)) end }, {"webmachine error reports", - fun() -> + fun(Sink) -> Path = "/cgi-bin/phpmyadmin", Reason = {error,{error,{badmatch,{error,timeout}}, [{myapp,dostuff,2,[{file,"src/myapp.erl"},{line,123}]}, {webmachine_resource,resource_call,3,[{file,"src/webmachine_resource.erl"},{line,169}]}]}}, sync_error_logger:error_msg("webmachine error: path=~p~n~p~n", [Path, Reason]), _ = gen_event:which_handlers(error_logger), - {Level, _, Msg,Metadata} = pop(), + {Level, _, Msg,Metadata} = pop(Sink), ?assertEqual(lager_util:level_to_num(error),Level), ?assertEqual(self(),proplists:get_value(pid,Metadata)), ?assertEqual("Webmachine error at path \"/cgi-bin/phpmyadmin\" : no match of right hand value {error,timeout} in myapp:dostuff/2 line 123", lists:flatten(Msg)) @@ -1293,7 +1335,7 @@ error_logger_redirect_test_() -> end }, {"Cowboy error reports, 8 arg version", - fun() -> + fun(Sink) -> Stack = [{my_handler,init, 3,[{file,"src/my_handler.erl"},{line,123}]}, {cowboy_handler,handler_init,4,[{file,"src/cowboy_handler.erl"},{line,169}]}], @@ -1306,14 +1348,14 @@ error_logger_redirect_test_() -> [my_handler, init, 3, error, {badmatch, {error, timeout}}, [], "Request", Stack]), _ = gen_event:which_handlers(error_logger), - {Level, _, Msg,Metadata} = pop(), + {Level, _, Msg,Metadata} = pop(Sink), ?assertEqual(lager_util:level_to_num(error),Level), ?assertEqual(self(),proplists:get_value(pid,Metadata)), ?assertEqual("Cowboy handler my_handler terminated in my_handler:init/3 with reason: no match of right hand value {error,timeout} in my_handler:init/3 line 123", lists:flatten(Msg)) end }, {"Cowboy error reports, 10 arg version", - fun() -> + fun(Sink) -> Stack = [{my_handler,somecallback, 3,[{file,"src/my_handler.erl"},{line,123}]}, {cowboy_handler,handler_init,4,[{file,"src/cowboy_handler.erl"},{line,169}]}], sync_error_logger:error_msg( @@ -1325,51 +1367,67 @@ error_logger_redirect_test_() -> {}, "Request", Stack]), _ = gen_event:which_handlers(error_logger), - {Level, _, Msg,Metadata} = pop(), + {Level, _, Msg,Metadata} = pop(Sink), ?assertEqual(lager_util:level_to_num(error),Level), ?assertEqual(self(),proplists:get_value(pid,Metadata)), ?assertEqual("Cowboy handler my_handler terminated in my_handler:somecallback/3 with reason: no match of right hand value {error,timeout} in my_handler:somecallback/3 line 123", lists:flatten(Msg)) end }, {"Cowboy error reports, 5 arg version", - fun() -> + fun(Sink) -> sync_error_logger:error_msg( "** Cowboy handler ~p terminating; " "function ~p/~p was not exported~n" "** Request was ~p~n** State was ~p~n~n", [my_handler, to_json, 2, "Request", {}]), _ = gen_event:which_handlers(error_logger), - {Level, _, Msg,Metadata} = pop(), + {Level, _, Msg,Metadata} = pop(Sink), ?assertEqual(lager_util:level_to_num(error),Level), ?assertEqual(self(),proplists:get_value(pid,Metadata)), ?assertEqual("Cowboy handler my_handler terminated with reason: call to undefined function my_handler:to_json/2", lists:flatten(Msg)) end }, {"messages should not be generated if they don't satisfy the threshold", - fun() -> - lager:set_loglevel(?MODULE, error), - ?assertEqual({?ERROR bor ?CRITICAL bor ?ALERT bor ?EMERGENCY, []}, lager_config:get(loglevel)), + fun(Sink) -> + lager:set_loglevel(Sink, ?MODULE, undefined, error), + ?assertEqual({?ERROR bor ?CRITICAL bor ?ALERT bor ?EMERGENCY, []}, lager_config:get({Sink, loglevel})), sync_error_logger:info_report([hello, world]), _ = gen_event:which_handlers(error_logger), - ?assertEqual(0, count()), - ?assertEqual(0, count_ignored()), - lager:set_loglevel(?MODULE, info), - ?assertEqual({?INFO bor ?NOTICE bor ?WARNING bor ?ERROR bor ?CRITICAL bor ?ALERT bor ?EMERGENCY, []}, lager_config:get(loglevel)), + ?assertEqual(0, count(Sink)), + ?assertEqual(0, count_ignored(Sink)), + lager:set_loglevel(Sink, ?MODULE, undefined, info), + ?assertEqual({?INFO bor ?NOTICE bor ?WARNING bor ?ERROR bor ?CRITICAL bor ?ALERT bor ?EMERGENCY, []}, lager_config:get({Sink, loglevel})), sync_error_logger:info_report([hello, world]), _ = gen_event:which_handlers(error_logger), - ?assertEqual(1, count()), - ?assertEqual(0, count_ignored()), - lager:set_loglevel(?MODULE, error), - ?assertEqual({?ERROR bor ?CRITICAL bor ?ALERT bor ?EMERGENCY, []}, lager_config:get(loglevel)), - lager_config:set(loglevel, {element(2, lager_util:config_to_mask(debug)), []}), + ?assertEqual(1, count(Sink)), + ?assertEqual(0, count_ignored(Sink)), + lager:set_loglevel(Sink, ?MODULE, undefined, error), + ?assertEqual({?ERROR bor ?CRITICAL bor ?ALERT bor ?EMERGENCY, []}, lager_config:get({Sink, loglevel})), + lager_config:set({Sink, loglevel}, {element(2, lager_util:config_to_mask(debug)), []}), sync_error_logger:info_report([hello, world]), _ = gen_event:which_handlers(error_logger), - ?assertEqual(1, count()), - ?assertEqual(1, count_ignored()) + ?assertEqual(1, count(Sink)), + ?assertEqual(1, count_ignored(Sink)) end } - ] - }. + ], + SinkTests = lists:map( + fun({Name, F}) -> + fun(Sink) -> {Name, fun() -> F(Sink) end} end + end, + Tests), + {"Error logger redirect", [ + {"Redirect to default sink", + {foreach, + fun error_logger_redirect_setup/0, + fun error_logger_redirect_cleanup/1, + SinkTests}}, + {"Redirect to error_logger_lager_event sink", + {foreach, + fun error_logger_redirect_setup_sink/0, + fun error_logger_redirect_cleanup/1, + SinkTests}} + ]}. safe_format_test() -> ?assertEqual("foo bar", lists:flatten(lager:safe_format("~p ~p", [foo, bar], 1024))),