2013-04-08 11:12:54 +02:00
|
|
|
%%%-------------------------------------------------------------------
|
|
|
|
%%% @author Evgeniy Khramtsov <ekhramtsov@process-one.net>
|
|
|
|
%%% @copyright (C) 2013, Evgeniy Khramtsov
|
|
|
|
%%% @doc
|
|
|
|
%%%
|
|
|
|
%%% @end
|
|
|
|
%%% Created : 12 May 2013 by Evgeniy Khramtsov <ekhramtsov@process-one.net>
|
2013-06-18 16:56:29 +02:00
|
|
|
%%%
|
2015-01-21 14:52:37 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2013-2015 ProcessOne
|
2013-06-18 16:56:29 +02:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
|
|
|
%%%
|
2014-02-22 11:27:40 +01:00
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2013-06-18 16:56:29 +02:00
|
|
|
|
2013-04-08 11:12:54 +02:00
|
|
|
%%%-------------------------------------------------------------------
|
|
|
|
-module(ejabberd_logger).
|
|
|
|
|
|
|
|
%% API
|
2013-06-27 10:58:56 +02:00
|
|
|
-export([start/0, reopen_log/0, get/0, set/1, get_log_path/0]).
|
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
|
|
|
|
|
|
|
-type loglevel() :: 0 | 1 | 2 | 3 | 4 | 5.
|
|
|
|
|
|
|
|
-spec start() -> ok.
|
|
|
|
-spec get_log_path() -> string().
|
|
|
|
-spec reopen_log() -> ok.
|
|
|
|
-spec get() -> {loglevel(), atom(), string()}.
|
2013-06-27 11:27:20 +02:00
|
|
|
-spec set(loglevel() | {loglevel(), list()}) -> {module, module()}.
|
2013-04-08 11:12:54 +02:00
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% API
|
|
|
|
%%%===================================================================
|
2013-06-27 10:58:56 +02:00
|
|
|
%% @doc Returns the full path to the ejabberd log file.
|
|
|
|
%% It first checks for application configuration parameter 'log_path'.
|
|
|
|
%% If not defined it checks the environment variable EJABBERD_LOG_PATH.
|
|
|
|
%% And if that one is neither defined, returns the default value:
|
|
|
|
%% "ejabberd.log" in current directory.
|
|
|
|
get_log_path() ->
|
|
|
|
case application:get_env(ejabberd, log_path) of
|
|
|
|
{ok, Path} ->
|
|
|
|
Path;
|
|
|
|
undefined ->
|
|
|
|
case os:getenv("EJABBERD_LOG_PATH") of
|
|
|
|
false ->
|
|
|
|
?LOG_PATH;
|
|
|
|
Path ->
|
|
|
|
Path
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
|
|
|
-ifdef(LAGER).
|
|
|
|
|
2015-01-22 12:40:27 +01:00
|
|
|
get_integer_env(Name, Default) ->
|
2013-11-12 12:33:09 +01:00
|
|
|
case application:get_env(ejabberd, Name) of
|
2015-01-22 12:40:27 +01:00
|
|
|
{ok, I} when is_integer(I), I>=0 ->
|
2013-11-12 12:33:09 +01:00
|
|
|
I;
|
|
|
|
undefined ->
|
|
|
|
Default;
|
|
|
|
{ok, Junk} ->
|
|
|
|
error_logger:error_msg("wrong value for ~s: ~p; "
|
|
|
|
"using ~p as a fallback~n",
|
|
|
|
[Name, Junk, Default]),
|
|
|
|
Default
|
|
|
|
end.
|
2015-01-22 12:40:27 +01:00
|
|
|
get_string_env(Name, Default) ->
|
2014-07-02 10:41:12 +02:00
|
|
|
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 ~s: ~p; "
|
|
|
|
"using ~p as a fallback~n",
|
|
|
|
[Name, Junk, Default]),
|
|
|
|
Default
|
|
|
|
end.
|
2013-11-12 12:33:09 +01:00
|
|
|
|
2013-04-08 11:12:54 +02:00
|
|
|
start() ->
|
2013-11-11 03:13:29 +01:00
|
|
|
application:load(sasl),
|
|
|
|
application:set_env(sasl, sasl_error_logger, false),
|
2013-06-27 10:58:56 +02:00
|
|
|
application:load(lager),
|
|
|
|
ConsoleLog = get_log_path(),
|
|
|
|
Dir = filename:dirname(ConsoleLog),
|
|
|
|
ErrorLog = filename:join([Dir, "error.log"]),
|
2013-07-15 03:49:28 +02:00
|
|
|
CrashLog = filename:join([Dir, "crash.log"]),
|
2015-01-22 12:40:27 +01:00
|
|
|
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),
|
2013-11-12 12:33:09 +01:00
|
|
|
application:set_env(lager, error_logger_hwm, LogRateLimit),
|
2013-06-27 10:58:56 +02:00
|
|
|
application:set_env(
|
|
|
|
lager, handlers,
|
|
|
|
[{lager_console_backend, info},
|
2014-07-02 10:41:12 +02:00
|
|
|
{lager_file_backend, [{file, ConsoleLog}, {level, info}, {date, LogRotateDate},
|
2013-11-18 14:22:48 +01:00
|
|
|
{count, LogRotateCount}, {size, LogRotateSize}]},
|
2014-07-02 10:41:12 +02:00
|
|
|
{lager_file_backend, [{file, ErrorLog}, {level, error}, {date, LogRotateDate},
|
2013-11-18 14:22:48 +01:00
|
|
|
{count, LogRotateCount}, {size, LogRotateSize}]}]),
|
2013-07-15 03:49:28 +02:00
|
|
|
application:set_env(lager, crash_log, CrashLog),
|
2014-07-02 10:41:12 +02:00
|
|
|
application:set_env(lager, crash_log_date, LogRotateDate),
|
2013-11-18 14:22:48 +01:00
|
|
|
application:set_env(lager, crash_log_size, LogRotateSize),
|
|
|
|
application:set_env(lager, crash_log_count, LogRotateCount),
|
2013-06-27 10:58:56 +02:00
|
|
|
ejabberd:start_app(lager),
|
2013-04-08 11:12:54 +02:00
|
|
|
ok.
|
|
|
|
|
2013-06-27 10:58:56 +02:00
|
|
|
reopen_log() ->
|
2013-11-18 14:22:48 +01:00
|
|
|
lager_crash_log ! rotate,
|
2013-06-27 10:58:56 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun({lager_file_backend, File}) ->
|
|
|
|
whereis(lager_event) ! {rotate, File};
|
|
|
|
(_) ->
|
|
|
|
ok
|
2013-11-11 03:13:29 +01:00
|
|
|
end, gen_event:which_handlers(lager_event)).
|
2013-06-27 10:58:56 +02:00
|
|
|
|
|
|
|
get() ->
|
|
|
|
case lager:get_loglevel(lager_console_backend) of
|
|
|
|
none -> {0, no_log, "No log"};
|
|
|
|
emergency -> {1, critical, "Critical"};
|
|
|
|
alert -> {1, critical, "Critical"};
|
|
|
|
critical -> {1, critical, "Critical"};
|
|
|
|
error -> {2, error, "Error"};
|
|
|
|
warning -> {3, warning, "Warning"};
|
|
|
|
notice -> {3, warning, "Warning"};
|
|
|
|
info -> {4, info, "Info"};
|
|
|
|
debug -> {5, debug, "Debug"}
|
|
|
|
end.
|
|
|
|
|
2013-06-27 11:27:20 +02:00
|
|
|
set(LogLevel) when is_integer(LogLevel) ->
|
2013-06-27 10:58:56 +02:00
|
|
|
LagerLogLevel = case LogLevel of
|
|
|
|
0 -> none;
|
|
|
|
1 -> critical;
|
|
|
|
2 -> error;
|
|
|
|
3 -> warning;
|
|
|
|
4 -> info;
|
|
|
|
5 -> debug
|
|
|
|
end,
|
|
|
|
case lager:get_loglevel(lager_console_backend) of
|
|
|
|
LagerLogLevel ->
|
|
|
|
ok;
|
|
|
|
_ ->
|
|
|
|
ConsoleLog = get_log_path(),
|
|
|
|
lists:foreach(
|
|
|
|
fun({lager_file_backend, File} = H) when File == ConsoleLog ->
|
|
|
|
lager:set_loglevel(H, LagerLogLevel);
|
|
|
|
(lager_console_backend = H) ->
|
|
|
|
lager:set_loglevel(H, LagerLogLevel);
|
|
|
|
(_) ->
|
|
|
|
ok
|
|
|
|
end, gen_event:which_handlers(lager_event))
|
|
|
|
end,
|
2013-06-27 11:27:20 +02:00
|
|
|
{module, lager};
|
|
|
|
set({_LogLevel, _}) ->
|
|
|
|
error_logger:error_msg("custom loglevels are not supported for 'lager'"),
|
2013-06-27 10:58:56 +02:00
|
|
|
{module, lager}.
|
|
|
|
|
|
|
|
-else.
|
|
|
|
|
|
|
|
start() ->
|
|
|
|
set(4),
|
|
|
|
LogPath = get_log_path(),
|
|
|
|
error_logger:add_report_handler(p1_logger_h, LogPath),
|
|
|
|
ok.
|
2013-04-08 11:12:54 +02:00
|
|
|
|
|
|
|
reopen_log() ->
|
|
|
|
%% TODO: Use the Reopen log API for logger_h ?
|
|
|
|
p1_logger_h:reopen_log(),
|
2013-06-27 10:58:56 +02:00
|
|
|
reopen_sasl_log().
|
2013-04-08 11:12:54 +02:00
|
|
|
|
|
|
|
get() ->
|
|
|
|
p1_loglevel:get().
|
|
|
|
|
|
|
|
set(LogLevel) ->
|
|
|
|
p1_loglevel:set(LogLevel).
|
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% Internal functions
|
|
|
|
%%%===================================================================
|
2013-06-27 10:58:56 +02:00
|
|
|
reopen_sasl_log() ->
|
|
|
|
case application:get_env(sasl,sasl_error_logger) of
|
|
|
|
{ok, {file, SASLfile}} ->
|
|
|
|
error_logger:delete_report_handler(sasl_report_file_h),
|
2013-08-27 09:43:50 +02:00
|
|
|
rotate_sasl_log(SASLfile),
|
2013-06-27 10:58:56 +02:00
|
|
|
error_logger:add_report_handler(sasl_report_file_h,
|
|
|
|
{SASLfile, get_sasl_error_logger_type()});
|
|
|
|
_ -> false
|
|
|
|
end,
|
|
|
|
ok.
|
|
|
|
|
2013-08-27 09:43:50 +02:00
|
|
|
rotate_sasl_log(Filename) ->
|
|
|
|
case file:read_file_info(Filename) of
|
|
|
|
{ok, _FileInfo} ->
|
|
|
|
file:rename(Filename, [Filename, ".0"]),
|
|
|
|
ok;
|
|
|
|
{error, _Reason} ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2013-04-08 11:12:54 +02:00
|
|
|
%% Function copied from Erlang/OTP lib/sasl/src/sasl.erl which doesn't export it
|
|
|
|
get_sasl_error_logger_type () ->
|
|
|
|
case application:get_env (sasl, errlog_type) of
|
|
|
|
{ok, error} -> error;
|
|
|
|
{ok, progress} -> progress;
|
|
|
|
{ok, all} -> all;
|
|
|
|
{ok, Bad} -> exit ({bad_config, {sasl, {errlog_type, Bad}}});
|
|
|
|
_ -> all
|
|
|
|
end.
|
2013-11-11 03:13:29 +01:00
|
|
|
|
|
|
|
-endif.
|