Skip to content

Commit 2d9b8ce

Browse files
committed
Only start syslog application if it is required
Fix up syslog protocol options to set ip to localhost if unset Ensure default handler for OTP 21.1+ logger is removed
1 parent 1be4734 commit 2d9b8ce

File tree

4 files changed

+94
-41
lines changed

4 files changed

+94
-41
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,11 @@ define PROJECT_ENV
132132
endef
133133

134134
LOCAL_DEPS = sasl mnesia os_mon inets
135-
BUILD_DEPS = rabbitmq_cli
136-
DEPS = ranch syslog lager rabbit_common
135+
BUILD_DEPS = rabbitmq_cli syslog
136+
DEPS = ranch lager rabbit_common
137137
TEST_DEPS = rabbitmq_ct_helpers rabbitmq_ct_client_helpers amqp_client meck proper
138138

139-
dep_syslog = git https://github.com/schlagert/syslog 3.4.3
139+
dep_syslog = git https://github.com/schlagert/syslog 3.4.5
140140

141141
define usage_xml_to_erl
142142
$(subst __,_,$(patsubst $(DOCS_DIR)/rabbitmq%.1.xml, src/rabbit_%_usage.erl, $(subst -,_,$(1))))

src/rabbit.erl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,6 @@ start_apps(Apps) ->
517517

518518
start_apps(Apps, RestartTypes) ->
519519
app_utils:load_applications(Apps),
520-
521520
ConfigEntryDecoder = case application:get_env(rabbit, config_entry_decoder) of
522521
undefined ->
523522
[];

src/rabbit_lager.erl

Lines changed: 75 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
-module(rabbit_lager).
1818

19-
-include("rabbit_log.hrl").
19+
-include_lib("rabbit_common/include/rabbit_log.hrl").
2020

2121
%% API
2222
-export([start_logger/0, log_locations/0, fold_sinks/2,
@@ -26,11 +26,10 @@
2626
-export([configure_lager/0]).
2727

2828
start_logger() ->
29-
application:stop(lager),
30-
application:stop(syslog),
31-
ensure_lager_configured(),
32-
application:ensure_all_started(syslog),
33-
lager:start(),
29+
ok = maybe_remove_logger_handler(),
30+
ok = app_utils:stop_applications([lager, syslog]),
31+
ok = ensure_lager_configured(),
32+
ok = app_utils:start_applications([lager]),
3433
fold_sinks(
3534
fun
3635
(_, [], Acc) ->
@@ -176,9 +175,12 @@ lager_configured() ->
176175
application:get_env(syslog, syslog_error_logger) =/= undefined.
177176

178177
configure_lager() ->
179-
application:load(lager),
180-
application:load(syslog),
178+
ok = app_utils:load_applications([lager]),
181179
%% Turn off reformatting for error_logger messages
180+
case application:get_env(lager, error_logger_redirect) of
181+
undefined -> application:set_env(lager, error_logger_redirect, true);
182+
_ -> ok
183+
end,
182184
case application:get_env(lager, error_logger_format_raw) of
183185
undefined -> application:set_env(lager, error_logger_format_raw, true);
184186
_ -> ok
@@ -190,7 +192,7 @@ configure_lager() ->
190192
%% difference.
191193
case application:get_env(rabbit, lager_log_root) of
192194
{ok, Value} ->
193-
application:set_env(lager, log_root, Value);
195+
ok = application:set_env(lager, log_root, Value);
194196
_ ->
195197
ok
196198
end;
@@ -199,7 +201,7 @@ configure_lager() ->
199201
%% Set rabbit.log config variable based on environment.
200202
prepare_rabbit_log_config(),
201203
%% Configure syslog library.
202-
configure_syslog(),
204+
ok = configure_syslog_error_logger(),
203205
%% At this point we should have rabbit.log application variable
204206
%% configured to generate RabbitMQ log handlers.
205207
GeneratedHandlers = generate_lager_handlers(),
@@ -217,8 +219,8 @@ configure_lager() ->
217219
FormerRabbitHandlers)
218220
end,
219221

220-
application:set_env(lager, handlers, Handlers),
221-
application:set_env(lager, rabbit_handlers, GeneratedHandlers),
222+
ok = application:set_env(lager, handlers, Handlers),
223+
ok = application:set_env(lager, rabbit_handlers, GeneratedHandlers),
222224

223225
%% Setup extra sink/handlers. If they are not configured, redirect
224226
%% messages to the default sink. To know the list of expected extra
@@ -253,29 +255,60 @@ configure_lager() ->
253255
[error_logger_lager_event | list_expected_sinks()],
254256
SinkConfigs),
255257
Sinks = merge_lager_sink_handlers(LagerSinks, GeneratedSinks, []),
256-
application:set_env(lager, extra_sinks, Sinks),
258+
ok = application:set_env(lager, extra_sinks, Sinks),
257259

258260
case application:get_env(lager, error_logger_hwm) of
259261
undefined ->
260-
application:set_env(lager, error_logger_hwm, 1000),
262+
ok = application:set_env(lager, error_logger_hwm, 1000),
261263
% NB: 50 is the default value in lager.app.src
262-
application:set_env(lager, error_logger_hwm_original, 50);
264+
ok = application:set_env(lager, error_logger_hwm_original, 50);
263265
{ok, Val} when is_integer(Val) andalso Val < 1000 ->
264-
application:set_env(lager, error_logger_hwm, 1000),
265-
application:set_env(lager, error_logger_hwm_original, Val);
266+
ok = application:set_env(lager, error_logger_hwm, 1000),
267+
ok = application:set_env(lager, error_logger_hwm_original, Val);
266268
{ok, Val} ->
267-
application:set_env(lager, error_logger_hwm_original, Val),
269+
ok = application:set_env(lager, error_logger_hwm_original, Val),
268270
ok
269271
end,
270272
ok.
271273

272-
configure_syslog() ->
274+
configure_syslog_error_logger() ->
273275
%% Disable error_logger forwarding to syslog if it's not configured
274276
case application:get_env(syslog, syslog_error_logger) of
275-
undefined -> application:set_env(syslog, syslog_error_logger, false);
277+
undefined ->
278+
application:set_env(syslog, syslog_error_logger, false);
276279
_ -> ok
277280
end.
278281

282+
-define(SYSLOG_LOCAL_IP, {ip,{127,0,0,1}}).
283+
configure_syslog() ->
284+
ok = app_utils:load_applications([syslog]),
285+
%% https://github.com/schlagert/syslog#configuration
286+
Protocol = case application:get_env(syslog, protocol) of
287+
undefined ->
288+
{rfc3164, udp, [?SYSLOG_LOCAL_IP]};
289+
%% {protocol,
290+
%% rfc3164 |
291+
%% rfc5424 |
292+
%% {rfc3164 | rfc5424, tcp | udp} |
293+
%% {rfc3164 | rfc5424, udp, [gen_udp:option()]} |
294+
%% {rfc3164 | rfc5424, tcp, [gen_tcp:option()]} |
295+
%% {rfc5424, tls, [ssl:connect_option()]}
296+
%% }
297+
{ok, Rfc} when Rfc =:= rfc3164; Rfc =:= rfc5424 ->
298+
{Rfc, udp, [?SYSLOG_LOCAL_IP]};
299+
{ok, {Rfc, Transport}} when Rfc =:= rfc3164; Rfc =:= rfc5424 ->
300+
{Rfc, Transport, [?SYSLOG_LOCAL_IP]};
301+
{ok, {Rfc, Transport, Opts}} when Rfc =:= rfc3164; Rfc =:= rfc5424 ->
302+
case proplists:lookup(ip, Opts) of
303+
none ->
304+
{Rfc, Transport, [?SYSLOG_LOCAL_IP|Opts]};
305+
_ ->
306+
{Rfc, Transport, Opts}
307+
end
308+
end,
309+
ok = application:unset_env(syslog, protocol),
310+
ok = application:set_env(syslog, protocol, Protocol).
311+
279312
remove_rabbit_handlers(Handlers, FormerHandlers) ->
280313
lists:filter(fun(Handler) ->
281314
not lists:member(Handler, FormerHandlers)
@@ -317,8 +350,9 @@ lager_backend(exchange) -> lager_exchange_backend.
317350
%% Syslog backend is using an old API for configuration and
318351
%% does not support proplists.
319352
generate_handler(syslog_lager_backend, HandlerConfig) ->
320-
Level = proplists:get_value(level, HandlerConfig,
321-
default_config_value(level)),
353+
DefaultConfigVal = default_config_value(level),
354+
Level = proplists:get_value(level, HandlerConfig, DefaultConfigVal),
355+
ok = configure_syslog(),
322356
[{syslog_lager_backend,
323357
[Level,
324358
{},
@@ -384,7 +418,7 @@ prepare_rabbit_log_config() ->
384418

385419
set_env_default_log_disabled() ->
386420
%% Disabling all the logs.
387-
application:set_env(rabbit, log, []).
421+
ok = application:set_env(rabbit, log, []).
388422

389423
set_env_default_log_console() ->
390424
LogConfig = application:get_env(rabbit, log, []),
@@ -395,7 +429,7 @@ set_env_default_log_console() ->
395429
{enabled, true})}),
396430
%% Remove the file handler - disable logging to file
397431
LogConfigConsoleNoFile = lists:keydelete(file, 1, LogConfigConsole),
398-
application:set_env(rabbit, log, LogConfigConsoleNoFile).
432+
ok = application:set_env(rabbit, log, LogConfigConsoleNoFile).
399433

400434
set_env_default_log_file(FileName, Override) ->
401435
LogConfig = application:get_env(rabbit, log, []),
@@ -416,7 +450,7 @@ set_env_default_log_file(FileName, Override) ->
416450
LogConfig
417451
end
418452
end,
419-
application:set_env(rabbit, log, NewLogConfig).
453+
ok = application:set_env(rabbit, log, NewLogConfig).
420454

421455
set_env_upgrade_log_file(FileName) ->
422456
LogConfig = application:get_env(rabbit, log, []),
@@ -436,7 +470,7 @@ set_env_upgrade_log_file(FileName) ->
436470
%% No cahnge. We don't want to override the configured value.
437471
_File -> LogConfig
438472
end,
439-
application:set_env(rabbit, log, NewLogConfig).
473+
ok = application:set_env(rabbit, log, NewLogConfig).
440474

441475
generate_lager_sinks(SinkNames, SinkConfigs) ->
442476
lists:map(fun(SinkName) ->
@@ -523,7 +557,6 @@ merge_lager_sink_handlers([{Name, Sink} | RestSinks], GeneratedSinks, Agg) ->
523557
end;
524558
merge_lager_sink_handlers([], GeneratedSinks, Agg) -> GeneratedSinks ++ Agg.
525559

526-
527560
list_expected_sinks() ->
528561
case application:get_env(rabbit, lager_extra_sinks) of
529562
{ok, List} ->
@@ -543,6 +576,20 @@ list_expected_sinks() ->
543576
%% module is later cover-compiled, the compile option will
544577
%% be lost, so we will be able to retrieve the list from the
545578
%% application environment.
546-
application:set_env(rabbit, lager_extra_sinks, List),
579+
ok = application:set_env(rabbit, lager_extra_sinks, List),
547580
List
548581
end.
582+
583+
maybe_remove_logger_handler() ->
584+
M = logger,
585+
F = remove_handler,
586+
try
587+
ok = erlang:apply(M, F, [default])
588+
catch
589+
error:undef ->
590+
% OK since the logger module only exists in OTP 21.1 or later
591+
ok;
592+
Err:Reason ->
593+
error_logger:error_msg("calling ~p:~p failed: ~p:~p~n",
594+
[M, F, Err, Reason])
595+
end.

src/rabbit_plugins.erl

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ setup() ->
134134
{error, E1} -> throw({error, {cannot_delete_plugins_expand_dir,
135135
[ExpandDir, E1]}})
136136
end,
137-
138137
Enabled = enabled_plugins(),
139138
prepare_plugins(Enabled).
140139

@@ -196,7 +195,7 @@ list(PluginsPath, IncludeRequiredDeps) ->
196195
{AllPlugins, LoadingProblems} = discover_plugins(split_path(PluginsPath)),
197196
{UniquePlugins, DuplicateProblems} = remove_duplicate_plugins(AllPlugins),
198197
Plugins1 = maybe_keep_required_deps(IncludeRequiredDeps, UniquePlugins),
199-
Plugins2 = remove_otp_overrideable_plugins(Plugins1),
198+
Plugins2 = remove_plugins(Plugins1),
200199
maybe_report_plugin_loading_problems(LoadingProblems ++ DuplicateProblems),
201200
ensure_dependencies(Plugins2).
202201

@@ -248,11 +247,19 @@ strictly_plugins(Plugins) ->
248247

249248
%% For a few known cases, an externally provided plugin can be trusted.
250249
%% In this special case, it overrides the plugin.
251-
plugin_provided_by_otp(#plugin{name = eldap}) ->
250+
is_plugin_provided_by_otp(#plugin{name = eldap}) ->
252251
%% eldap was added to Erlang/OTP R15B01 (ERTS 5.9.1). In this case,
253252
%% we prefer this version to the plugin.
254253
rabbit_misc:version_compare(erlang:system_info(version), "5.9.1", gte);
255-
plugin_provided_by_otp(_) ->
254+
is_plugin_provided_by_otp(_) ->
255+
false.
256+
257+
is_skipped_plugin(#plugin{name = syslog}) ->
258+
% syslog is shipped as an .ez file, but it's not an actual plugin and
259+
% it's not a direct dependency of the rabbit application, so we must
260+
% skip it here
261+
true;
262+
is_skipped_plugin(_) ->
256263
false.
257264

258265
%% Make sure we don't list OTP apps in here, and also that we detect
@@ -295,7 +302,6 @@ running_plugins() ->
295302

296303
prepare_plugins(Enabled) ->
297304
ExpandDir = plugins_expand_dir(),
298-
299305
AllPlugins = list(plugins_dir()),
300306
Wanted = dependencies(false, Enabled, AllPlugins),
301307
WantedPlugins = lookup_plugins(Wanted, AllPlugins),
@@ -306,7 +312,6 @@ prepare_plugins(Enabled) ->
306312
{error, E2} -> throw({error, {cannot_create_plugins_expand_dir,
307313
[ExpandDir, E2]}})
308314
end,
309-
310315
[prepare_plugin(Plugin, ExpandDir) || Plugin <- ValidPlugins],
311316
Wanted.
312317

@@ -681,9 +686,11 @@ list_all_deps([Application | Applications], Deps) ->
681686
list_all_deps([], Deps) ->
682687
Deps.
683688

684-
remove_otp_overrideable_plugins(Plugins) ->
685-
lists:filter(fun(P) -> not plugin_provided_by_otp(P) end,
686-
Plugins).
689+
remove_plugins(Plugins) ->
690+
Fun = fun(P) ->
691+
not (is_plugin_provided_by_otp(P) orelse is_skipped_plugin(P))
692+
end,
693+
lists:filter(Fun, Plugins).
687694

688695
maybe_report_plugin_loading_problems([]) ->
689696
ok;

0 commit comments

Comments
 (0)