Skip to content

Commit

Permalink
Logging: Fix crash when Epoch-based timestamps are used with JSON
Browse files Browse the repository at this point in the history
The code was passing a number (the timestamp) to
unicode:characters_to_binary/1 which expects an iolist to convert to
UTF-8.

We now verify if we have a number before calling that function. If this
is a number (integer or float), we keep it as is because JSON supports
that type.
  • Loading branch information
dumbbell committed Aug 9, 2021
1 parent b6d83db commit 5131f9a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,14 @@ format_meta(Meta, Config) ->
maps:fold(
fun
(time, Timestamp, Acc) ->
FormattedTime = unicode:characters_to_binary(
rabbit_logger_fmt_helpers:format_time(
Timestamp, Config)),
Acc#{time => FormattedTime};
FormattedTime0 = rabbit_logger_fmt_helpers:format_time(
Timestamp, Config),
FormattedTime1 = case is_number(FormattedTime0) of
true -> FormattedTime0;
false -> unicode:characters_to_binary(
FormattedTime0)
end,
Acc#{time => FormattedTime1};
(domain = Key, Components, Acc) ->
Term = unicode:characters_to_binary(
string:join(
Expand Down
11 changes: 11 additions & 0 deletions deps/rabbit/test/logging_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
logging_as_multi_line_works/1,
formatting_as_json_configured_in_env_works/1,
formatting_as_json_configured_in_config_works/1,
formatting_as_json_using_epoch_secs_timestamps_works/1,
renaming_json_fields_works/1,
removing_specific_json_fields_works/1,
removing_non_mentionned_json_fields_works/1,
Expand Down Expand Up @@ -76,6 +77,7 @@ groups() ->
logging_as_multi_line_works,
formatting_as_json_configured_in_env_works,
formatting_as_json_configured_in_config_works,
formatting_as_json_using_epoch_secs_timestamps_works,
renaming_json_fields_works,
removing_specific_json_fields_works,
removing_non_mentionned_json_fields_works,
Expand Down Expand Up @@ -600,6 +602,15 @@ formatting_as_json_configured_in_config_works(Config) ->
[{persistent, true}]),
formatting_as_json_works(Config, Context).

formatting_as_json_using_epoch_secs_timestamps_works(Config) ->
Context = default_context(Config),
ok = application:set_env(
rabbit, log,
[{file, [{formatter, {rabbit_logger_json_fmt,
#{time_format => {epoch, secs, int}}}}]}],
[{persistent, true}]),
formatting_as_json_works(Config, Context).

formatting_as_json_works(_, Context) ->
rabbit_prelaunch_logging:clear_config_run_number(),
rabbit_prelaunch_logging:setup(Context),
Expand Down

0 comments on commit 5131f9a

Please sign in to comment.