Skip to content

Logging: Add journald support #2940

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion deps/rabbit/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,15 @@ APPS_DIR := $(CURDIR)/apps
LOCAL_DEPS = sasl rabbitmq_prelaunch os_mon inets compiler public_key crypto ssl syntax_tools xmerl

BUILD_DEPS = rabbitmq_cli
DEPS = ranch rabbit_common ra sysmon_handler stdout_formatter recon observer_cli osiris amqp10_common syslog
DEPS = ranch rabbit_common ra sysmon_handler stdout_formatter recon observer_cli osiris amqp10_common syslog systemd
TEST_DEPS = rabbitmq_ct_helpers rabbitmq_ct_client_helpers amqp_client meck proper

PLT_APPS += mnesia

dep_syslog = git https://github.com/schlagert/syslog 4.0.0
dep_osiris = git https://github.com/rabbitmq/osiris master
# TODO: Use systemd from Hex.pm, once there is a new post-0.6.0 release.
dep_systemd = git https://github.com/hauleth/erlang-systemd 0ce748edffcb72bb028733e9ca4707cb30add853

define usage_xml_to_erl
$(subst __,_,$(patsubst $(DOCS_DIR)/rabbitmq%.1.xml, src/rabbit_%_usage.erl, $(subst -,_,$(1))))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
default_formatter/1,
default_console_formatter/1,
default_file_formatter/1,
default_journald_formatter/1,
default_syslog_formatter/1,
enable_quick_dbg/1,
use_colored_logging/0,
use_colored_logging/1,
translate_formatter_conf/2]).
translate_formatter_conf/2,
translate_journald_fields_conf/2]).
-export([filter_log_event/2]).

-ifdef(TEST).
Expand Down Expand Up @@ -121,6 +123,10 @@ default_console_formatter(Context) ->
default_file_formatter(Context) ->
default_formatter(Context#{output_supports_colors => false}).

default_journald_formatter(_Context) ->
{rabbit_logger_text_fmt, #{prefix_format => [],
use_colors => false}}.

default_syslog_formatter(Context) ->
{Module, Config} = default_file_formatter(Context),
case Module of
Expand Down Expand Up @@ -155,8 +161,8 @@ enable_quick_dbg(#{dbg_output := Output, dbg_mods := Mods}) ->
{rabbit_logger_text_fmt, formatter_plaintext_conf()} |
{rabbit_logger_json_fmt, formatter_json_conf()}.
%% @doc
%% Called from the Cuttlefish schema to derive the actual configuration from
%% several Cuttlefish variables.
%% Called from the Cuttlefish schema to derive the actual formatter
%% configuration from several Cuttlefish variables.

translate_formatter_conf(Var, Conf) when is_list(Var) ->
try
Expand Down Expand Up @@ -404,7 +410,7 @@ translate_json_formatter_conf(Var, Conf, GenericConfig) ->

-spec parse_json_field_mapping(string()) -> json_field_map().
%% @doc
%% Parses the field_map pattern.
%% Parses the JSON formatter field_map pattern.
%%
%% The pattern is of the form: `time:ts level msg *:-'.
%%
Expand Down Expand Up @@ -496,5 +502,73 @@ parse_json_verbosity_mapping([], #{'$REST' := Default} = Mapping) ->
parse_json_verbosity_mapping([], Mapping) ->
Mapping.

-spec translate_journald_fields_conf(string(), cuttlefish_conf:conf()) ->
proplists:proplist().
%% @doc
%% Called from the Cuttlefish schema to create the actual journald handler
%% configuration.

translate_journald_fields_conf(Var, Conf) when is_list(Var) ->
try
RawFieldMapping = cuttlefish:conf_get(Var, Conf),
parse_journald_field_mapping(RawFieldMapping)
catch
Class:Reason:Stacktrace ->
?LOG_ERROR(
rabbit_prelaunch_errors:format_exception(
Class, Reason, Stacktrace),
#{domain => ?RMQLOG_DOMAIN_PRELAUNCH}),
throw({configuration_translation_failure, Reason})
end.

-spec parse_journald_field_mapping(string()) ->
[atom() | {atom(), atom()}].
%% @doc
%% Parses the journald fields pattern.
%%
%% The pattern is of the form: `SYSLOG_IDENTIFIER="rabbitmq-server" pid
%% CODE_FILE=file'.
%%
%% `SYSLOG_IDENTIFIER="rabbitmq"' means the `SYSLOG_IDENTIFIER' field should
%% be set to the string `rabbitmq-server'.
%%
%% `pid' means that field should be kept as-is.
%%
%% `CODE_FILE=file' means the `CODE_FILE' field should be set to the value of
%% the `pid' field.

parse_journald_field_mapping(RawMapping) ->
parse_journald_field_mapping(string:split(RawMapping, " ", all), []).

parse_journald_field_mapping([Entry | Rest], Mapping) ->
Mapping1 = case string:split(Entry, "=", leading) of
[[$_ | _], _] ->
throw({bad_journald_mapping,
leading_underscore_forbidden,
Entry});
[Name, Value] ->
case re:run(Name, "^[A-Z0-9_]+$", [{capture, none}]) of
match ->
ReOpts = [{capture, all_but_first, list}],
case re:run(Value, "^\"(.+)\"$", ReOpts) of
{match, [Data]} ->
[{Name, Data} | Mapping];
nomatch ->
Field = list_to_atom(Value),
[{Name, Field} | Mapping]
end;
nomatch ->
throw({bad_journald_mapping,
name_with_invalid_characters,
Entry})
end;
[FieldS] ->
Field = list_to_atom(FieldS),
[Field | Mapping]
end,
parse_journald_field_mapping(Rest, Mapping1);
parse_journald_field_mapping([], Mapping) ->
lists:reverse(Mapping).

levels() ->
[debug, info, notice, warning, error, critical, alert, emergency].
15 changes: 15 additions & 0 deletions deps/rabbit/priv/schema/rabbit.schema
Original file line number Diff line number Diff line change
Expand Up @@ -1304,6 +1304,21 @@ end}.
rabbit_prelaunch_early_logging:translate_formatter_conf("log.exchange.formatter", Conf)
end}.

{mapping, "log.journald", "rabbit.log.journald.enabled", [
{datatype, {enum, [true, false]}}
]}.
{mapping, "log.journald.level", "rabbit.log.journald.level", [
{datatype, {enum, [debug, info, notice, warning, error, critical, alert, emergency, none]}}
]}.
{mapping, "log.journald.fields", "rabbit.log.journald.fields", [
{default, "SYSLOG_IDENTIFIER=\"rabbitmq-server\" syslog_timestamp syslog_pid priority ERL_PID=pid CODE_FILE=file CODE_LINE=line CODE_MFA=mfa"},
{datatype, string}
]}.
{translation, "rabbit.log.journald.fields",
fun(Conf) ->
rabbit_prelaunch_early_logging:translate_journald_fields_conf("log.journald.fields", Conf)
end}.

{mapping, "log.syslog", "rabbit.log.syslog.enabled", [
{datatype, {enum, [true, false]}}
]}.
Expand Down
Loading