fix logger
This commit is contained in:
parent
b274b48e3d
commit
3b46f8fba4
@ -13,6 +13,7 @@
|
|||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([start_link/1, start_link/2, write/2]).
|
-export([start_link/1, start_link/2, write/2]).
|
||||||
|
-export([archive_file/1]).
|
||||||
|
|
||||||
%% gen_server callbacks
|
%% gen_server callbacks
|
||||||
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
|
||||||
@ -89,12 +90,7 @@ handle_cast({write, Data}, State = #state{file = {OldIoDevice, OldFilePath}, que
|
|||||||
true ->
|
true ->
|
||||||
ok = file:close(OldIoDevice),
|
ok = file:close(OldIoDevice),
|
||||||
%% 归档
|
%% 归档
|
||||||
erlang:spawn(fun() ->
|
erlang:spawn(fun() -> archive_file(OldFilePath) end),
|
||||||
%% 压缩文件
|
|
||||||
ok = erl_tar:create(OldFilePath ++ ".tar", [OldFilePath], [compressed]),
|
|
||||||
%% 删除原来的文件
|
|
||||||
ok = file:delete(OldFilePath)
|
|
||||||
end),
|
|
||||||
|
|
||||||
%% 开启新的文件
|
%% 开启新的文件
|
||||||
NewFilePath = make_file(FileName, NDate),
|
NewFilePath = make_file(FileName, NDate),
|
||||||
@ -189,4 +185,17 @@ maybe_new_file({Y, M, D}, {Y0, M0, D0}) ->
|
|||||||
% {Date, _} = calendar:gregorian_seconds_to_datetime(Seconds - Day * 86400),
|
% {Date, _} = calendar:gregorian_seconds_to_datetime(Seconds - Day * 86400),
|
||||||
% FilePath = make_file(FileName, Date),
|
% FilePath = make_file(FileName, Date),
|
||||||
% filelib:is_file(FilePath) andalso file:delete(FilePath)
|
% filelib:is_file(FilePath) andalso file:delete(FilePath)
|
||||||
% end, lists:seq(1, 10)).
|
% end, lists:seq(1, 10)).
|
||||||
|
|
||||||
|
|
||||||
|
-spec archive_file(FilePath :: string()) -> ok.
|
||||||
|
archive_file(FilePath) when is_list(FilePath) ->
|
||||||
|
TarFile = FilePath ++ ".tar",
|
||||||
|
{ok, Tar} = erl_tar:open(TarFile, [write, compressed]),
|
||||||
|
BaseName = filename:basename(FilePath),
|
||||||
|
{ok, Data} = file:read_file(FilePath),
|
||||||
|
|
||||||
|
ok = erl_tar:add(Tar, Data, BaseName, []),
|
||||||
|
ok = erl_tar:close(Tar),
|
||||||
|
%% 删除原来的文件
|
||||||
|
ok = file:delete(FilePath).
|
||||||
Loading…
x
Reference in New Issue
Block a user