ahmednawras/log4erl

Unexpected requests to disk

Closed this issue · 1 comments

Hi,
We have following section in config:

logger benchmark_logger{
file_appender benchmark_appender{
dir = "log/",
level = info,
file = benchmark,
type = size,
max = 10000,
%max = 5,
suffix = log,
rotation = 9999999,
format = '[%d %T] %l%n'
}
During writting to this log we have only one file in log/ directory also log4erl:info/2 is hanging very long time.

file_appender.erl

rotate(#file_appender{fd = Fd, dir=Dir,  file_name=Fn, counter=Cntr, rotation=Rot, suffix=Suf, log_type=Ltype, level=Level, format=Format} = _S) ->
    file:close(Fd),
    ?LOG("Starting rotation~n"),
    rotate_file(Dir ++ "/" ++ Fn, Rot - 1, Suf),
    Src = Dir ++ "/" ++ Fn ++ "." ++ Suf,
    {ok ,Fd2} = file:open(Src, ?FILE_OPTIONS_ROTATE),
    State2 = #file_appender{dir = Dir, file_name = Fn, fd = Fd2, counter=Cntr, log_type = Ltype, rotation = Rot, suffix=Suf, level=Level, format=Format},
    {ok, State2}.

rotate_file(FileBase, Index, Suffix) when Index > 0 ->
    file:rename(FileBase ++ "_" ++ integer_to_list(Index) ++ "." ++ Suffix, 
        FileBase ++ "_" ++ integer_to_list(Index + 1) ++ "." ++ Suffix),
    rotate_file(FileBase, Index - 1, Suffix);
rotate_file(FileBase, _Index, Suffix) ->
    file:rename(FileBase ++ "." ++ Suffix, FileBase ++ "_1." ++ Suffix).

As you can see in rotate_file/3 function we try check file via file:rename many times (9999999), even if file not existed.

Hi,

The code above added to preserve descending order of files number after rotation. It is not a big issue if rotation is set to small number but I agree there could be more elegant solutions. Perhaps the best is to rotate files in descending order. What do you think? If you agree, I'll go ahead and change the code.

BR.

Ahmed