mirror of
https://github.com/valitydev/logger_logstash_formatter.git
synced 2024-11-06 10:35:23 +00:00
parent
013525713c
commit
498fda5fdd
8
Makefile
8
Makefile
@ -8,7 +8,8 @@ TEMPLATES_PATH = .
|
||||
|
||||
BUILD_IMAGE_TAG := cd38c35976f3684fe7552533b6175a4c3460e88b
|
||||
|
||||
CALL_ANYWHERE := all submodules rebar-update compile lint xref dialyze test clean distclean
|
||||
CALL_ANYWHERE := all submodules rebar-update compile lint xref dialyze \
|
||||
test clean distclean check_format format
|
||||
|
||||
CALL_W_CONTAINER := $(CALL_ANYWHERE)
|
||||
|
||||
@ -49,3 +50,8 @@ distclean:
|
||||
lint:
|
||||
elvis rock
|
||||
|
||||
check_format:
|
||||
$(REBAR) as test fmt -c
|
||||
|
||||
format:
|
||||
$(REBAR) fmt -w
|
||||
|
@ -1 +1 @@
|
||||
Subproject commit 540183862bc9fd04682e226de2056a320fd44be9
|
||||
Subproject commit f42e059d9ec93826ba4ad23232eed8ce67bd5486
|
@ -47,3 +47,11 @@
|
||||
nowarn_missing_spec
|
||||
]}.
|
||||
|
||||
{plugins, [
|
||||
{erlfmt, "0.8.0"}
|
||||
]}.
|
||||
|
||||
{erlfmt, [
|
||||
{print_width, 120},
|
||||
{files, "{src,include,test}/*.{hrl,erl}"}
|
||||
]}.
|
||||
|
@ -20,13 +20,14 @@
|
||||
-type log_level_map() :: #{logger:level() => atom()}.
|
||||
|
||||
-type config() :: #{
|
||||
exclude_meta_fields => [atom()] | exclude_all,
|
||||
log_level_map => log_level_map(),
|
||||
message_redaction_regex_list => list(),
|
||||
single_line => boolean(),
|
||||
chars_limit => pos_integer() | unlimited,
|
||||
depth => pos_integer() | unlimited
|
||||
exclude_meta_fields => [atom()] | exclude_all,
|
||||
log_level_map => log_level_map(),
|
||||
message_redaction_regex_list => list(),
|
||||
single_line => boolean(),
|
||||
chars_limit => pos_integer() | unlimited,
|
||||
depth => pos_integer() | unlimited
|
||||
}.
|
||||
|
||||
-type chars_limit() :: pos_integer() | unlimited.
|
||||
-type depth() :: pos_integer() | unlimited.
|
||||
|
||||
@ -34,21 +35,19 @@
|
||||
-define(MESSAGE_TRANSFORM_REGEXES_KEY, logger_logstash_formatter_message_transform_regexes).
|
||||
|
||||
-spec format(logger:log_event(), logger:formatter_config()) -> unicode:chardata().
|
||||
|
||||
format(#{msg := {report, _}} = Msg, Config) ->
|
||||
FormatterConfig = #{
|
||||
chars_limit => get_chars_limit(Config),
|
||||
depth => get_depth(Config),
|
||||
single_line => get_single_line(Config),
|
||||
template => [msg]
|
||||
chars_limit => get_chars_limit(Config),
|
||||
depth => get_depth(Config),
|
||||
single_line => get_single_line(Config),
|
||||
template => [msg]
|
||||
},
|
||||
Message = logger_formatter:format(Msg, FormatterConfig),
|
||||
format(Msg#{msg => {"~ts", [Message]}}, Config#{exclude_meta_fields => exclude_all});
|
||||
|
||||
format(Event, Config) ->
|
||||
{Meta, TimeStamp} = get_meta_and_timestamp(Event, Config),
|
||||
Severity = get_severity(Event, Config),
|
||||
Message = get_message(Event, Config),
|
||||
Severity = get_severity(Event, Config),
|
||||
Message = get_message(Event, Config),
|
||||
{RedactedMsg, RedactedMeta} = redact(Message, Meta, Config),
|
||||
PrintableMeta = meta_to_printable(RedactedMeta, Config),
|
||||
Formatted = create_message(RedactedMsg, Severity, TimeStamp, PrintableMeta),
|
||||
@ -66,13 +65,14 @@ format_time(USec) ->
|
||||
erlang:list_to_binary(Str).
|
||||
|
||||
get_meta_and_timestamp(#{meta := Meta0}, Config) ->
|
||||
{Meta, TimeStamp} = case get_timestamp(Meta0) of
|
||||
{error, _} ->
|
||||
TS = format_time(os:system_time(microsecond)),
|
||||
{Meta0, TS};
|
||||
TS ->
|
||||
% Succesfully got time from meta, can remove the field now
|
||||
{maps:remove(time, Meta0), TS}
|
||||
{Meta, TimeStamp} =
|
||||
case get_timestamp(Meta0) of
|
||||
{error, _} ->
|
||||
TS = format_time(os:system_time(microsecond)),
|
||||
{Meta0, TS};
|
||||
TS ->
|
||||
% Succesfully got time from meta, can remove the field now
|
||||
{maps:remove(time, Meta0), TS}
|
||||
end,
|
||||
{reduce_meta(Meta, Config), TimeStamp}.
|
||||
|
||||
@ -99,12 +99,12 @@ do_get_message({string, Message}, _Config) when is_list(Message) ->
|
||||
do_get_message({string, Message}, _Config) when is_binary(Message) ->
|
||||
Message;
|
||||
do_get_message({report, _Report}, _Config) ->
|
||||
erlang:throw({formatter_error, unexpected_report}); % there shouldn't be any reports here
|
||||
% there shouldn't be any reports here
|
||||
erlang:throw({formatter_error, unexpected_report});
|
||||
do_get_message({Format, Args}, Config) ->
|
||||
format_to_binary(Format, Args, Config).
|
||||
|
||||
-spec reformat(FormatList, config()) -> FormatList when
|
||||
FormatList :: [char() | io_lib:format_spec()].
|
||||
-spec reformat(FormatList, config()) -> FormatList when FormatList :: [char() | io_lib:format_spec()].
|
||||
reformat(FormatList, Config) ->
|
||||
try_reformat({get_single_line(Config), get_depth(Config)}, FormatList).
|
||||
|
||||
@ -113,8 +113,7 @@ try_reformat({false, unlimited}, FormatList) ->
|
||||
try_reformat(Flags, FormatList) ->
|
||||
lists:map(fun(C) -> do_reformat(Flags, C) end, FormatList).
|
||||
|
||||
-spec do_reformat({boolean(), depth()}, FormatItem) -> FormatItem when
|
||||
FormatItem :: char() | io_lib:format_spec().
|
||||
-spec do_reformat({boolean(), depth()}, FormatItem) -> FormatItem when FormatItem :: char() | io_lib:format_spec().
|
||||
do_reformat(Flags, Spec) ->
|
||||
Spec1 = apply_single_line_reformat(Flags, Spec),
|
||||
apply_depth_reformat(Flags, Spec1).
|
||||
@ -144,14 +143,14 @@ create_message(Message, Severity, TimeStamp, Meta) ->
|
||||
maps:merge(
|
||||
Meta,
|
||||
#{
|
||||
'@severity' => Severity,
|
||||
'@severity' => Severity,
|
||||
'@timestamp' => TimeStamp,
|
||||
message => Message
|
||||
message => Message
|
||||
}
|
||||
).
|
||||
|
||||
meta_to_printable(Meta, Config) ->
|
||||
maps:map(fun(K, V) -> printable(K, V, Config) end, Meta).
|
||||
maps:map(fun(K, V) -> printable(K, V, Config) end, Meta).
|
||||
|
||||
-spec format_to_binary(io:format(), [any()], config()) -> binary().
|
||||
format_to_binary(Format, Args, Config) ->
|
||||
@ -191,13 +190,12 @@ printable(_, {A, B, C} = V, Config) when not is_integer(A); not is_integer(B); n
|
||||
% jsx:is_term treats all 3 length tuples as timestamps and fails if they are actually not
|
||||
% so we filter tuples, that are definetly not timestamps
|
||||
format_to_binary("~p", [V], Config);
|
||||
|
||||
%% if a value is expressable in json use it directly, otherwise
|
||||
%% try to get a printable representation and express it as a json
|
||||
%% string
|
||||
printable(Key, Value, Config) when is_atom(Key); is_binary(Key) ->
|
||||
case jsx:is_term(Value) of
|
||||
true -> Value;
|
||||
true -> Value;
|
||||
false -> format_to_binary("~p", [Value], Config)
|
||||
end.
|
||||
|
||||
@ -210,7 +208,6 @@ redact(Message, Meta, Config) ->
|
||||
|
||||
redact_all(Message, []) ->
|
||||
Message;
|
||||
|
||||
redact_all(Message, Regexes) ->
|
||||
lists:foldl(fun redact_one/2, Message, Regexes).
|
||||
|
||||
@ -243,29 +240,24 @@ compile_regex(Regex) ->
|
||||
|
||||
traverse_and_redact(V, []) ->
|
||||
V;
|
||||
|
||||
traverse_and_redact(Map, Regexes) when is_map(Map)->
|
||||
F = fun (_, V) ->
|
||||
traverse_and_redact(Map, Regexes) when is_map(Map) ->
|
||||
F = fun(_, V) ->
|
||||
traverse_and_redact(V, Regexes)
|
||||
end,
|
||||
maps:map(F, Map);
|
||||
|
||||
traverse_and_redact(List, Regexes) when is_list(List) ->
|
||||
F = fun(V) ->
|
||||
traverse_and_redact(V, Regexes)
|
||||
end,
|
||||
lists:map(F, List);
|
||||
|
||||
traverse_and_redact(Tuple, Regexes) when is_tuple(Tuple) ->
|
||||
erlang:list_to_tuple(traverse_and_redact(erlang:tuple_to_list(Tuple), Regexes));
|
||||
|
||||
traverse_and_redact(Binary, Regexes) when is_binary(Binary) ->
|
||||
redact_all(Binary, Regexes);
|
||||
|
||||
traverse_and_redact(Item, _) ->
|
||||
Item.
|
||||
|
||||
-ifdef(TEST) .
|
||||
-ifdef(TEST).
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
|
||||
create_log_event(Level, Message, Meta) ->
|
||||
@ -283,27 +275,32 @@ get_time() ->
|
||||
USec = os:system_time(microsecond),
|
||||
{USec, format_time(USec)}.
|
||||
|
||||
-spec test() -> _. % auto generated eunit function
|
||||
% auto generated eunit function
|
||||
-spec test() -> _.
|
||||
|
||||
-spec basic_test_() -> _.
|
||||
|
||||
basic_test_() ->
|
||||
{USec, BinTime} = get_time(),
|
||||
Event0 = create_log_event(info, {string, "The simplest log ever"}, #{time => USec}),
|
||||
Event1 = create_log_event(info, {"The simplest ~p ever", ["log"]}, #{time => USec}),
|
||||
Event2 = create_log_event(info, {report, "The simplest log ever"}, #{time => USec}),
|
||||
[
|
||||
{"Basic log", ?_assertEqual(
|
||||
create_message(<<"The simplest log ever">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(Event0, #{}))
|
||||
)},
|
||||
{"Formated log", ?_assertEqual(
|
||||
create_message(<<"The simplest \"log\" ever">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(Event1, #{}))
|
||||
)},
|
||||
{"Report log", ?_assertEqual(
|
||||
create_message(<<"The simplest log ever">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(Event2, #{}))
|
||||
)}
|
||||
{"Basic log",
|
||||
?_assertEqual(
|
||||
create_message(<<"The simplest log ever">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(Event0, #{}))
|
||||
)},
|
||||
{"Formated log",
|
||||
?_assertEqual(
|
||||
create_message(<<"The simplest \"log\" ever">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(Event1, #{}))
|
||||
)},
|
||||
{"Report log",
|
||||
?_assertEqual(
|
||||
create_message(<<"The simplest log ever">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(Event2, #{}))
|
||||
)}
|
||||
].
|
||||
|
||||
-spec redact_test_() -> _.
|
||||
@ -321,24 +318,28 @@ redact_test_() ->
|
||||
}),
|
||||
Expected = create_message(<<"CVC: ***">>, <<"info">>, BinTime, #{}),
|
||||
[
|
||||
{"String redact", ?_assertEqual(
|
||||
Expected,
|
||||
parse_log_line(format(Event0, #{message_redaction_regex_list => [<<"424">>]}))
|
||||
)},
|
||||
{"Format redact", ?_assertEqual(
|
||||
Expected,
|
||||
parse_log_line(format(Event1, #{message_redaction_regex_list => [<<"424">>]})) % equals Event0 format
|
||||
)},
|
||||
{"Meta redact", ?_assertEqual(
|
||||
create_message(<<"No message">>, <<"info">>, BinTime, #{
|
||||
cvc => <<"***">>,
|
||||
cvc_list => [<<"434">>, <<"***">>],
|
||||
cvc_map => #{my_code => <<"***">>},
|
||||
deep_cvc => #{your_code => [<<"434">>, <<"***">>]},
|
||||
cvc_tuple => <<"{<<\"***\">>,gotcha}">>
|
||||
}),
|
||||
parse_log_line(format(Event2, #{message_redaction_regex_list => [<<"424">>]}))
|
||||
)}
|
||||
{"String redact",
|
||||
?_assertEqual(
|
||||
Expected,
|
||||
parse_log_line(format(Event0, #{message_redaction_regex_list => [<<"424">>]}))
|
||||
)},
|
||||
{"Format redact",
|
||||
?_assertEqual(
|
||||
Expected,
|
||||
% equals Event0 format
|
||||
parse_log_line(format(Event1, #{message_redaction_regex_list => [<<"424">>]}))
|
||||
)},
|
||||
{"Meta redact",
|
||||
?_assertEqual(
|
||||
create_message(<<"No message">>, <<"info">>, BinTime, #{
|
||||
cvc => <<"***">>,
|
||||
cvc_list => [<<"434">>, <<"***">>],
|
||||
cvc_map => #{my_code => <<"***">>},
|
||||
deep_cvc => #{your_code => [<<"434">>, <<"***">>]},
|
||||
cvc_tuple => <<"{<<\"***\">>,gotcha}">>
|
||||
}),
|
||||
parse_log_line(format(Event2, #{message_redaction_regex_list => [<<"424">>]}))
|
||||
)}
|
||||
].
|
||||
|
||||
-spec excludes_test_() -> _.
|
||||
@ -352,26 +353,30 @@ excludes_test_() ->
|
||||
answer => 42
|
||||
}),
|
||||
[
|
||||
{"Default excludes", ?_assertEqual(
|
||||
create_message(<<"Excludes">>, <<"info">>, BinTime, #{answer => 42}),
|
||||
parse_log_line(format(Event, #{}))
|
||||
)},
|
||||
{"No excludes", ?_assertEqual(
|
||||
create_message(<<"Excludes">>, <<"info">>, BinTime, #{
|
||||
gl => BinPid,
|
||||
domain => [<<"rbkmoney">>],
|
||||
answer => 42
|
||||
}),
|
||||
parse_log_line(format(Event, #{exclude_meta_fields => []}))
|
||||
)},
|
||||
{"Custom excludes", ?_assertEqual(
|
||||
create_message(<<"Excludes">>, <<"info">>, BinTime, #{gl => BinPid, domain => [<<"rbkmoney">>]}),
|
||||
parse_log_line(format(Event, #{exclude_meta_fields => [answer]}))
|
||||
)},
|
||||
{"Exlude all", ?_assertEqual(
|
||||
create_message(<<"Excludes">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(Event, #{exclude_meta_fields => exclude_all}))
|
||||
)}
|
||||
{"Default excludes",
|
||||
?_assertEqual(
|
||||
create_message(<<"Excludes">>, <<"info">>, BinTime, #{answer => 42}),
|
||||
parse_log_line(format(Event, #{}))
|
||||
)},
|
||||
{"No excludes",
|
||||
?_assertEqual(
|
||||
create_message(<<"Excludes">>, <<"info">>, BinTime, #{
|
||||
gl => BinPid,
|
||||
domain => [<<"rbkmoney">>],
|
||||
answer => 42
|
||||
}),
|
||||
parse_log_line(format(Event, #{exclude_meta_fields => []}))
|
||||
)},
|
||||
{"Custom excludes",
|
||||
?_assertEqual(
|
||||
create_message(<<"Excludes">>, <<"info">>, BinTime, #{gl => BinPid, domain => [<<"rbkmoney">>]}),
|
||||
parse_log_line(format(Event, #{exclude_meta_fields => [answer]}))
|
||||
)},
|
||||
{"Exlude all",
|
||||
?_assertEqual(
|
||||
create_message(<<"Excludes">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(Event, #{exclude_meta_fields => exclude_all}))
|
||||
)}
|
||||
].
|
||||
|
||||
-spec level_mapping_test_() -> _.
|
||||
@ -379,10 +384,11 @@ level_mapping_test_() ->
|
||||
{USec, BinTime} = get_time(),
|
||||
Event = create_log_event(info, {string, "Mapping"}, #{time => USec}),
|
||||
[
|
||||
{"Level mapping", ?_assertEqual(
|
||||
create_message(<<"Mapping">>, <<"INFORMATION">>, BinTime, #{}),
|
||||
parse_log_line(format(Event, #{log_level_map => #{info => 'INFORMATION'}}))
|
||||
)}
|
||||
{"Level mapping",
|
||||
?_assertEqual(
|
||||
create_message(<<"Mapping">>, <<"INFORMATION">>, BinTime, #{}),
|
||||
parse_log_line(format(Event, #{log_level_map => #{info => 'INFORMATION'}}))
|
||||
)}
|
||||
].
|
||||
|
||||
-spec line_break_exists_and_single_test() -> _.
|
||||
@ -396,22 +402,26 @@ single_line_reformat_test_() ->
|
||||
ComplexpEvent = create_log_event(info, {"Complex ~1p", [[1, 2, 3]]}, #{time => USec}),
|
||||
ComplexPEvent = create_log_event(info, {"Complex ~1P", [[1, 2, 3], 100]}, #{time => USec}),
|
||||
[
|
||||
{"Single line p", ?_assertEqual(
|
||||
create_message(<<"Complex [1,2,3]">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(ComplexpEvent, #{single_line => true}))
|
||||
)},
|
||||
{"Single line P", ?_assertEqual(
|
||||
create_message(<<"Complex [1,2,3]">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(ComplexPEvent, #{single_line => true}))
|
||||
)},
|
||||
{"Multiple lines p", ?_assertEqual(
|
||||
create_message(<<"Complex [1,\n 2,\n 3]">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(ComplexpEvent, #{single_line => false}))
|
||||
)},
|
||||
{"Multiple lines P", ?_assertEqual(
|
||||
create_message(<<"Complex [1,\n 2,\n 3]">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(ComplexPEvent, #{single_line => false}))
|
||||
)}
|
||||
{"Single line p",
|
||||
?_assertEqual(
|
||||
create_message(<<"Complex [1,2,3]">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(ComplexpEvent, #{single_line => true}))
|
||||
)},
|
||||
{"Single line P",
|
||||
?_assertEqual(
|
||||
create_message(<<"Complex [1,2,3]">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(ComplexPEvent, #{single_line => true}))
|
||||
)},
|
||||
{"Multiple lines p",
|
||||
?_assertEqual(
|
||||
create_message(<<"Complex [1,\n 2,\n 3]">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(ComplexpEvent, #{single_line => false}))
|
||||
)},
|
||||
{"Multiple lines P",
|
||||
?_assertEqual(
|
||||
create_message(<<"Complex [1,\n 2,\n 3]">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(ComplexPEvent, #{single_line => false}))
|
||||
)}
|
||||
].
|
||||
|
||||
-spec depth_reformat_test_() -> _.
|
||||
@ -434,83 +444,101 @@ depth_reformat_test_() ->
|
||||
]}
|
||||
],
|
||||
[
|
||||
{"Unlimited p", ?_assertEqual(
|
||||
create_message(<<"[1,2,3]">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(ComplexpEvent, #{depth => unlimited}))
|
||||
)},
|
||||
{"Unlimited P", ?_assertEqual(
|
||||
create_message(<<"[1,2,3]">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(ComplexPEvent, #{depth => unlimited}))
|
||||
)},
|
||||
{"Unlimited w", ?_assertEqual(
|
||||
create_message(<<"[1,2,3]">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(ComplexwEvent, #{depth => unlimited}))
|
||||
)},
|
||||
{"Unlimited W", ?_assertEqual(
|
||||
create_message(<<"[1,2,3]">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(ComplexWEvent, #{depth => unlimited}))
|
||||
)},
|
||||
{"Unlimited small P", ?_assertEqual(
|
||||
create_message(<<"[1|...]">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(ComplexSmallPEvent, #{depth => unlimited}))
|
||||
)},
|
||||
{"Unlimited small W", ?_assertEqual(
|
||||
create_message(<<"[1|...]">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(ComplexSmallWEvent, #{depth => unlimited}))
|
||||
)},
|
||||
{"Limited p", ?_assertEqual(
|
||||
create_message(<<"[...]">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(ComplexpEvent, #{depth => 1}))
|
||||
)},
|
||||
{"Limited P", ?_assertEqual(
|
||||
create_message(<<"[...]">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(ComplexPEvent, #{depth => 1}))
|
||||
)},
|
||||
{"Limited w", ?_assertEqual(
|
||||
create_message(<<"[...]">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(ComplexwEvent, #{depth => 1}))
|
||||
)},
|
||||
{"Limited W", ?_assertEqual(
|
||||
create_message(<<"[...]">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(ComplexWEvent, #{depth => 1}))
|
||||
)},
|
||||
{"Limited small P", ?_assertEqual(
|
||||
create_message(<<"[1|...]">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(ComplexSmallPEvent, #{depth => 3}))
|
||||
)},
|
||||
{"Limited small W", ?_assertEqual(
|
||||
create_message(<<"[1|...]">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(ComplexSmallWEvent, #{depth => 3}))
|
||||
)},
|
||||
{"Limited very small P", ?_assertEqual(
|
||||
create_message(<<"[...]">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(ComplexSmallPEvent, #{depth => 1}))
|
||||
)},
|
||||
{"Limited very small W", ?_assertEqual(
|
||||
create_message(<<"[...]">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(ComplexSmallWEvent, #{depth => 1}))
|
||||
)},
|
||||
{"Limited report", ?_assertEqual(
|
||||
create_message(
|
||||
<<
|
||||
"supervisor: well_some_sup, started: [{id,", ?MODULE_STRING, "},",
|
||||
"{restart_type,permanent},{pid,...},{...}|...]"
|
||||
>>,
|
||||
<<"info">>,
|
||||
BinTime,
|
||||
#{}
|
||||
),
|
||||
parse_log_line(format(
|
||||
create_log_event(info, {report, Report}, #{time => USec}),
|
||||
% NOTE
|
||||
% Standard logger formatter never allow depth to go below 5
|
||||
% > https://github.com/erlang/otp/blob/a4ff9f3/lib/kernel/src/logger_formatter.erl#L363
|
||||
#{chars_limit => 120, depth => 5}
|
||||
))
|
||||
)}
|
||||
{"Unlimited p",
|
||||
?_assertEqual(
|
||||
create_message(<<"[1,2,3]">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(ComplexpEvent, #{depth => unlimited}))
|
||||
)},
|
||||
{"Unlimited P",
|
||||
?_assertEqual(
|
||||
create_message(<<"[1,2,3]">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(ComplexPEvent, #{depth => unlimited}))
|
||||
)},
|
||||
{"Unlimited w",
|
||||
?_assertEqual(
|
||||
create_message(<<"[1,2,3]">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(ComplexwEvent, #{depth => unlimited}))
|
||||
)},
|
||||
{"Unlimited W",
|
||||
?_assertEqual(
|
||||
create_message(<<"[1,2,3]">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(ComplexWEvent, #{depth => unlimited}))
|
||||
)},
|
||||
{"Unlimited small P",
|
||||
?_assertEqual(
|
||||
create_message(<<"[1|...]">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(ComplexSmallPEvent, #{depth => unlimited}))
|
||||
)},
|
||||
{"Unlimited small W",
|
||||
?_assertEqual(
|
||||
create_message(<<"[1|...]">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(ComplexSmallWEvent, #{depth => unlimited}))
|
||||
)},
|
||||
{"Limited p",
|
||||
?_assertEqual(
|
||||
create_message(<<"[...]">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(ComplexpEvent, #{depth => 1}))
|
||||
)},
|
||||
{"Limited P",
|
||||
?_assertEqual(
|
||||
create_message(<<"[...]">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(ComplexPEvent, #{depth => 1}))
|
||||
)},
|
||||
{"Limited w",
|
||||
?_assertEqual(
|
||||
create_message(<<"[...]">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(ComplexwEvent, #{depth => 1}))
|
||||
)},
|
||||
{"Limited W",
|
||||
?_assertEqual(
|
||||
create_message(<<"[...]">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(ComplexWEvent, #{depth => 1}))
|
||||
)},
|
||||
{"Limited small P",
|
||||
?_assertEqual(
|
||||
create_message(<<"[1|...]">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(ComplexSmallPEvent, #{depth => 3}))
|
||||
)},
|
||||
{"Limited small W",
|
||||
?_assertEqual(
|
||||
create_message(<<"[1|...]">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(ComplexSmallWEvent, #{depth => 3}))
|
||||
)},
|
||||
{"Limited very small P",
|
||||
?_assertEqual(
|
||||
create_message(<<"[...]">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(ComplexSmallPEvent, #{depth => 1}))
|
||||
)},
|
||||
{"Limited very small W",
|
||||
?_assertEqual(
|
||||
create_message(<<"[...]">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(ComplexSmallWEvent, #{depth => 1}))
|
||||
)},
|
||||
{"Limited report",
|
||||
?_assertEqual(
|
||||
create_message(
|
||||
<<
|
||||
"supervisor: well_some_sup, started: [{id,",
|
||||
?MODULE_STRING,
|
||||
"},",
|
||||
"{restart_type,permanent},{pid,...},{...}|...]"
|
||||
>>,
|
||||
<<"info">>,
|
||||
BinTime,
|
||||
#{}
|
||||
),
|
||||
parse_log_line(
|
||||
format(
|
||||
create_log_event(info, {report, Report}, #{time => USec}),
|
||||
% NOTE
|
||||
% Standard logger formatter never allow depth to go below 5
|
||||
% > https://github.com/erlang/otp/blob/a4ff9f3/lib/kernel/src/logger_formatter.erl#L363
|
||||
#{chars_limit => 120, depth => 5}
|
||||
)
|
||||
)
|
||||
)}
|
||||
].
|
||||
|
||||
|
||||
-spec chars_limit_test_() -> _.
|
||||
chars_limit_test_() ->
|
||||
{USec, BinTime} = get_time(),
|
||||
@ -526,21 +554,26 @@ chars_limit_test_() ->
|
||||
]}
|
||||
],
|
||||
[
|
||||
{"Unlimited", ?_assertEqual(
|
||||
create_message(<<"[1,2,3]">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(Event, #{chars_limit => unlimited}))
|
||||
)},
|
||||
{"Limited", ?_assertEqual(
|
||||
create_message(<<"[...]">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(Event, #{chars_limit => 1}))
|
||||
)},
|
||||
{"Limited report", ?_assertEqual(
|
||||
create_message(<<"supervisor: well_some_sup, started: ...">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(
|
||||
create_log_event(info, {report, Report}, #{time => USec}),
|
||||
#{chars_limit => 40}
|
||||
))
|
||||
)}
|
||||
{"Unlimited",
|
||||
?_assertEqual(
|
||||
create_message(<<"[1,2,3]">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(Event, #{chars_limit => unlimited}))
|
||||
)},
|
||||
{"Limited",
|
||||
?_assertEqual(
|
||||
create_message(<<"[...]">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(Event, #{chars_limit => 1}))
|
||||
)},
|
||||
{"Limited report",
|
||||
?_assertEqual(
|
||||
create_message(<<"supervisor: well_some_sup, started: ...">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(
|
||||
format(
|
||||
create_log_event(info, {report, Report}, #{time => USec}),
|
||||
#{chars_limit => 40}
|
||||
)
|
||||
)
|
||||
)}
|
||||
].
|
||||
|
||||
-spec various_format_types_test_() -> _.
|
||||
@ -553,20 +586,26 @@ various_format_types_test_() ->
|
||||
],
|
||||
[
|
||||
[
|
||||
{"Without reformat", ?_assertEqual(
|
||||
create_message(<<"Just atom">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(
|
||||
create_log_event(info, {F, [atom]}, #{time => USec}),
|
||||
#{single_line_message => false}
|
||||
))
|
||||
)},
|
||||
{"With reformat", ?_assertEqual(
|
||||
create_message(<<"Just atom">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(format(
|
||||
create_log_event(info, {F, [atom]}, #{time => USec}),
|
||||
#{single_line_message => true}
|
||||
))
|
||||
)}
|
||||
{"Without reformat",
|
||||
?_assertEqual(
|
||||
create_message(<<"Just atom">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(
|
||||
format(
|
||||
create_log_event(info, {F, [atom]}, #{time => USec}),
|
||||
#{single_line_message => false}
|
||||
)
|
||||
)
|
||||
)},
|
||||
{"With reformat",
|
||||
?_assertEqual(
|
||||
create_message(<<"Just atom">>, <<"info">>, BinTime, #{}),
|
||||
parse_log_line(
|
||||
format(
|
||||
create_log_event(info, {F, [atom]}, #{time => USec}),
|
||||
#{single_line_message => true}
|
||||
)
|
||||
)
|
||||
)}
|
||||
]
|
||||
|| F <- ValidIOFormats
|
||||
].
|
||||
@ -574,7 +613,9 @@ various_format_types_test_() ->
|
||||
-spec timestamp_format_simple_test() -> _.
|
||||
timestamp_format_simple_test() ->
|
||||
Event = create_log_event(info, {string, "time format"}, #{time => 1581958959142512}),
|
||||
[<<"{\"@severity\":\"info\",\"@timestamp\":\"2020-02-17T17:02:39.142512Z\",\"message\":\"time format\"}">>,
|
||||
<<"\n">>] = format(Event, #{}).
|
||||
[
|
||||
<<"{\"@severity\":\"info\",\"@timestamp\":\"2020-02-17T17:02:39.142512Z\",\"message\":\"time format\"}">>,
|
||||
<<"\n">>
|
||||
] = format(Event, #{}).
|
||||
|
||||
-endif.
|
||||
|
Loading…
Reference in New Issue
Block a user