26
26
%% with the result of closing the old handler when swapping handlers.
27
27
%% The first init/1 additionally allows for simple log rotation
28
28
%% when the suffix is not the empty string.
29
%% The original init/2 also opened the file in 'write' mode, thus
30
%% overwriting old logs. To remedy this, init/2 from
31
%% lib/stdlib/src/error_logger_file_h.erl from R14B3 was copied as
32
%% init_file/2 and changed so that it opens the file in 'append' mode.
30
34
%% Used only when swapping handlers in log rotation
31
35
init({{File, Suffix}, []}) ->
32
case rabbit_misc:append_file(File, Suffix) of
36
case rabbit_file:append_file(File, Suffix) of
37
ok -> file:delete(File),
35
40
rabbit_log:error("Failed to append contents of "
36
41
"log file '~s' to '~s':~n~p~n",
46
51
init({File, []}) ->
48
init({File, _Type} = FileInfo) ->
49
rabbit_misc:ensure_parent_dirs_exist(File),
50
error_logger_file_h:init(FileInfo);
53
%% Used only when taking over from the tty handler
54
init({{File, []}, _}) ->
56
init({File, {error_logger, Buf}}) ->
57
rabbit_file:ensure_parent_dirs_exist(File),
58
init_file(File, {error_logger, Buf});
52
rabbit_misc:ensure_parent_dirs_exist(File),
53
error_logger_file_h:init(File).
60
rabbit_file:ensure_parent_dirs_exist(File),
63
init_file(File, {error_logger, Buf}) ->
64
case init_file(File, error_logger) of
65
{ok, {Fd, File, PrevHandler}} ->
66
[handle_event(Event, {Fd, File, PrevHandler}) ||
67
{_, Event} <- lists:reverse(Buf)],
68
{ok, {Fd, File, PrevHandler}};
72
init_file(File, PrevHandler) ->
73
process_flag(trap_exit, true),
74
case file:open(File, [append]) of
75
{ok,Fd} -> {ok, {Fd, File, PrevHandler}};
55
79
handle_event(Event, State) ->
56
80
error_logger_file_h:handle_event(Event, State).