25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-20 16:15:59 +01:00

Lager to Elixir Logger bridge is now compliant with ejabberd loglevel set / get

This should fix #966
This commit is contained in:
Mickael Remond 2016-03-09 22:30:46 +01:00
parent 035c63fd2a
commit 92a0181932
2 changed files with 29 additions and 16 deletions

View File

@ -104,8 +104,10 @@ get_string_env(Name, Default) ->
start() -> start() ->
StartedApps = application:which_applications(5000), StartedApps = application:which_applications(5000),
case lists:keyfind(logger, 1, StartedApps) of case lists:keyfind(logger, 1, StartedApps) of
%% Elixir logger is started. We assume everything is in place
%% to use lager to Elixir logger bridge.
{logger, _, _} -> {logger, _, _} ->
error_logger:info_msg("Ignoring logger options, using Elixir Logger.", []), error_logger:info_msg("Ignoring ejabberd logger options, using Elixir Logger.", []),
%% Do not start lager, we rely on Elixir Logger %% Do not start lager, we rely on Elixir Logger
do_start_for_logger(); do_start_for_logger();
_ -> _ ->
@ -119,7 +121,7 @@ do_start_for_logger() ->
application:set_env(lager, error_logger_redirect, false), application:set_env(lager, error_logger_redirect, false),
application:set_env(lager, error_logger_whitelist, ['Elixir.Logger.ErrorHandler']), application:set_env(lager, error_logger_whitelist, ['Elixir.Logger.ErrorHandler']),
application:set_env(lager, crash_log, false), application:set_env(lager, crash_log, false),
application:set_env(lager, handlers, [{elixir_logger_backend, [{level, debug}]}]), application:set_env(lager, handlers, [{elixir_logger_backend, [{level, info}]}]),
ejabberd:start_app(lager), ejabberd:start_app(lager),
ok. ok.
@ -201,6 +203,8 @@ set(LogLevel) when is_integer(LogLevel) ->
lager:set_loglevel(H, LagerLogLevel); lager:set_loglevel(H, LagerLogLevel);
(lager_console_backend = H) -> (lager_console_backend = H) ->
lager:set_loglevel(H, LagerLogLevel); lager:set_loglevel(H, LagerLogLevel);
(elixir_logger_backend = H) ->
lager:set_loglevel(H, LagerLogLevel);
(_) -> (_) ->
ok ok
end, gen_event:which_handlers(lager_event)) end, gen_event:which_handlers(lager_event))
@ -211,13 +215,15 @@ set({_LogLevel, _}) ->
{module, lager}. {module, lager}.
get_lager_loglevel() -> get_lager_loglevel() ->
R = case get_lager_handlers() of Handlers = get_lager_handlers(),
[] -> none; lists:foldl(fun(lager_console_backend, _Acc) ->
[elixir_logger_backend] -> debug; lager:get_loglevel(lager_console_backend);
[FirstHandler|_] -> (elixir_logger_backend, _Acc) ->
lager:get_loglevel(FirstHandler) lager:get_loglevel(elixir_logger_backend);
end, (_, Acc) ->
R. Acc
end,
none, Handlers).
get_lager_handlers() -> get_lager_handlers() ->
case catch gen_event:which_handlers(lager_event) of case catch gen_event:which_handlers(lager_event) of

View File

@ -30,15 +30,19 @@
-export([init/1, handle_call/2, handle_event/2, handle_info/2, terminate/2, -export([init/1, handle_call/2, handle_event/2, handle_info/2, terminate/2,
code_change/3]). code_change/3]).
init(_Opts) -> -record(state, {level = debug}).
State = [],
init(Opts) ->
Level = proplists:get_value(level, Opts, debug),
State = #state{level = Level},
{ok, State}. {ok, State}.
%% @private %% @private
handle_event({log, LagerMsg}, State) -> handle_event({log, LagerMsg}, State) ->
#{mode := Mode, truncate := Truncate, level := MinLevel, utc_log := UTCLog} = 'Elixir.Logger.Config':'__data__'(), #{mode := Mode, truncate := Truncate, level := MinLevel, utc_log := UTCLog} = 'Elixir.Logger.Config':'__data__'(),
MsgLevel = severity_to_level(lager_msg:severity(LagerMsg)), MsgLevel = severity_to_level(lager_msg:severity(LagerMsg)),
case {lager_util:is_loggable(LagerMsg, lager_util:level_to_num(debug), ?MODULE), 'Elixir.Logger':compare_levels(MsgLevel, MinLevel)} of case {lager_util:is_loggable(LagerMsg, lager_util:level_to_num(State#state.level), ?MODULE),
'Elixir.Logger':compare_levels(MsgLevel, MinLevel)} of
{_, lt}-> {_, lt}->
{ok, State}; {ok, State};
{true, _} -> {true, _} ->
@ -55,13 +59,15 @@ handle_event({log, LagerMsg}, State) ->
_ -> _ ->
{ok, State} {ok, State}
end; end;
handle_event(_, State) -> handle_event(_Msg, State) ->
{ok, State}. {ok, State}.
%% @private %% @private
%% TODO Handle loglevels %% TODO Handle loglevels
handle_call(_Msg, State) -> handle_call(get_loglevel, State) ->
{ok, ok, State}. {ok, lager_util:config_to_mask(State#state.level), State};
handle_call({set_loglevel, Config}, State) ->
{ok, ok, State#state{level = Config}}.
%% @private %% @private
handle_info(_Msg, State) -> handle_info(_Msg, State) ->
@ -77,7 +83,8 @@ code_change(_OldVsn, State, _Extra) ->
notify(sync, Msg) -> notify(sync, Msg) ->
gen_event:sync_notify('Elixir.Logger', Msg); gen_event:sync_notify('Elixir.Logger', Msg);
notify(async, Msg) -> gen_event:notify('Elixir.Logger', Msg). notify(async, Msg) ->
gen_event:notify('Elixir.Logger', Msg).
normalize_pid(Metadata) -> normalize_pid(Metadata) ->
case proplists:get_value(pid, Metadata) of case proplists:get_value(pid, Metadata) of