Skip to content

Commit d97fbe9

Browse files
Merge pull request #12158 from rabbitmq/mergify/bp/v3.13.x/pr-12157
Support unicode messages by exchange logging (backport #12153) (backport #12157)
2 parents 5f4758a + bd6e832 commit d97fbe9

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

deps/rabbit/src/rabbit_logger_exchange_h.erl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,18 @@ make_headers(_, _) ->
112112
[{<<"node">>, longstr, Node}].
113113

114114
try_format_body(LogEvent, #{formatter := {Formatter, FormatterConfig}}) ->
115-
Formatted = try_format_body(LogEvent, Formatter, FormatterConfig),
116-
erlang:iolist_to_binary(Formatted).
115+
try_format_body(LogEvent, Formatter, FormatterConfig).
117116

118117
try_format_body(LogEvent, Formatter, FormatterConfig) ->
119118
try
120-
Formatter:format(LogEvent, FormatterConfig)
119+
Formatted = Formatter:format(LogEvent, FormatterConfig),
120+
case unicode:characters_to_binary(Formatted) of
121+
Binary when is_binary(Binary) ->
122+
Binary;
123+
Error ->
124+
%% The formatter returned invalid or incomplete unicode
125+
throw(Error)
126+
end
121127
catch
122128
C:R:S ->
123129
case {?DEFAULT_FORMATTER, ?DEFAULT_FORMATTER_CONFIG} of

deps/rabbit/test/logging_SUITE.erl

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,11 @@ logging_to_exchange_works(Config) ->
10291029
#{domain => ?RMQLOG_DOMAIN_UPGRADE}, Config1),
10301030
rabbit_ct_helpers:await_condition(ContainsLogEntry4, 30_000),
10311031

1032+
ContainsLogEntryUnicode =
1033+
ping_log(rmq_1_exchange, info, "unicode 257 is ā",
1034+
#{domain => ?RMQLOG_DOMAIN_UPGRADE}, Config1),
1035+
rabbit_ct_helpers:await_condition(ContainsLogEntryUnicode, 30_000),
1036+
10321037
%% increase log level
10331038
ok = rabbit_ct_broker_helpers:rpc(
10341039
Config, 0,
@@ -1179,14 +1184,17 @@ ping_log(Id, Level, Metadata, Config) ->
11791184
32,
11801185
"abcdefghijklmnopqrstuvwxyz"
11811186
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"),
1182-
ct:log("Logging \"~ts\" at level ~ts (~tp)", [RandomMsg, Level, Metadata]),
1187+
ping_log(Id, Level, RandomMsg, Metadata, Config).
1188+
1189+
ping_log(Id, Level, Msg, Metadata, Config) ->
1190+
ct:log("Logging \"~ts\" at level ~ts (~tp)", [Msg, Level, Metadata]),
11831191
case need_rpc(Config) of
1184-
false -> logger:log(Level, RandomMsg, Metadata);
1192+
false -> logger:log(Level, Msg, Metadata);
11851193
true -> rabbit_ct_broker_helpers:rpc(
11861194
Config, 0,
1187-
logger, log, [Level, RandomMsg, Metadata])
1195+
logger, log, [Level, Msg, Metadata])
11881196
end,
1189-
check_log(Id, Level, RandomMsg, Config).
1197+
check_log(Id, Level, Msg, Config).
11901198

11911199
need_rpc(Config) ->
11921200
rabbit_ct_helpers:get_config(
@@ -1216,7 +1224,7 @@ check_log1(#{id := Id,
12161224
end,
12171225
fun() ->
12181226
{ok, Content} = file:read_file(Filename),
1219-
ReOpts = [{capture, none}, multiline],
1227+
ReOpts = [{capture, none}, multiline, unicode],
12201228
match =:= re:run(Content, RandomMsg ++ "$", ReOpts)
12211229
end;
12221230
check_log1(#{module := Mod,
@@ -1227,7 +1235,7 @@ check_log1(#{module := Mod,
12271235
when ?IS_STD_H_COMPAT(Mod) andalso ?IS_STDDEV(Stddev) ->
12281236
Filename = html_report_filename(Config),
12291237
{ColorStart, ColorEnd} = get_color_config(Handler, Level),
1230-
ReOpts = [{capture, none}, multiline],
1238+
ReOpts = [{capture, none}, multiline, unicode],
12311239
fun() ->
12321240
{ok, Content} = file:read_file(Filename),
12331241
Regex =
@@ -1239,7 +1247,7 @@ check_log1(#{module := rabbit_logger_exchange_h},
12391247
RandomMsg,
12401248
Config) ->
12411249
{Chan, QName} = ?config(test_channel_and_queue, Config),
1242-
ReOpts = [{capture, none}, multiline],
1250+
ReOpts = [{capture, none}, multiline, unicode],
12431251
fun() ->
12441252
Ret = amqp_channel:call(
12451253
Chan, #'basic.get'{queue = QName, no_ack = false}),
@@ -1257,7 +1265,7 @@ check_log1(#{module := syslog_logger_h},
12571265
_Level,
12581266
RandomMsg,
12591267
Config) ->
1260-
ReOpts = [{capture, none}, multiline],
1268+
ReOpts = [{capture, none}, multiline, unicode],
12611269
fun() ->
12621270
Buffer = get_syslogd_messages(Config),
12631271
match =:= re:run(Buffer, RandomMsg ++ "$", ReOpts)

0 commit comments

Comments
 (0)