Skip to content

Commit

Permalink
Merge pull request #3281 from rabbitmq/fix-epoch-timestamp-crash-in-j…
Browse files Browse the repository at this point in the history
…son-logging

Logging: Fix crash when Epoch-based timestamps are used with JSON
  • Loading branch information
dumbbell committed Aug 10, 2021
2 parents 5f7c79f + 6c8cf4c commit c2ea12b
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 c2ea12b

Please sign in to comment.