Moved constants to configuration file

This commit is contained in:
Fabio Salvini 2017-07-02 11:53:21 +02:00
parent 85c9443407
commit 0222425c53
2 changed files with 34 additions and 3 deletions

View File

@ -18,6 +18,8 @@
terminate/2, code_change/3]).
-define(SERVER, ?MODULE).
-define(DEFAULT_TIMER_TIME, 1000).
-define(DEFAULT_SAFE_TIMER_TIME, 30000).
-record(log, {file, error_regex}).
%% -record(state, {}).
@ -100,8 +102,8 @@ handle_cast(_Msg, State) ->
handle_info({log_line, Text}, [off, Log = #log{error_regex = ErrorRegex}]) ->
case isError(Text, ErrorRegex) of
true ->
{ok, Timer} = timer:send_after(1000, {timeout}),
{ok, SafeTimer} = timer:send_after(30000, {timeout}),
{ok, Timer} = timer:send_after(timer_time(), {timeout}),
{ok, SafeTimer} = timer:send_after(safe_timer_time(), {timeout}),
{noreply, [on, Log, Text, Timer, SafeTimer]};
false -> {noreply, [off, Log]}
end;
@ -109,7 +111,7 @@ handle_info({log_line, Text}, [on, Log = #log{error_regex = ErrorRegex}, Error,
case isError(Text, ErrorRegex) of
true ->
timer:cancel(Timer),
{ok, NewTimer} = timer:send_after(1000, {timeout}),
{ok, NewTimer} = timer:send_after(timer_time(), {timeout}),
{noreply, [on, Log, Error ++ Text, NewTimer, SafeTimer]};
false ->
{noreply, [on, Log, Error ++ Text, Timer, SafeTimer]}
@ -155,3 +157,11 @@ isError(Text, ErrorRegex) ->
nomatch ->
false
end.
timer_time() ->
{ok, ProcessingConfig} = application:get_env(log_monitor, processing_config),
proplists:get_value(gathering_time, ProcessingConfig, ?DEFAULT_TIMER_TIME).
safe_timer_time() ->
{ok, ProcessingConfig} = application:get_env(log_monitor, processing_config),
proplists:get_value(max_gathering_time, ProcessingConfig, ?DEFAULT_TIMER_TIME).

View File

@ -4,12 +4,19 @@
{email_config,
[
{sender, "log@monitor.com"},
%% The receiver to use for all the groups that do not specify any receiver.
{default_receiver, "salvini.fabio001@gmail.com"},
%% The receiver to use for the application errors.
{admin_receiver, "salvini.fabio001@gmail.com"},
%% Subject of the email.
%% Placeholders:
%% %f: the full path of the log file (ex. /var/log/myapp.log).
%% %F: the basename of the log file (ex. myapp).
{subject, "[%F] Error notification"},
{connection, [
{relay, "smtp.fabiosalvinii.com"},
{username, "salvini.fabio001@gmail.com"},
@ -17,9 +24,23 @@
}
]
},
{processing_config,
[
%% How long to gather log lines after an error occurs.
%% If another error occurs, the time is reset.
{gathering_time, 1000},
%% Max time for gathering log lines.
%% Precaution in case of infinite consecutive errors.
{max_gathering_time, 30000}
]
},
%% Location of the file that contains the logs to monitor.
{logfiles_config, "/home/fsalvini/gitRepos/log_monitor/config/logfiles.config"}
]
},
%% Directory where the application data is stored.
%% WARNING: If you leave it inside /tmp, every time the node is restarted the
%% application will reset.