diff options
author | Hubert Plociniczak <hubert@lshift.net> | 2008-08-18 12:11:58 +0100 |
---|---|---|
committer | Hubert Plociniczak <hubert@lshift.net> | 2008-08-18 12:11:58 +0100 |
commit | 17829f7daf06af1014289d45f7788943b80065ec (patch) | |
tree | e68d6b0bc8670adfe193566683e2a683307c78c1 | |
parent | e89e0f2280f575d897c80ea254189b4517fd3c1c (diff) | |
download | rabbitmq-server-17829f7daf06af1014289d45f7788943b80065ec.tar.gz |
Added optional argument that specifies the suffix
of the old log file. Using add_handler and
delete_handler because swap_handlers doesn't behave
as expected.
-rw-r--r-- | docs/rabbitmqctl.pod | 6 | ||||
-rw-r--r-- | src/rabbit.erl | 54 | ||||
-rw-r--r-- | src/rabbit_control.erl | 5 |
3 files changed, 54 insertions, 11 deletions
diff --git a/docs/rabbitmqctl.pod b/docs/rabbitmqctl.pod index ebe0cd43..d7f453fb 100644 --- a/docs/rabbitmqctl.pod +++ b/docs/rabbitmqctl.pod @@ -66,8 +66,12 @@ force_reset It should only be used as a last resort if the database or cluster configuration has been corrupted. -reopen_logs +reopen_logs [suffix] instruct the RabbitMQ node to close and reopen the log files. + When I<suffix> value is provided, the RabbitMQ broker will attempt + to rename the current log file to the file with appended suffix. + If file with the original name and suffix already exists, then + file renaming operation is aborted. This command might be helpful when you are e.g. writing your own logrotate script and you do not want to restart the RabbitMQ node. diff --git a/src/rabbit.erl b/src/rabbit.erl index 7d738ff7..a5a74231 100644 --- a/src/rabbit.erl +++ b/src/rabbit.erl @@ -27,7 +27,7 @@ -behaviour(application). --export([start/0, stop/0, stop_and_halt/0, status/0, reopen_logs/0]). +-export([start/0, stop/0, stop_and_halt/0, status/0, reopen_logs/0, reopen_logs/1]). -export([start/2, stop/1]). @@ -39,6 +39,7 @@ -include("rabbit_framing.hrl"). -include("rabbit.hrl"). +-include_lib("kernel/include/file.hrl"). -define(APPS, [os_mon, mnesia, rabbit]). @@ -50,6 +51,7 @@ -spec(stop/0 :: () -> 'ok'). -spec(stop_and_halt/0 :: () -> 'ok'). -spec(reopen_logs/0 :: () -> 'ok'). +-spec(reopen_logs/1 :: name() -> 'ok'). -spec(status/0 :: () -> [{running_applications, [{atom(), string(), string()}]} | {nodes, [node()]} | @@ -87,8 +89,13 @@ status() -> rabbit_mnesia:status(). reopen_logs() -> - ok = reopen_main_logs(), - ok = reopen_sasl_logs(). + ok = reopen_main_logs([]), + ok = reopen_sasl_logs([]). + +reopen_logs(Suffix) -> + LSuffix = binary_to_list(Suffix), + ok = reopen_main_logs(LSuffix), + ok = reopen_sasl_logs(LSuffix). %%-------------------------------------------------------------------- @@ -266,17 +273,46 @@ sasl_log_location() -> _ -> undefined end. -reopen_main_logs() -> +sasl_error_logger_type() -> + case application:get_env(sasl, errlog_type) of + {ok, error} -> error; + {ok, progress} -> progress; + {ok, all} -> all; + {ok, Bad} -> throw({error, {wrong_errlog_type, Bad}}); + _ -> all + end. + +reopen_main_logs(Suffix) -> case error_log_location() of tty -> ok; - File -> error_logger:swap_handler({logfile, File}) + File -> error_logger:delete_report_handler(error_logger_file_h), + rotate_log_file(File, Suffix), + error_logger:add_report_handler(error_logger_file_h,File) end. -reopen_sasl_logs() -> +reopen_sasl_logs(Suffix) -> case sasl_log_location() of undefined -> ok; tty -> ok; - File -> gen_event:swap_handler(error_logger, - {sasl_error_logger, swap}, - {sasl_report_file_h, File}) + File -> error_logger:delete_report_handler(sasl_report_file_h), + rotate_log_file(File,Suffix), + error_logger:add_report_handler(sasl_report_file_h, + {File, sasl_error_logger_type()}) + end. + +rotate_log_file(File, Suffix) -> + case file:read_file_info(File) of + {ok, FInfo} -> rename_log_file(File, FInfo#file_info.size, Suffix); + {error, _} -> ok + end. + +rename_log_file(_, 0, _) -> + ok; +rename_log_file(_, _, []) -> + ok; +rename_log_file(File, _, Suffix) -> + case file:read_file_info([File,Suffix]) of + {ok, _} -> ok; + {error, enoent} -> file:rename(File, [File, Suffix]); + {error, _} -> ok end. diff --git a/src/rabbit_control.erl b/src/rabbit_control.erl index 5879c811..4c1b92bb 100644 --- a/src/rabbit_control.erl +++ b/src/rabbit_control.erl @@ -73,7 +73,7 @@ Available commands: force_reset cluster <ClusterNode> ... status - reopen_logs + reopen_logs [Suffix] add_user <UserName> <Password> delete_user <UserName> @@ -133,6 +133,9 @@ action(status, Node, []) -> action(reopen_logs, Node, []) -> io:format("Reopening logs for node ~p ...", [Node]), call(Node, {rabbit, reopen_logs, []}); +action(reopen_logs, Node, Args = [Suffix]) -> + io:format("Moving logs to files with suffix ~p and reopening logs ...", [Suffix]), + call(Node, {rabbit, reopen_logs, Args}); action(add_user, Node, Args = [Username, _Password]) -> io:format("Creating user ~p ...", [Username]), |