%%%
%%%----------------------------------------------------------------------
-{deps, [{p1_utils, ".*", {git, "https://github.com/processone/p1_utils", {tag, "1.0.16"}}},
+{deps, [{lager, ".*", {git, "https://github.com/erlang-lager/lager", "3.6.10"}},
+ {p1_utils, ".*", {git, "https://github.com/processone/p1_utils", {tag, "1.0.16"}}},
{cache_tab, ".*", {git, "https://github.com/processone/cache_tab", {tag, "1.0.20"}}},
{fast_tls, ".*", {git, "https://github.com/processone/fast_tls", {tag, "1.1.2"}}},
{stringprep, ".*", {git, "https://github.com/processone/stringprep", {tag, "1.0.17"}}},
{if_var_true, sip, {d, 'SIP'}},
{if_var_true, stun, {d, 'STUN'}},
{if_version_above, "20", {d, 'DEPRECATED_GET_STACKTRACE'}},
+ {if_version_below, "22", {d, 'LAGER'}},
{if_var_true, roster_gateway_workaround, {d, 'ROSTER_GATWAY_WORKAROUND'}},
{if_var_match, db_type, mssql, {d, 'mssql'}},
{if_var_true, elixir, {d, 'ELIXIR_ENABLED'}},
-module(ejabberd_logger).
-compile({no_auto_import, [get/0]}).
--include_lib("kernel/include/logger.hrl").
-
%% API
-export([start/0, get/0, set/1, get_log_path/0, flush/0]).
-export([convert_loglevel/1, loglevels/0]).
+-ifndef(LAGER).
-export([progress_filter/2]).
+-endif.
%% Deprecated functions
-export([restart/0, reopen_log/0, rotate_log/0]).
-deprecated([{restart, 0},
{reopen_log, 0},
{rotate_log, 0}]).
--type loglevel() :: none | logger:level().
+-type loglevel() :: none | emergency | alert | critical |
+ error | warning | notice | info | debug.
-define(is_loglevel(L),
((L == none) or (L == emergency) or (L == alert)
or (L == critical) or (L == error) or (L == warning)
or (L == notice) or (L == info) or (L == debug))).
+-spec restart() -> ok.
+-spec reopen_log() -> ok.
+-spec rotate_log() -> ok.
+-spec get() -> loglevel().
+-spec set(0..5 | loglevel()) -> ok.
+-spec flush() -> ok.
+
%%%===================================================================
%%% API
%%%===================================================================
end
end.
--spec get_integer_env(atom(), T) -> T.
-get_integer_env(Name, Default) ->
- case application:get_env(ejabberd, Name) of
- {ok, I} when is_integer(I), I>0 ->
- I;
- undefined ->
- Default;
- {ok, Junk} ->
- ?LOG_ERROR("Wrong value for ~ts: ~p; "
- "using ~p as a fallback",
- [Name, Junk, Default]),
- Default
- end.
-
-spec loglevels() -> [loglevel(), ...].
loglevels() ->
[none, emergency, alert, critical, error, warning, notice, info, debug].
convert_loglevel(4) -> info;
convert_loglevel(5) -> debug.
+-spec get_integer_env(atom(), T) -> T.
+get_integer_env(Name, Default) ->
+ case application:get_env(ejabberd, Name) of
+ {ok, I} when is_integer(I), I>=0 ->
+ I;
+ undefined ->
+ Default;
+ {ok, Junk} ->
+ error_logger:error_msg("wrong value for ~ts: ~p; "
+ "using ~p as a fallback~n",
+ [Name, Junk, Default]),
+ Default
+ end.
+
+-ifdef(LAGER).
+-spec get_string_env(atom(), T) -> T.
+get_string_env(Name, Default) ->
+ case application:get_env(ejabberd, Name) of
+ {ok, L} when is_list(L) ->
+ L;
+ undefined ->
+ Default;
+ {ok, Junk} ->
+ error_logger:error_msg("wrong value for ~ts: ~p; "
+ "using ~p as a fallback~n",
+ [Name, Junk, Default]),
+ Default
+ end.
+
-spec start() -> ok.
start() ->
start(info).
--spec start(loglevel()) -> ok.
+start(Level) ->
+ 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 ejabberd logger options, using Elixir Logger.", []),
+ %% Do not start lager, we rely on Elixir Logger
+ do_start_for_logger(Level);
+ _ ->
+ do_start(Level)
+ end.
+
+do_start_for_logger(Level) ->
+ application:load(sasl),
+ application:set_env(sasl, sasl_error_logger, false),
+ application:load(lager),
+ 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, Level}]}]),
+ ejabberd:start_app(lager),
+ ok.
+
+do_start(Level) ->
+ application:load(sasl),
+ application:set_env(sasl, sasl_error_logger, false),
+ application:load(lager),
+ ConsoleLog = get_log_path(),
+ Dir = filename:dirname(ConsoleLog),
+ ErrorLog = filename:join([Dir, "error.log"]),
+ CrashLog = filename:join([Dir, "crash.log"]),
+ LogRotateDate = get_string_env(log_rotate_date, ""),
+ LogRotateSize = get_integer_env(log_rotate_size, 10*1024*1024),
+ LogRotateCount = get_integer_env(log_rotate_count, 1),
+ LogRateLimit = get_integer_env(log_rate_limit, 100),
+ ConsoleLevel = case get_lager_version() >= "3.6.0" of
+ true -> [{level, Level}];
+ false -> Level
+ end,
+ application:set_env(lager, error_logger_hwm, LogRateLimit),
+ application:set_env(
+ lager, handlers,
+ [{lager_console_backend, ConsoleLevel},
+ {lager_file_backend, [{file, ConsoleLog}, {level, Level}, {date, LogRotateDate},
+ {count, LogRotateCount}, {size, LogRotateSize}]},
+ {lager_file_backend, [{file, ErrorLog}, {level, error}, {date, LogRotateDate},
+ {count, LogRotateCount}, {size, LogRotateSize}]}]),
+ application:set_env(lager, crash_log, CrashLog),
+ application:set_env(lager, crash_log_date, LogRotateDate),
+ application:set_env(lager, crash_log_size, LogRotateSize),
+ application:set_env(lager, crash_log_count, LogRotateCount),
+ ejabberd:start_app(lager),
+ lists:foreach(fun(Handler) ->
+ lager:set_loghwm(Handler, LogRateLimit)
+ end, gen_event:which_handlers(lager_event)).
+
+restart() ->
+ Level = ejabberd_option:loglevel(),
+ application:stop(lager),
+ start(Level).
+
+reopen_log() ->
+ ok.
+
+rotate_log() ->
+ catch lager_crash_log ! rotate,
+ lists:foreach(
+ fun({lager_file_backend, File}) ->
+ whereis(lager_event) ! {rotate, File};
+ (_) ->
+ ok
+ end, gen_event:which_handlers(lager_event)).
+
+get() ->
+ 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).
+
+set(N) when is_integer(N), N>=0, N=<5 ->
+ set(convert_loglevel(N));
+set(Level) when ?is_loglevel(Level) ->
+ case get() of
+ Level ->
+ ok;
+ _ ->
+ ConsoleLog = get_log_path(),
+ lists:foreach(
+ fun({lager_file_backend, File} = H) when File == ConsoleLog ->
+ lager:set_loglevel(H, Level);
+ (lager_console_backend = H) ->
+ lager:set_loglevel(H, Level);
+ (elixir_logger_backend = H) ->
+ lager:set_loglevel(H, Level);
+ (_) ->
+ ok
+ end, get_lager_handlers())
+ end,
+ case Level of
+ debug -> xmpp:set_config([{debug, true}]);
+ _ -> xmpp:set_config([{debug, false}])
+ end.
+
+get_lager_handlers() ->
+ case catch gen_event:which_handlers(lager_event) of
+ {'EXIT',noproc} ->
+ [];
+ Result ->
+ Result
+ end.
+
+-spec get_lager_version() -> string().
+get_lager_version() ->
+ Apps = application:loaded_applications(),
+ case lists:keyfind(lager, 1, Apps) of
+ {_, _, Vsn} -> Vsn;
+ false -> "0.0.0"
+ end.
+
+flush() ->
+ application:stop(lager).
+
+-else.
+-include_lib("kernel/include/logger.hrl").
+
+-spec start() -> ok | {error, term()}.
+start() ->
+ start(info).
+
start(Level) ->
EjabberdLog = get_log_path(),
Dir = filename:dirname(EjabberdLog),
end,
case logger:add_handler(ejabberd_log, logger_std_h,
#{level => all,
- config => Config#{type => {file, EjabberdLog}},
+ config => Config#{file => EjabberdLog},
formatter => {logger_formatter, FileFmtConfig}}) of
ok -> ok;
{error, {already_exist, _}} -> ok
end,
case logger:add_handler(error_log, logger_std_h,
#{level => error,
- config => Config#{type => {file, ErrorLog}},
+ config => Config#{file => ErrorLog},
formatter => {logger_formatter, FileFmtConfig}}) of
ok -> ok;
{error, {already_exist, _}} -> ok
rotate_log() ->
ok.
--spec get() -> loglevel().
get() ->
#{level := Level} = logger:get_primary_config(),
Level.
--spec set(0..5 | loglevel()) -> ok.
set(N) when is_integer(N), N>=0, N=<5 ->
set(convert_loglevel(N));
set(Level) when ?is_loglevel(Level) ->
end
end.
--spec flush() -> ok.
flush() ->
lists:foreach(
fun(#{id := HandlerId, module := logger_std_h}) ->
(_) ->
ok
end, logger:get_handler_config()).
+
+-endif.