From 433b04ce96873023190557df81f570afd3999c5e Mon Sep 17 00:00:00 2001 From: Hubert Plociniczak Date: Tue, 26 Aug 2008 13:25:55 +0100 Subject: Added wrappers around standard error_logger_file_h and sasl_report_file_h modules, because they were not expecting proper arguments in init/1 when swapping handlers. Renamed reopen_logs to rotate_logs. --- docs/rabbitmqctl.pod | 13 ++++---- src/rabbit.erl | 57 +++++++++++++++++++---------------- src/rabbit_control.erl | 12 ++++---- src/rabbit_error_logger_file_h.erl | 52 ++++++++++++++++++++++++++++++++ src/rabbit_sasl_report_file_h.erl | 61 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 157 insertions(+), 38 deletions(-) create mode 100644 src/rabbit_error_logger_file_h.erl create mode 100644 src/rabbit_sasl_report_file_h.erl diff --git a/docs/rabbitmqctl.pod b/docs/rabbitmqctl.pod index eb1750d0..d6e0bac7 100644 --- a/docs/rabbitmqctl.pod +++ b/docs/rabbitmqctl.pod @@ -66,12 +66,13 @@ force_reset It should only be used as a last resort if the database or cluster configuration has been corrupted. -reopen_logs [suffix] - instruct the RabbitMQ node to close and reopen the log files. - When the I value is provided, the RabbitMQ broker will - attempt to append the current contents of the log file to the file - with name composed of the original name and the suffix. It will - create a new file if such a file does not already exist. +rotate_logs [suffix] + instruct the RabbitMQ node to rotate the log files. The RabbitMQ + broker will attempt to append the current contents of the log file + to the file with name composed of the original name and the suffix. + It will create a new file if such a file does not already exist. + When no I is specified, the log file is simply reopened; + no rotation takes place. 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 3de40378..c82e0a13 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, reopen_logs/1]). +-export([start/0, stop/0, stop_and_halt/0, status/0, rotate_logs/0, rotate_logs/1]). -export([start/2, stop/1]). @@ -50,8 +50,8 @@ -spec(start/0 :: () -> 'ok'). -spec(stop/0 :: () -> 'ok'). -spec(stop_and_halt/0 :: () -> 'ok'). --spec(reopen_logs/0 :: () -> 'ok'). --spec(reopen_logs/1 :: name() -> 'ok' | {'error', 'cannot_append_logfile'}). +-spec(rotate_logs/0 :: () -> 'ok'). +-spec(rotate_logs/1 :: name() -> 'ok' | {'error', 'cannot_append_logfile'}). -spec(status/0 :: () -> [{running_applications, [{atom(), string(), string()}]} | {nodes, [node()]} | @@ -88,14 +88,14 @@ status() -> [{running_applications, application:which_applications()}] ++ rabbit_mnesia:status(). -reopen_logs() -> - ok = reopen_logs(error_log_location(), [], main_log), - ok = reopen_logs(sasl_log_location(), [], sasl_log). +rotate_logs() -> + ok = rotate_logs(error_log_location(wrapper), [], main_log), + ok = rotate_logs(sasl_log_location(), [], sasl_log). -reopen_logs(Suffix) -> +rotate_logs(Suffix) -> LSuffix = binary_to_list(Suffix), - case reopen_logs(error_log_location(), LSuffix, main_log) of - ok -> reopen_logs(sasl_log_location(), LSuffix, sasl_log); + case rotate_logs(error_log_location(wrapper), LSuffix, main_log) of + ok -> rotate_logs(sasl_log_location(), LSuffix, sasl_log); Error -> Error end. @@ -176,7 +176,11 @@ start(normal, []) -> {ok, DefaultVHost} = application:get_env(default_vhost), ok = error_logger:add_report_handler( rabbit_error_logger, [DefaultVHost]), - ok = start_builtin_amq_applications() + ok = start_builtin_amq_applications(), + ok = swap_handler(error_logger_file_h, rabbit_error_logger_file_h, + error_log_location(default)), + ok = swap_handler(sasl_report_file_h, rabbit_sasl_report_file_h, + sasl_log_location()) end}, {"TCP listeners", fun () -> @@ -208,7 +212,7 @@ print_banner() -> ?PROTOCOL_VERSION_MAJOR, ?PROTOCOL_VERSION_MINOR, ?COPYRIGHT_MESSAGE, ?INFORMATION_MESSAGE]), io:format("Logging to ~p~nSASL logging to ~p~n~n", - [error_log_location(), sasl_log_location()]). + [error_log_location(default), sasl_log_location()]). start_child(Mod) -> {ok,_} = supervisor:start_child(rabbit_sup, @@ -260,10 +264,15 @@ ensure_working_log_config() -> _Filename -> ok end. -error_log_location() -> +error_log_location(default) -> case error_logger:logfile(filename) of {error,no_log_file} -> tty; File -> File + end; +error_log_location(wrapper) -> + case gen_event:call(error_logger, rabbit_error_logger_file_h, filename) of + {error,no_log_file} -> tty; + File -> File end. sasl_log_location() -> @@ -275,28 +284,24 @@ sasl_log_location() -> _ -> undefined end. -reopen_logs(File, Suffix,Swap) -> +rotate_logs(File, Suffix, main_log) -> + rotate_logs(File, Suffix, rabbit_error_logger_file_h, rabbit_error_logger_file_h); +rotate_logs(File, Suffix, sasl_log) -> + rotate_logs(File, Suffix, rabbit_sasl_report_file_h, rabbit_sasl_report_file_h). + +rotate_logs(File, Suffix, OldHandler, NewHandler) -> case File of undefined -> ok; tty -> ok; _ -> case append_to_log_file(File, Suffix) of - omit -> swap_handler(Swap, File); - ok -> swap_handler(Swap, File); + omit -> swap_handler(OldHandler, NewHandler, File); + ok -> swap_handler(OldHandler, NewHandler, File); Error -> Error end end. -swap_handler(main_log, File) -> - error_logger:swap_handler({logfile, File}), - error_logger:delete_report_handler(error_logger_file_h), - ok; -swap_handler(sasl_log, File ) -> - gen_event:swap_handler(error_logger, - {error_logger, swap}, - {sasl_report_file_h, File}), - gen_event:add_handler(error_logger, error_logger, []), - error_logger:delete_report_handler(sasl_report_file_h), - ok. +swap_handler(Old, New, File) -> + gen_event:swap_handler(error_logger, {Old, swap}, {New, File}). append_to_log_file(File, Suffix) -> case file:read_file_info(File) of diff --git a/src/rabbit_control.erl b/src/rabbit_control.erl index 4c1b92bb..28761eeb 100644 --- a/src/rabbit_control.erl +++ b/src/rabbit_control.erl @@ -73,7 +73,7 @@ Available commands: force_reset cluster ... status - reopen_logs [Suffix] + rotate_logs [Suffix] add_user delete_user @@ -130,12 +130,12 @@ action(status, Node, []) -> io:format("~n~p~n", [Res]), ok; -action(reopen_logs, Node, []) -> +action(rotate_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}); + call(Node, {rabbit, rotate_logs, []}); +action(rotate_logs, Node, Args = [Suffix]) -> + io:format("Rotating logs to files with suffix ~p ...", [Suffix]), + call(Node, {rabbit, rotate_logs, Args}); action(add_user, Node, Args = [Username, _Password]) -> io:format("Creating user ~p ...", [Username]), diff --git a/src/rabbit_error_logger_file_h.erl b/src/rabbit_error_logger_file_h.erl new file mode 100644 index 00000000..4fadf46f --- /dev/null +++ b/src/rabbit_error_logger_file_h.erl @@ -0,0 +1,52 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developers of the Original Code are LShift Ltd., +%% Cohesive Financial Technologies LLC., and Rabbit Technologies Ltd. +%% +%% Portions created by LShift Ltd., Cohesive Financial Technologies +%% LLC., and Rabbit Technologies Ltd. are Copyright (C) 2007-2008 +%% LShift Ltd., Cohesive Financial Technologies LLC., and Rabbit +%% Technologies Ltd.; +%% +%% All Rights Reserved. +%% +%% Contributor(s): ______________________________________. +%% + +-module(rabbit_error_logger_file_h). + +-behaviour(gen_event). + +-export([init/1, handle_event/2, handle_call/2, handle_info/2, terminate/2, code_change/3]). + +init({File, []}) -> + error_logger_file_h:init(File); +init({_File, _Type} = FileInfo) -> + error_logger_file_h:init(FileInfo); +init(File) -> + error_logger_file_h:init(File). + +handle_event(Event, State) -> + error_logger_file_h:handle_event(Event, State). + +handle_info(Event, State) -> + error_logger_file_h:handle_info(Event, State). + +handle_call(Event, State) -> + error_logger_file_h:handle_call(Event, State). + +terminate(Reason, State) -> + error_logger_file_h:terminate(Reason, State). + +code_change(OldVsn, State, Extra) -> + error_logger_file_h:code_change(OldVsn, State, Extra). diff --git a/src/rabbit_sasl_report_file_h.erl b/src/rabbit_sasl_report_file_h.erl new file mode 100644 index 00000000..7bfa2ca1 --- /dev/null +++ b/src/rabbit_sasl_report_file_h.erl @@ -0,0 +1,61 @@ +%% The contents of this file are subject to the Mozilla Public License +%% Version 1.1 (the "License"); you may not use this file except in +%% compliance with the License. You may obtain a copy of the License at +%% http://www.mozilla.org/MPL/ +%% +%% Software distributed under the License is distributed on an "AS IS" +%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the +%% License for the specific language governing rights and limitations +%% under the License. +%% +%% The Original Code is RabbitMQ. +%% +%% The Initial Developers of the Original Code are LShift Ltd., +%% Cohesive Financial Technologies LLC., and Rabbit Technologies Ltd. +%% +%% Portions created by LShift Ltd., Cohesive Financial Technologies +%% LLC., and Rabbit Technologies Ltd. are Copyright (C) 2007-2008 +%% LShift Ltd., Cohesive Financial Technologies LLC., and Rabbit +%% Technologies Ltd.; +%% +%% All Rights Reserved. +%% +%% Contributor(s): ______________________________________. +%% + +-module(rabbit_sasl_report_file_h). + +-behaviour(gen_event). + +-export([init/1, handle_event/2, handle_call/2, handle_info/2, terminate/2, code_change/3]). + +init({File, []}) -> + sasl_report_file_h:init({File, sasl_error_logger_type()}); +init({_File, _Type} = FileInfo) -> + sasl_report_file_h:init(FileInfo); +init(File) -> + sasl_report_file_h:init({File, sasl_error_logger_type()}). + +handle_event(Event, State) -> + sasl_report_file_h:handle_event(Event, State). + +handle_info(Event, State) -> + sasl_report_file_h:handle_info(Event, State). + +handle_call(Event, State) -> + sasl_report_file_h:handle_call(Event, State). + +terminate(Reason, State) -> + sasl_report_file_h:terminate(Reason, State). + +code_change(OldVsn, State, Extra) -> + sasl_report_file_h:code_change(OldVsn, State, Extra). + +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. -- cgit v1.2.1