Skip to content

Commit

Permalink
OTP 21 support
Browse files Browse the repository at this point in the history
To support OTP 21 we manually start error_logger if it is not present
(and lager is configured to install an error_logger handler) and we add
error_logger as a logger handler.

Longer term we should switch to installing our own logger handler, but
that is a larger task as we'd have to redo all the event parsing, if
that is even possible.
  • Loading branch information
Vagabond committed Jun 5, 2018
1 parent d0a9db3 commit 05bc579
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
2 changes: 1 addition & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

{erl_opts, [
{lager_extra_sinks, ['__lager_test_sink']},
{platform_define, "(19|20)", test_statem},
{platform_define, "(19|20|21)", test_statem},
{platform_define, "(18)", 'FUNCTION_NAME', unavailable},
{platform_define, "(18)", 'FUNCTION_ARITY', 0},
debug_info,
Expand Down
28 changes: 17 additions & 11 deletions src/lager_app.erl
Original file line number Diff line number Diff line change
Expand Up @@ -158,24 +158,30 @@ start_error_logger_handler(true, HWM, WhiteList) ->
throw({error, bad_config})
end,

case whereis(error_logger) of
undefined ->
%% On OTP 21 and above, error_logger is deprecated in favor of 'logger'
%% As a band-aid, boot up error_logger anyway and install it as a logger handler
%% we can't use error_logger:add_report_handler because we want supervision of the handler
%% so we have to manually add the logger handler
%%
%% Longer term we should be installing a logger handler instead, but this will bridge the gap
%% for now.
error_logger:start(),
_ = logger:add_handler(error_logger,error_logger,#{level=>info,filter_default=>log});
_ ->
ok
end,

_ = case supervisor:start_child(lager_handler_watcher_sup, [error_logger, error_logger_lager_h, [HWM, GlStrategy]]) of
%% capture which handlers we removed from error_logger so we can restore them when lager stops
OldHandlers = case supervisor:start_child(lager_handler_watcher_sup, [error_logger, error_logger_lager_h, [HWM, GlStrategy]]) of
{ok, _} ->
[begin error_logger:delete_report_handler(X), X end ||
X <- gen_event:which_handlers(error_logger) -- [error_logger_lager_h | WhiteList]];
{error, _} ->
[]
end,

Handlers = case application:get_env(lager, handlers) of
undefined ->
[{lager_console_backend, [{level, info}]},
{lager_file_backend, [{file, "log/error.log"}, {level, error}, {size, 10485760}, {date, "$D0"}, {count, 5}]},
{lager_file_backend, [{file, "log/console.log"}, {level, info}, {size, 10485760}, {date, "$D0"}, {count, 5}]}];
{ok, Val} ->
Val
end,
Handlers.
OldHandlers.

configure_sink(Sink, SinkDef) ->
lager_config:new_sink(Sink),
Expand Down

0 comments on commit 05bc579

Please sign in to comment.