Skip to content

Commit 571b975

Browse files
committed
Logging: Allow to set timezone in rfc3339- and format-string-based time formats
This is not exposed to the end user (yet) through the Cuttlefish configuration. But this is required to make logging_SUITE timezone agnostic (i.e. the timezone of the host running the testsuite should not affect the formatted times).
1 parent e128c6f commit 571b975

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

deps/rabbit/apps/rabbitmq_prelaunch/src/rabbit_logger_fmt_helpers.erl

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@
1414
format_time(Timestamp, #{time_format := Format}) ->
1515
format_time1(Timestamp, Format);
1616
format_time(Timestamp, _) ->
17-
format_time1(Timestamp, {rfc3339, $\s}).
17+
format_time1(Timestamp, {rfc3339, $\s, ""}).
1818

19-
format_time1(Timestamp, {rfc3339, Sep}) ->
19+
format_time1(Timestamp, {rfc3339, Sep, Offset}) ->
2020
Options = [{unit, microsecond},
21+
{offset, Offset},
2122
{time_designator, Sep}],
2223
calendar:system_time_to_rfc3339(Timestamp, Options);
2324
format_time1(Timestamp, {epoch, secs, int}) ->
@@ -28,12 +29,18 @@ format_time1(Timestamp, {epoch, secs, binary}) ->
2829
io_lib:format("~.6.0f", [Timestamp / 1000000]);
2930
format_time1(Timestamp, {epoch, usecs, binary}) ->
3031
io_lib:format("~b", [Timestamp]);
31-
format_time1(Timestamp, {Format, Args}) ->
32+
format_time1(Timestamp, {LocalOrUniversal, Format, Args}) ->
3233
%% The format string and the args list is prepared by
3334
%% `rabbit_prelaunch_early_logging:translate_generic_conf()'.
3435
{{Year, Month, Day},
35-
{Hour, Minute, Second}} = calendar:system_time_to_local_time(
36-
Timestamp, microsecond),
36+
{Hour, Minute, Second}} = case LocalOrUniversal of
37+
local ->
38+
calendar:system_time_to_local_time(
39+
Timestamp, microsecond);
40+
universal ->
41+
calendar:system_time_to_universal_time(
42+
Timestamp, microsecond)
43+
end,
3744
Args1 = lists:map(
3845
fun
3946
(year) -> Year;

deps/rabbit/apps/rabbitmq_prelaunch/src/rabbit_prelaunch_early_logging.erl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,11 @@ translate_formatter_conf(Var, Conf) when is_list(Var) ->
181181
-type time_format_string_arg() :: year | month | day |
182182
hour | minute | second |
183183
{second_fractional, non_neg_integer()}.
184-
-type time_format() :: {rfc3339, char()} |
184+
-type time_format() :: {rfc3339, char(), string() | integer()} |
185185
{epoch, secs | usecs, binary | int} |
186-
{string(), [time_format_string_arg()]}.
186+
{local | universal,
187+
string(),
188+
[time_format_string_arg()]}.
187189
-type level_format() :: lc | uc | lc3 | uc3 | lc4 | uc4.
188190
-type formatter_generic_conf() :: #{time_format := time_format(),
189191
level_format := level_format(),
@@ -201,9 +203,9 @@ translate_generic_conf(Var, Conf) ->
201203
Formatter = cuttlefish:conf_get(Var, Conf),
202204
TimeFormat = case cuttlefish:conf_get(Var ++ ".time_format", Conf) of
203205
rfc3339_T ->
204-
{rfc3339, $T};
206+
{rfc3339, $T, ""};
205207
rfc3339_space ->
206-
{rfc3339, $\s};
208+
{rfc3339, $\s, ""};
207209
epoch_secs when Formatter =:= json ->
208210
{epoch, secs, int};
209211
epoch_usecs when Formatter =:= json ->
@@ -213,7 +215,8 @@ translate_generic_conf(Var, Conf) ->
213215
epoch_usecs ->
214216
{epoch, usecs, binary};
215217
lager_default ->
216-
{"~4..0b-~2..0b-~2..0b "
218+
{local,
219+
"~4..0b-~2..0b-~2..0b "
217220
"~2..0b:~2..0b:~2..0b.~3..0b",
218221
[year, month, day,
219222
hour, minute, second,

deps/rabbit/test/logging_SUITE.erl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -495,19 +495,20 @@ setting_level_format_works(LevelFormat, LevelName, Config) ->
495495
Line).
496496

497497
setting_time_format_works(Config) ->
498-
DateTime = "2018-02-01T16:17:58.123456+01:00",
498+
DateTime = "2018-05-01T16:17:58.123456+01:00",
499499
Timestamp = calendar:rfc3339_to_system_time(
500500
DateTime, [{unit, microsecond}]),
501501
TimeFormats =
502-
#{{rfc3339, $T} => DateTime,
503-
{rfc3339, $\s} => "2018-02-01 16:17:58.123456+01:00",
502+
#{{rfc3339, $T, "+01:00"} => DateTime,
503+
{rfc3339, $\s, "+01:00"} => "2018-05-01 16:17:58.123456+01:00",
504504
{epoch, usecs, binary} => integer_to_list(Timestamp),
505505
{epoch, secs, binary} => io_lib:format("~.6.0f", [Timestamp / 1000000]),
506-
{"~4..0b-~2..0b-~2..0b "
506+
{universal,
507+
"~4..0b-~2..0b-~2..0b "
507508
"~2..0b:~2..0b:~2..0b.~3..0b",
508509
[year, month, day,
509510
hour, minute, second,
510-
{second_fractional, 3}]} => "2018-02-01 16:17:58.123"},
511+
{second_fractional, 3}]} => "2018-05-01 15:17:58.123"},
511512
maps:fold(
512513
fun(TimeFormat, TimeValue, Acc) ->
513514
remove_all_handlers(),

0 commit comments

Comments
 (0)