25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-12-20 17:27:00 +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() ->
StartedApps = application:which_applications(5000),
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, _, _} ->
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_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_whitelist, ['Elixir.Logger.ErrorHandler']),
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),
ok.
@ -201,6 +203,8 @@ set(LogLevel) when is_integer(LogLevel) ->
lager:set_loglevel(H, LagerLogLevel);
(lager_console_backend = H) ->
lager:set_loglevel(H, LagerLogLevel);
(elixir_logger_backend = H) ->
lager:set_loglevel(H, LagerLogLevel);
(_) ->
ok
end, gen_event:which_handlers(lager_event))
@ -211,13 +215,15 @@ set({_LogLevel, _}) ->
{module, lager}.
get_lager_loglevel() ->
R = case get_lager_handlers() of
[] -> none;
[elixir_logger_backend] -> debug;
[FirstHandler|_] ->
lager:get_loglevel(FirstHandler)
end,
R.
Handlers = get_lager_handlers(),
lists:foldl(fun(lager_console_backend, _Acc) ->
lager:get_loglevel(lager_console_backend);
(elixir_logger_backend, _Acc) ->
lager:get_loglevel(elixir_logger_backend);
(_, Acc) ->
Acc
end,
none, Handlers).
get_lager_handlers() ->
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,
code_change/3]).
init(_Opts) ->
State = [],
-record(state, {level = debug}).
init(Opts) ->
Level = proplists:get_value(level, Opts, debug),
State = #state{level = Level},
{ok, State}.
%% @private
handle_event({log, LagerMsg}, State) ->
#{mode := Mode, truncate := Truncate, level := MinLevel, utc_log := UTCLog} = 'Elixir.Logger.Config':'__data__'(),
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}->
{ok, State};
{true, _} ->
@ -55,13 +59,15 @@ handle_event({log, LagerMsg}, State) ->
_ ->
{ok, State}
end;
handle_event(_, State) ->
handle_event(_Msg, State) ->
{ok, State}.
%% @private
%% TODO Handle loglevels
handle_call(_Msg, State) ->
{ok, ok, State}.
handle_call(get_loglevel, State) ->
{ok, lager_util:config_to_mask(State#state.level), State};
handle_call({set_loglevel, Config}, State) ->
{ok, ok, State#state{level = Config}}.
%% @private
handle_info(_Msg, State) ->
@ -77,7 +83,8 @@ code_change(_OldVsn, State, _Extra) ->
notify(sync, 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) ->
case proplists:get_value(pid, Metadata) of