leo_logger_rotator.erl предоставляет подсказку для решения.
Мы можем изменить lager_rotator_default.erl на наш lager_rotator.erl, который является функцией «Пользовательское вращение журнала» в лагере.
-module(lager_rotator).
-include_lib("kernel/include/file.hrl").
-behaviour(lager_rotator_behaviour).
-export([
create_logfile/2, open_logfile/2, ensure_logfile/4, rotate_logfile/2
]).
create_logfile(Name, Buffer) ->
{{Y, M, D}, {H, _, _}} = calendar:now_to_local_time(os:timestamp()),
DateHour = {Y, M, D, H},
FileName = filename(Name, DateHour, 1),
file:delete(Name),
file:make_symlink(filename:absname(FileName), Name),
open_logfile(Name, Buffer).
open_logfile(Name, Buffer) ->
case filelib:ensure_dir(Name) of
ok ->
Options = [append, raw] ++
case Buffer of
{Size, Interval} when is_integer(Interval), Interval >= 0, is_integer(Size), Size >= 0 ->
[{delayed_write, Size, Interval}];
_ -> []
end,
case file:open(Name, Options) of
{ok, FD} ->
case file:read_file_info(Name) of
{ok, FInfo} ->
Inode = FInfo#file_info.inode,
{ok, {FD, Inode, FInfo#file_info.size}};
X -> X
end;
Y -> Y
end;
Z -> Z
end.
ensure_logfile(Name, FD, Inode, Buffer) ->
case file:read_link(Name) of
{ok, _} ->
lager_ensure_logfile(Name, FD, Inode, Buffer);
_ ->
create_logfile(Name, Buffer)
end.
lager_ensure_logfile(Name, undefined, _Inode, Buffer) ->
open_logfile(Name, Buffer);
lager_ensure_logfile(Name, FD, Inode, Buffer) ->
case file:read_file_info(Name) of
{ok, FInfo} ->
Inode2 = FInfo#file_info.inode,
case Inode == Inode2 of
true ->
{ok, {FD, Inode, FInfo#file_info.size}};
false ->
%% delayed write can cause file:close not to do a close
_ = file:close(FD),
_ = file:close(FD),
case open_logfile(Name, Buffer) of
{ok, {FD2, Inode3, Size}} ->
%% inode changed, file was probably moved and
%% recreated
{ok, {FD2, Inode3, Size}};
Error ->
Error
end
end;
_ ->
%% delayed write can cause file:close not to do a close
_ = file:close(FD),
_ = file:close(FD),
case open_logfile(Name, Buffer) of
{ok, {FD2, Inode3, Size}} ->
%% file was removed
{ok, {FD2, Inode3, Size}};
Error ->
Error
end
end.
rotate_logfile(Name, _Count) ->
case file:read_link(Name) of
{ok, LinkedName} ->
case filelib:file_size(LinkedName) of
0 ->
%% if the files size is zero, it is removed
catch file:delete(LinkedName);
_ ->
void
end;
_ ->
void
end,
{ok, {FD, _, _}} = create_logfile(Name, []),
file:close(FD).
%% @doc Create name of a new file
%% @private
filename(BaseFileName, DateHour, Branch) ->
FileName = lists:append([BaseFileName,
suffix(DateHour, false), ".", integer_to_list(Branch)
]),
case filelib:is_file(FileName) of
true ->
filename(BaseFileName, DateHour, Branch + 1);
_ ->
FileName
end.
%% @doc Zero-padding number
%% @private
zeropad(Num, MinLength) ->
NumStr = integer_to_list(Num),
zeropad_str(NumStr, MinLength - length(NumStr)).
zeropad_str(NumStr, Zeros) when Zeros > 0 ->
zeropad_str([$0 | NumStr], Zeros - 1);
zeropad_str(NumStr, _) ->
NumStr.
%% @doc Create a suffix
%% WithHour: true/false, add Hour to filename or not
%% @private
suffix({Y, M, D, H}, WithHour) ->
YS = zeropad(Y, 4),
MS = zeropad(M, 2),
DS = zeropad(D, 2),
HS = zeropad(H, 2),
case WithHour of
true ->
lists:flatten([$., YS, MS, DS, $., HS]);
_ ->
lists:flatten([$., YS, MS, DS])
end.
затем добавьте lager_rotator в lager_file_backend в sys.config
{rotator, lager_rotator}
теперь я могу получить имя файла журнала с датой
debug.log (symbolic link to debug.log.20190426.2)
debug.log.20190426.1
debug.log.20190426.2