Skip to content

Commit

Permalink
Allow crash log to set rotator module
Browse files Browse the repository at this point in the history
  • Loading branch information
windkit committed Jan 4, 2018
1 parent 9669bb1 commit 6027aac
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
2 changes: 2 additions & 0 deletions src/lager.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
%% Number of rotated crash logs to keep, 0 means keep only the
%% current one - default is 0
{crash_log_count, 5},
%% Crash Log Rotator Module - default is lager_rotator_default
{crash_log_rotator, lager_rotator_default},
%% Whether to redirect error_logger messages into the default lager_event sink - defaults to true
{error_logger_redirect, true},
%% How many messages per second to allow from error_logger before we start dropping them
Expand Down
42 changes: 22 additions & 20 deletions src/lager_crash_log.erl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2,
code_change/3]).

-export([start_link/5, start/5]).
-export([start_link/6, start/6]).

-record(state, {
name :: string(),
Expand All @@ -56,32 +56,34 @@
size :: integer(),
date :: undefined | string(),
count :: integer(),
flap=false :: boolean()
flap=false :: boolean(),
rotator :: atom()
}).

%% @private
start_link(Filename, MaxBytes, Size, Date, Count) ->
start_link(Filename, MaxBytes, Size, Date, Count, Rotator) ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [Filename, MaxBytes,
Size, Date, Count], []).
Size, Date, Count, Rotator], []).

%% @private
start(Filename, MaxBytes, Size, Date, Count) ->
start(Filename, MaxBytes, Size, Date, Count, Rotator) ->
gen_server:start({local, ?MODULE}, ?MODULE, [Filename, MaxBytes, Size,
Date, Count], []).
Date, Count, Rotator], []).

%% @private
init([RelFilename, MaxBytes, Size, Date, Count]) ->
init([RelFilename, MaxBytes, Size, Date, Count, Rotator]) ->
Filename = lager_util:expand_path(RelFilename),
case lager_rotator_default:open_logfile(Filename, false) of
case Rotator:open_logfile(Filename, false) of
{ok, {FD, Inode, _}} ->
schedule_rotation(Date),
{ok, #state{name=Filename, fd=FD, inode=Inode,
fmtmaxbytes=MaxBytes, size=Size, count=Count, date=Date}};
fmtmaxbytes=MaxBytes, size=Size, count=Count, date=Date,
rotator=Rotator}};
{error, Reason} ->
?INT_LOG(error, "Failed to open crash log file ~s with error: ~s",
[Filename, file:format_error(Reason)]),
{ok, #state{name=Filename, fmtmaxbytes=MaxBytes, flap=true,
size=Size, count=Count, date=Date}}
size=Size, count=Count, date=Date, rotator=Rotator}}
end.

%% @private
Expand All @@ -99,8 +101,8 @@ handle_cast(_Request, State) ->
{noreply, State}.

%% @private
handle_info(rotate, #state{name=Name, count=Count, date=Date} = State) ->
_ = lager_rotator_default:rotate_logfile(Name, Count),
handle_info(rotate, #state{name=Name, count=Count, date=Date, rotator=Rotator} = State) ->
_ = Rotator:rotate_logfile(Name, Count),
schedule_rotation(Date),
{noreply, State};
handle_info(_Info, State) ->
Expand Down Expand Up @@ -187,7 +189,7 @@ sasl_limited_str(crash_report, Report, FmtMaxBytes) ->
lager_stdlib:proc_lib_format(Report, FmtMaxBytes).

do_log({log, Event}, #state{name=Name, fd=FD, inode=Inode, flap=Flap,
fmtmaxbytes=FmtMaxBytes, size=RotSize, count=Count} = State) ->
fmtmaxbytes=FmtMaxBytes, size=RotSize, count=Count, rotator=Rotator} = State) ->
%% borrowed from riak_err
{ReportStr, Pid, MsgStr, _ErrorP} = case Event of
{error, _GL, {Pid1, Fmt, Args}} ->
Expand All @@ -202,9 +204,9 @@ do_log({log, Event}, #state{name=Name, fd=FD, inode=Inode, flap=Flap,
if ReportStr == ignore ->
{ok, State};
true ->
case lager_rotator_default:ensure_logfile(Name, FD, Inode, false) of
case Rotator:ensure_logfile(Name, FD, Inode, false) of
{ok, {_, _, Size}} when RotSize /= 0, Size > RotSize ->
_ = lager_rotator_default:rotate_logfile(Name, Count),
_ = Rotator:rotate_logfile(Name, Count),
handle_cast({log, Event}, State);
{ok, {NewFD, NewInode, _Size}} ->
{Date, TS} = lager_util:format_time(
Expand Down Expand Up @@ -268,7 +270,7 @@ filesystem_test_() ->
fun(CrashLog) ->
{"under normal circumstances, file should be opened",
fun() ->
{ok, _} = ?MODULE:start_link(CrashLog, 65535, 0, undefined, 0),
{ok, _} = ?MODULE:start_link(CrashLog, 65535, 0, undefined, 0, lager_rotator_default),
_ = gen_event:which_handlers(error_logger),
sync_error_logger:error_msg("Test message\n"),
{ok, Bin} = file:read_file(CrashLog),
Expand All @@ -280,7 +282,7 @@ filesystem_test_() ->
fun() ->
{ok, FInfo} = file:read_file_info(CrashLog),
file:write_file_info(CrashLog, FInfo#file_info{mode = 0}),
{ok, _} = ?MODULE:start_link(CrashLog, 65535, 0, undefined, 0),
{ok, _} = ?MODULE:start_link(CrashLog, 65535, 0, undefined, 0, lager_rotator_default),
?assertEqual(1, lager_test_backend:count()),
{_Level, _Time, Message,_Metadata} = lager_test_backend:pop(),
?assertEqual(
Expand All @@ -291,7 +293,7 @@ filesystem_test_() ->
fun(CrashLog) ->
{"file that becomes unavailable at runtime should trigger an error message",
fun() ->
{ok, _} = ?MODULE:start_link(CrashLog, 65535, 0, undefined, 0),
{ok, _} = ?MODULE:start_link(CrashLog, 65535, 0, undefined, 0, lager_rotator_default),
?assertEqual(0, lager_test_backend:count()),
sync_error_logger:error_msg("Test message\n"),
_ = gen_event:which_handlers(error_logger),
Expand All @@ -316,7 +318,7 @@ filesystem_test_() ->
{ok, FInfo} = file:read_file_info(CrashLog),
OldPerms = FInfo#file_info.mode,
file:write_file_info(CrashLog, FInfo#file_info{mode = 0}),
{ok, _} = ?MODULE:start_link(CrashLog, 65535, 0, undefined, 0),
{ok, _} = ?MODULE:start_link(CrashLog, 65535, 0, undefined, 0, lager_rotator_default),
?assertEqual(1, lager_test_backend:count()),
{_Level, _Time, Message,_Metadata} = lager_test_backend:pop(),
?assertEqual(
Expand All @@ -332,7 +334,7 @@ filesystem_test_() ->
fun(CrashLog) ->
{"external logfile rotation/deletion should be handled",
fun() ->
{ok, _} = ?MODULE:start_link(CrashLog, 65535, 0, undefined, 0),
{ok, _} = ?MODULE:start_link(CrashLog, 65535, 0, undefined, 0, lager_rotator_default),
?assertEqual(0, lager_test_backend:count()),
sync_error_logger:error_msg("Test message~n"),
_ = gen_event:which_handlers(error_logger),
Expand Down
8 changes: 7 additions & 1 deletion src/lager_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ determine_rotation_date({ok, Val3}) ->
determine_rotation_date(_) ->
undefined.

determine_rotator_mod({ok, Mod}, _Default) when is_atom(Mod) ->
Mod;
determine_rotator_mod(_, Default) ->
Default.

decide_crash_log(undefined) ->
[];
decide_crash_log(false) ->
Expand All @@ -79,8 +84,9 @@ decide_crash_log(File) ->
RotationCount = validate_positive(application:get_env(lager, crash_log_count), 0),

RotationDate = determine_rotation_date(application:get_env(lager, crash_log_date)),
RotationMod = determine_rotator_mod(application:get_env(lager, crash_log_rotator), lager_rotator_default),


[{lager_crash_log, {lager_crash_log, start_link, [File, MaxBytes,
RotationSize, RotationDate, RotationCount]},
RotationSize, RotationDate, RotationCount, RotationMod]},
permanent, 5000, worker, [lager_crash_log]}].

0 comments on commit 6027aac

Please sign in to comment.