Skip to content

Commit ccdc4d4

Browse files
committed
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.
1 parent 1ea378a commit ccdc4d4

5 files changed

+32
-32
lines changed

include/lager.hrl

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

src/lager_console_backend.erl

+13-23
Original file line numberDiff line numberDiff line change
@@ -40,31 +40,34 @@ init([Level, true]) -> % for backwards compatibility
4040
init([Level,false]) -> % for backwards compatibility
4141
init([Level,{lager_default_formatter,?TERSE_FORMAT ++ [eol()]}]);
4242
init([Level,{Formatter,FormatterConfig}]) when is_atom(Formatter) ->
43-
IsSafe = case is_new_style_console_available() of
44-
false=Res ->
45-
spawn(fun warn_user/0),
46-
Res;
47-
Res ->
48-
Res
49-
end,
5043
Colors = case application:get_env(lager, colored) of
5144
{ok, true} ->
5245
{ok, LagerColors} = application:get_env(lager, colors),
5346
LagerColors;
5447
_ -> []
5548
end,
5649

57-
try {IsSafe, lager_util:config_to_mask(Level)} of
50+
try {is_new_style_console_available(), lager_util:config_to_mask(Level)} of
5851
{false, _} ->
59-
{error, "Old style console was detected"};
52+
Msg = "Lager's console backend is incompatible with the 'old' shell, not enabling it",
53+
%% be as noisy as possible, log to every possible place
54+
try
55+
alarm_handler:set_alarm({?MODULE, "WARNING: " ++ Msg})
56+
catch
57+
_:_ ->
58+
error_logger:warning_msg(Msg ++ "~n")
59+
end,
60+
io:format("WARNING: " ++ Msg ++ "~n"),
61+
?INT_LOG(warning, Msg, []),
62+
{error, {fatal, old_shell}};
6063
{true, Levels} ->
6164
{ok, #state{level=Levels,
6265
formatter=Formatter,
6366
format_config=FormatterConfig,
6467
colors=Colors}}
6568
catch
6669
_:_ ->
67-
{error, bad_log_level}
70+
{error, {fatal, bad_log_level}}
6871
end;
6972
init(Level) ->
7073
init([Level,{lager_default_formatter,?TERSE_FORMAT ++ [eol()]}]).
@@ -134,19 +137,6 @@ is_new_style_console_available() ->
134137
is_pid(whereis(user_drv)).
135138
-endif.
136139

137-
warn_user() ->
138-
Msg = lists:flatten(
139-
io_lib:format("WARNING: old-style console is in use, so ~s "
140-
"log output to the console is disabled. "
141-
"Restart the VM on a pseudo-tty to ensure "
142-
"use of the new-style VM console.",
143-
[?MODULE])),
144-
catch alarm_handler:set_alarm({?MODULE, Msg}),
145-
[begin
146-
error_logger:warning_msg(Msg),
147-
timer:sleep(1000)
148-
end || _ <- lists:seq(1, 10)].
149-
150140
-ifdef(TEST).
151141
console_log_test_() ->
152142
%% tiny recursive fun that pretends to be a group leader

src/lager_file_backend.erl

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ init(LogFileConfig) when is_list(LogFileConfig) ->
9999
case validate_logfile_proplist(LogFileConfig) of
100100
false ->
101101
%% falied to validate config
102-
{error, bad_config};
102+
{error, {fatal, bad_config}};
103103
Config ->
104104
%% probabably a better way to do this, but whatever
105105
[Name, Level, Date, Size, Count, SyncInterval, SyncSize, SyncOn, CheckInterval, Formatter, FormatterConfig] =

src/lager_handler_watcher.erl

+16-6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
-behaviour(gen_server).
2525

26+
-include("lager.hrl").
27+
2628
-ifdef(TEST).
2729
-include_lib("eunit/include/eunit.hrl").
2830
-endif.
@@ -73,6 +75,8 @@ handle_info({gen_event_EXIT, Module, Reason}, #state{module=Module,
7375
handle_info(reinstall_handler, #state{module=Module, config=Config, event=Event} = State) ->
7476
install_handler(Event, Module, Config),
7577
{noreply, State};
78+
handle_info(stop, State) ->
79+
{stop, normal, State};
7680
handle_info(_Info, State) ->
7781
{noreply, State}.
7882

@@ -87,12 +91,18 @@ code_change(_OldVsn, State, _Extra) ->
8791
install_handler(Event, Module, Config) ->
8892
case gen_event:add_sup_handler(Event, Module, Config) of
8993
ok ->
90-
_ = lager:log(debug, self(), "Lager installed handler ~p into ~p", [Module, Event]),
94+
?INT_LOG(debug, "Lager installed handler ~p into ~p", [Module, Event]),
9195
lager:update_loglevel_config(),
9296
ok;
97+
{error, {fatal, Reason}} ->
98+
?INT_LOG(error, "Lager fatally failed to install handler ~p into"
99+
" ~p, NOT retrying: ~p", [Module, Event, Reason]),
100+
%% tell ourselves to stop
101+
self() ! stop,
102+
ok;
93103
Error ->
94104
%% try to reinstall it later
95-
_ = lager:log(error, self(), "Lager failed to install handler ~p into"
105+
?INT_LOG(error, "Lager failed to install handler ~p into"
96106
" ~p, retrying later : ~p", [Module, Event, Error]),
97107
erlang:send_after(5000, self(), reinstall_handler),
98108
ok
@@ -145,10 +155,10 @@ reinstall_on_runtime_failure_test_() ->
145155
?assert(lists:member(lager_crash_backend, gen_event:which_handlers(lager_event))),
146156
timer:sleep(6000),
147157
?assertEqual(2, lager_test_backend:count()),
148-
{_Level, _Time, Message, _Metadata} = lager_test_backend:pop(),
149-
?assertEqual("Lager event handler lager_crash_backend exited with reason crash", lists:flatten(Message)),
150-
{_Level2, _Time2, Message2, _Metadata} = lager_test_backend:pop(),
151-
?assertMatch("Lager failed to install handler lager_crash_backend into lager_event, retrying later :"++_, lists:flatten(Message2)),
158+
{_Severity, _Date, Msg, _Metadata} = lager_test_backend:pop(),
159+
?assertEqual("Lager event handler lager_crash_backend exited with reason crash", lists:flatten(Msg)),
160+
{_Severity2, _Date2, Msg2, _Metadata2} = lager_test_backend:pop(),
161+
?assertMatch("Lager failed to install handler lager_crash_backend into lager_event, retrying later :"++_, lists:flatten(Msg2)),
152162
?assertEqual(false, lists:member(lager_crash_backend, gen_event:which_handlers(lager_event)))
153163
after
154164
application:stop(lager),

src/lager_handler_watcher_sup.erl

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ init([]) ->
3535
{ok, {{simple_one_for_one, 10, 60},
3636
[
3737
{lager_handler_watcher, {lager_handler_watcher, start_link, []},
38-
transient, 5000, worker, [lager_handler_watcher]}
38+
temporary, 5000, worker, [lager_handler_watcher]}
3939
]}}.

0 commit comments

Comments
 (0)