Browse Source

Rework the 'old shell' warning, and some related work

Add ability for handlers to 'fatally' fail, so they won't attempt to
reinstall themselves later.

Also fix an issue with the INT_LOG macro not doing the right thing when
only the lager_throttle_backend was installed.
pull/139/head
Andrew Thompson 12 years ago
parent
commit
ccdc4d4c8f
5 changed files with 32 additions and 32 deletions
  1. +1
    -1
      include/lager.hrl
  2. +13
    -23
      src/lager_console_backend.erl
  3. +1
    -1
      src/lager_file_backend.erl
  4. +16
    -6
      src/lager_handler_watcher.erl
  5. +1
    -1
      src/lager_handler_watcher_sup.erl

+ 1
- 1
include/lager.hrl View File

@ -84,7 +84,7 @@
%% from a gen_event handler %% from a gen_event handler
spawn(fun() -> spawn(fun() ->
case catch(gen_event:which_handlers(lager_event)) of case catch(gen_event:which_handlers(lager_event)) of
X when X == []; X == {'EXIT', noproc} ->
X when X == []; X == {'EXIT', noproc}; X == [lager_backend_throttle] ->
%% there's no handlers yet or lager isn't running, try again %% there's no handlers yet or lager isn't running, try again
%% in half a second. %% in half a second.
timer:sleep(500), timer:sleep(500),

+ 13
- 23
src/lager_console_backend.erl View File

@ -40,13 +40,6 @@ init([Level, true]) -> % for backwards compatibility
init([Level,false]) -> % for backwards compatibility init([Level,false]) -> % for backwards compatibility
init([Level,{lager_default_formatter,?TERSE_FORMAT ++ [eol()]}]); init([Level,{lager_default_formatter,?TERSE_FORMAT ++ [eol()]}]);
init([Level,{Formatter,FormatterConfig}]) when is_atom(Formatter) -> init([Level,{Formatter,FormatterConfig}]) when is_atom(Formatter) ->
IsSafe = case is_new_style_console_available() of
false=Res ->
spawn(fun warn_user/0),
Res;
Res ->
Res
end,
Colors = case application:get_env(lager, colored) of Colors = case application:get_env(lager, colored) of
{ok, true} -> {ok, true} ->
{ok, LagerColors} = application:get_env(lager, colors), {ok, LagerColors} = application:get_env(lager, colors),
@ -54,9 +47,19 @@ init([Level,{Formatter,FormatterConfig}]) when is_atom(Formatter) ->
_ -> [] _ -> []
end, end,
try {IsSafe, lager_util:config_to_mask(Level)} of
try {is_new_style_console_available(), lager_util:config_to_mask(Level)} of
{false, _} -> {false, _} ->
{error, "Old style console was detected"};
Msg = "Lager's console backend is incompatible with the 'old' shell, not enabling it",
%% be as noisy as possible, log to every possible place
try
alarm_handler:set_alarm({?MODULE, "WARNING: " ++ Msg})
catch
_:_ ->
error_logger:warning_msg(Msg ++ "~n")
end,
io:format("WARNING: " ++ Msg ++ "~n"),
?INT_LOG(warning, Msg, []),
{error, {fatal, old_shell}};
{true, Levels} -> {true, Levels} ->
{ok, #state{level=Levels, {ok, #state{level=Levels,
formatter=Formatter, formatter=Formatter,
@ -64,7 +67,7 @@ init([Level,{Formatter,FormatterConfig}]) when is_atom(Formatter) ->
colors=Colors}} colors=Colors}}
catch catch
_:_ -> _:_ ->
{error, bad_log_level}
{error, {fatal, bad_log_level}}
end; end;
init(Level) -> init(Level) ->
init([Level,{lager_default_formatter,?TERSE_FORMAT ++ [eol()]}]). init([Level,{lager_default_formatter,?TERSE_FORMAT ++ [eol()]}]).
@ -134,19 +137,6 @@ is_new_style_console_available() ->
is_pid(whereis(user_drv)). is_pid(whereis(user_drv)).
-endif. -endif.
warn_user() ->
Msg = lists:flatten(
io_lib:format("WARNING: old-style console is in use, so ~s "
"log output to the console is disabled. "
"Restart the VM on a pseudo-tty to ensure "
"use of the new-style VM console.",
[?MODULE])),
catch alarm_handler:set_alarm({?MODULE, Msg}),
[begin
error_logger:warning_msg(Msg),
timer:sleep(1000)
end || _ <- lists:seq(1, 10)].
-ifdef(TEST). -ifdef(TEST).
console_log_test_() -> console_log_test_() ->
%% tiny recursive fun that pretends to be a group leader %% tiny recursive fun that pretends to be a group leader

+ 1
- 1
src/lager_file_backend.erl View File

@ -99,7 +99,7 @@ init(LogFileConfig) when is_list(LogFileConfig) ->
case validate_logfile_proplist(LogFileConfig) of case validate_logfile_proplist(LogFileConfig) of
false -> false ->
%% falied to validate config %% falied to validate config
{error, bad_config};
{error, {fatal, bad_config}};
Config -> Config ->
%% probabably a better way to do this, but whatever %% probabably a better way to do this, but whatever
[Name, Level, Date, Size, Count, SyncInterval, SyncSize, SyncOn, CheckInterval, Formatter, FormatterConfig] = [Name, Level, Date, Size, Count, SyncInterval, SyncSize, SyncOn, CheckInterval, Formatter, FormatterConfig] =

+ 16
- 6
src/lager_handler_watcher.erl View File

@ -23,6 +23,8 @@
-behaviour(gen_server). -behaviour(gen_server).
-include("lager.hrl").
-ifdef(TEST). -ifdef(TEST).
-include_lib("eunit/include/eunit.hrl"). -include_lib("eunit/include/eunit.hrl").
-endif. -endif.
@ -73,6 +75,8 @@ handle_info({gen_event_EXIT, Module, Reason}, #state{module=Module,
handle_info(reinstall_handler, #state{module=Module, config=Config, event=Event} = State) -> handle_info(reinstall_handler, #state{module=Module, config=Config, event=Event} = State) ->
install_handler(Event, Module, Config), install_handler(Event, Module, Config),
{noreply, State}; {noreply, State};
handle_info(stop, State) ->
{stop, normal, State};
handle_info(_Info, State) -> handle_info(_Info, State) ->
{noreply, State}. {noreply, State}.
@ -87,12 +91,18 @@ code_change(_OldVsn, State, _Extra) ->
install_handler(Event, Module, Config) -> install_handler(Event, Module, Config) ->
case gen_event:add_sup_handler(Event, Module, Config) of case gen_event:add_sup_handler(Event, Module, Config) of
ok -> ok ->
_ = lager:log(debug, self(), "Lager installed handler ~p into ~p", [Module, Event]),
?INT_LOG(debug, "Lager installed handler ~p into ~p", [Module, Event]),
lager:update_loglevel_config(), lager:update_loglevel_config(),
ok; ok;
{error, {fatal, Reason}} ->
?INT_LOG(error, "Lager fatally failed to install handler ~p into"
" ~p, NOT retrying: ~p", [Module, Event, Reason]),
%% tell ourselves to stop
self() ! stop,
ok;
Error -> Error ->
%% try to reinstall it later %% try to reinstall it later
_ = lager:log(error, self(), "Lager failed to install handler ~p into"
?INT_LOG(error, "Lager failed to install handler ~p into"
" ~p, retrying later : ~p", [Module, Event, Error]), " ~p, retrying later : ~p", [Module, Event, Error]),
erlang:send_after(5000, self(), reinstall_handler), erlang:send_after(5000, self(), reinstall_handler),
ok ok
@ -145,10 +155,10 @@ reinstall_on_runtime_failure_test_() ->
?assert(lists:member(lager_crash_backend, gen_event:which_handlers(lager_event))), ?assert(lists:member(lager_crash_backend, gen_event:which_handlers(lager_event))),
timer:sleep(6000), timer:sleep(6000),
?assertEqual(2, lager_test_backend:count()), ?assertEqual(2, lager_test_backend:count()),
{_Level, _Time, Message, _Metadata} = lager_test_backend:pop(),
?assertEqual("Lager event handler lager_crash_backend exited with reason crash", lists:flatten(Message)),
{_Level2, _Time2, Message2, _Metadata} = lager_test_backend:pop(),
?assertMatch("Lager failed to install handler lager_crash_backend into lager_event, retrying later :"++_, lists:flatten(Message2)),
{_Severity, _Date, Msg, _Metadata} = lager_test_backend:pop(),
?assertEqual("Lager event handler lager_crash_backend exited with reason crash", lists:flatten(Msg)),
{_Severity2, _Date2, Msg2, _Metadata2} = lager_test_backend:pop(),
?assertMatch("Lager failed to install handler lager_crash_backend into lager_event, retrying later :"++_, lists:flatten(Msg2)),
?assertEqual(false, lists:member(lager_crash_backend, gen_event:which_handlers(lager_event))) ?assertEqual(false, lists:member(lager_crash_backend, gen_event:which_handlers(lager_event)))
after after
application:stop(lager), application:stop(lager),

+ 1
- 1
src/lager_handler_watcher_sup.erl View File

@ -35,5 +35,5 @@ init([]) ->
{ok, {{simple_one_for_one, 10, 60}, {ok, {{simple_one_for_one, 10, 60},
[ [
{lager_handler_watcher, {lager_handler_watcher, start_link, []}, {lager_handler_watcher, {lager_handler_watcher, start_link, []},
transient, 5000, worker, [lager_handler_watcher]}
temporary, 5000, worker, [lager_handler_watcher]}
]}}. ]}}.

Loading…
Cancel
Save