summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHubert Plociniczak <hubert@lshift.net>2008-08-19 14:48:03 +0100
committerHubert Plociniczak <hubert@lshift.net>2008-08-19 14:48:03 +0100
commitd6a2ba61a838b604fad47286afa097d136095635 (patch)
treee8005f0be175dad6a6d07393b4ac32eeabe90eb4
parent18c7a16507cde719f143213ed80663964a6e6c61 (diff)
downloadrabbitmq-server-git-d6a2ba61a838b604fad47286afa097d136095635.tar.gz
Used swap_handler mechanism provided in
error_logger to correctly reopen_logs. This creates another copy of the handler which has to be manually removed. Apparently the first one in the list is the old one. Updated man pages.
-rw-r--r--docs/rabbitmqctl.pod13
-rwxr-xr-xscripts/rabbitmq-server2
-rw-r--r--scripts/rabbitmq-server.bat2
-rw-r--r--src/rabbit.erl77
4 files changed, 48 insertions, 46 deletions
diff --git a/docs/rabbitmqctl.pod b/docs/rabbitmqctl.pod
index d7f453fbf0..eb1750d04c 100644
--- a/docs/rabbitmqctl.pod
+++ b/docs/rabbitmqctl.pod
@@ -68,10 +68,10 @@ force_reset
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.
+ When the I<suffix> 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.
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.
@@ -127,6 +127,11 @@ Grant user named foo access to the virtual host called test at the
default Erlang node:
rabbitmqctl map_user_vhost foo test
+
+Append current logs' content to the files with ".1" suffix and reopen
+them:
+
+ rabbitmqctl reopen_logs .1
=head1 SEE ALSO
diff --git a/scripts/rabbitmq-server b/scripts/rabbitmq-server
index a44dd6da24..d78be18eb9 100755
--- a/scripts/rabbitmq-server
+++ b/scripts/rabbitmq-server
@@ -39,7 +39,7 @@ CLUSTER_CONFIG_FILE=/etc/default/rabbitmq_cluster.config
## Log rotation
LOGS="${LOG_BASE}/${NODENAME}.log"
SASL_LOGS="${LOG_BASE}/${NODENAME}-sasl.log"
-BACKUP_EXTENSION=".bak"
+BACKUP_EXTENSION=".1"
[ -f "${LOGS}" ] && cat "${LOGS}" >> "${LOGS}${BACKUP_EXTENSION}"
[ -f "${SASL_LOGS}" ] && cat "${SASL_LOGS}" >> "${SASL_LOGS}${BACKUP_EXTENSION}"
diff --git a/scripts/rabbitmq-server.bat b/scripts/rabbitmq-server.bat
index 46f4bd923e..a32fe0fc21 100644
--- a/scripts/rabbitmq-server.bat
+++ b/scripts/rabbitmq-server.bat
@@ -65,7 +65,7 @@ set LOG_BASE=%RABBITMQ_BASE_UNIX%/log
rem We save the previous logs in their respective backup
rem Log management (rotation, filtering based of size...) is left as an exercice for the user.
-set BACKUP_EXTENSION=.bak
+set BACKUP_EXTENSION=.1
set LOGS="%RABBITMQ_BASE%\log\%NODENAME%.log"
set SASL_LOGS="%RABBITMQ_BASE%\log\%NODENAME%-sasl.log"
diff --git a/src/rabbit.erl b/src/rabbit.erl
index a5a74231bc..3de403789b 100644
--- a/src/rabbit.erl
+++ b/src/rabbit.erl
@@ -51,7 +51,7 @@
-spec(stop/0 :: () -> 'ok').
-spec(stop_and_halt/0 :: () -> 'ok').
-spec(reopen_logs/0 :: () -> 'ok').
--spec(reopen_logs/1 :: name() -> 'ok').
+-spec(reopen_logs/1 :: name() -> 'ok' | {'error', 'cannot_append_logfile'}).
-spec(status/0 :: () ->
[{running_applications, [{atom(), string(), string()}]} |
{nodes, [node()]} |
@@ -89,13 +89,15 @@ status() ->
rabbit_mnesia:status().
reopen_logs() ->
- ok = reopen_main_logs([]),
- ok = reopen_sasl_logs([]).
+ ok = reopen_logs(error_log_location(), [], main_log),
+ ok = reopen_logs(sasl_log_location(), [], sasl_log).
reopen_logs(Suffix) ->
LSuffix = binary_to_list(Suffix),
- ok = reopen_main_logs(LSuffix),
- ok = reopen_sasl_logs(LSuffix).
+ case reopen_logs(error_log_location(), LSuffix, main_log) of
+ ok -> reopen_logs(sasl_log_location(), LSuffix, sasl_log);
+ Error -> Error
+ end.
%%--------------------------------------------------------------------
@@ -273,46 +275,41 @@ sasl_log_location() ->
_ -> undefined
end.
-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: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(Suffix) ->
- case sasl_log_location() of
+reopen_logs(File, Suffix,Swap) ->
+ case File of
undefined -> ok;
tty -> ok;
- 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()})
+ _ -> case append_to_log_file(File, Suffix) of
+ omit -> swap_handler(Swap, File);
+ ok -> swap_handler(Swap, File);
+ Error -> Error
+ end
end.
-rotate_log_file(File, Suffix) ->
+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.
+
+append_to_log_file(File, Suffix) ->
case file:read_file_info(File) of
- {ok, FInfo} -> rename_log_file(File, FInfo#file_info.size, Suffix);
- {error, _} -> ok
+ {ok, FInfo} -> append_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
+append_file(_, 0, _) ->
+ omit;
+append_file(_, _, []) ->
+ omit;
+append_file(File, _, Suffix) ->
+ case file:read_file(File) of
+ {ok, Data} -> file:write_file([File, Suffix], Data, [append]);
+ {error, _} -> {error, cannot_append_logfile}
end.