Browse Source

Restart error_logger handler if it exits, cleanups

pull/4/head
Andrew Thompson 14 years ago
parent
commit
1f3493df12
2 changed files with 14 additions and 18 deletions
  1. +7
    -5
      src/error_logger_lager_h.erl
  2. +7
    -13
      src/lager.erl

+ 7
- 5
src/error_logger_lager_h.erl View File

@ -21,6 +21,8 @@
-export([init/1, handle_call/2, handle_event/2, handle_info/2, terminate/2,
code_change/3]).
-export([format_reason/1]).
init(_) ->
{ok, {}}.
@ -57,10 +59,10 @@ handle_event(Event, State) ->
Offender = format_offender(Off),
lager:log(error, Pid, "Supervisor ~w had child ~s exit with reason ~w in context ~w", [element(2, Name), Offender, Reason, Ctx]);
_ ->
lager:log(error, Pid, "SUPERVISOR REPORT" ++ print_silly_list(D))
lager:log(error, Pid, "SUPERVISOR REPORT " ++ print_silly_list(D))
end;
{error_report, _GL, {Pid, crash_report, [Self, Neighbours]}} ->
lager:log(error, Pid, "CRASH REPORT " ++ format_crash_report(Self));
lager:log(error, Pid, "CRASH REPORT " ++ format_crash_report(Self, Neighbours));
{info_msg, _GL, {Pid, Fmt, Args}} ->
lager:log(info, Pid, Fmt, Args);
{info_report, _GL, {Pid, std_info, D}} ->
@ -82,7 +84,7 @@ handle_event(Event, State) ->
Pid = proplists:get_value(pid, Started),
lager:log(info, P, "Supervisor ~w started ~s at pid ~w", [element(2, Name), MFA, Pid]);
_ ->
lager:log(info, P, "PROGRESS REPORT" ++ print_silly_list(D))
lager:log(info, P, "PROGRESS REPORT " ++ print_silly_list(D))
end;
_ ->
io:format("Event ~w~n", [Event])
@ -100,10 +102,10 @@ code_change(_OldVsn, State, _Extra) ->
%% internal functions
format_crash_report(Report) ->
format_crash_report(Report, Neighbours) ->
Name = proplists:get_value(registered_name, Report, proplists:get_value(pid, Report)),
{_Class, Reason, _Trace} = proplists:get_value(error_info, Report),
io_lib:format("Process ~w crashed with reason: ~s", [Name, format_reason(Reason)]).
io_lib:format("Process ~w with ~w neighbours crashed with reason: ~s", [Name, length(Neighbours), format_reason(Reason)]).
format_offender(Off) ->
case proplists:get_value(name, Off) of

+ 7
- 13
src/lager.erl View File

@ -96,7 +96,6 @@ get_loglevel(Handler) ->
%% gen_server callbacks
init([Handlers]) ->
%process_flag(trap_exit, true),
%% start a gen_event linked to this process
gen_event:start_link({local, lager_event}),
%% spin up all the defined handlers
@ -107,10 +106,9 @@ init([Handlers]) ->
{ok, true} ->
gen_event:add_sup_handler(error_logger, error_logger_lager_h, []),
%% TODO allow user to whitelist handlers to not be removed
Removed = [begin gen_event:delete_handler(error_logger, X, {stop_please, ?MODULE}), X end ||
[gen_event:delete_handler(error_logger, X, {stop_please, ?MODULE}) ||
X <- gen_event:which_handlers(error_logger) -- [error_logger_lager_h]],
io:format("Removed handlers ~p~n", [Removed]),
{ok, #state{error_logger_handlers=Removed}};
{ok, #state{}};
_ ->
{ok, #state{}}
end.
@ -136,20 +134,16 @@ handle_call(_Request, _From, State) ->
handle_cast(_Request, State) ->
{noreply, State}.
handle_info({gen_event_EXIT, error_logger_lager_h, {'EXIT', Reason}}, State) ->
lager:log(error, self(), ["Restarting lager error handler after it exited with ", error_logger_lager_h:format_reason(Reason)]),
gen_event:add_sup_handler(error_logger, error_logger_lager_h, []),
{noreply, State};
handle_info(Info, State) ->
io:format("got info ~p~n", [Info]),
{noreply, State}.
terminate(_Reason, #state{error_logger_handlers = Handlers}) ->
io:format("terminate ~p~n", Handlers),
terminate(_Reason, _State) ->
gen_event:stop(lager_event),
case Handlers of
undefined ->
ok;
_ ->
io:format("Reinstalling handlers ~p~n", [Handlers]),
[gen_event:add_handler(error_logger, X, []) || X <- Handlers]
end,
ok.
code_change(_OldVsn, State, _Extra) ->

Loading…
Cancel
Save