From 12171e196fa5a33affc4ba89ba191d53f2d47016 Mon Sep 17 00:00:00 2001 From: Wilson Li Date: Thu, 4 Jan 2018 14:26:24 +0900 Subject: [PATCH] Allow crash log to set rotator module --- README.md | 1 + src/lager.app.src | 2 ++ src/lager_crash_log.erl | 42 +++++++++++++++++++++-------------------- src/lager_sup.erl | 8 +++++++- 4 files changed, 32 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 1376846b..a8191af1 100644 --- a/README.md +++ b/README.md @@ -542,6 +542,7 @@ used: * `crash_log_size` * `crash_log_date` * `crash_log_count` +* `crash_log_rotator` See the `.app.src` file for further details. diff --git a/src/lager.app.src b/src/lager.app.src index bd476a6e..29142e15 100644 --- a/src/lager.app.src +++ b/src/lager.app.src @@ -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 diff --git a/src/lager_crash_log.erl b/src/lager_crash_log.erl index 5c43cca7..b40644b4 100644 --- a/src/lager_crash_log.erl +++ b/src/lager_crash_log.erl @@ -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(), @@ -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 @@ -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) -> @@ -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}} -> @@ -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( @@ -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), @@ -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( @@ -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), @@ -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( @@ -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), diff --git a/src/lager_sup.erl b/src/lager_sup.erl index 57b214b6..adf95ade 100644 --- a/src/lager_sup.erl +++ b/src/lager_sup.erl @@ -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) -> @@ -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]}].