fix(logger): json format log encode binary list as string array
This commit is contained in:
parent
6f7a4344dc
commit
afdda107af
|
@ -235,12 +235,18 @@ json(B, Config) when is_binary(B) ->
|
||||||
json(M, Config) when is_list(M), is_tuple(hd(M)), tuple_size(hd(M)) =:= 2 ->
|
json(M, Config) when is_list(M), is_tuple(hd(M)), tuple_size(hd(M)) =:= 2 ->
|
||||||
best_effort_json_obj(M, Config);
|
best_effort_json_obj(M, Config);
|
||||||
json(L, Config) when is_list(L) ->
|
json(L, Config) when is_list(L) ->
|
||||||
|
case lists:all(fun erlang:is_binary/1, L) of
|
||||||
|
true ->
|
||||||
|
%% string array
|
||||||
|
L;
|
||||||
|
false ->
|
||||||
try unicode:characters_to_binary(L, utf8) of
|
try unicode:characters_to_binary(L, utf8) of
|
||||||
B when is_binary(B) -> B;
|
B when is_binary(B) -> B;
|
||||||
_ -> [json(I, Config) || I <- L]
|
_ -> [json(I, Config) || I <- L]
|
||||||
catch
|
catch
|
||||||
_:_ ->
|
_:_ ->
|
||||||
[json(I, Config) || I <- L]
|
[json(I, Config) || I <- L]
|
||||||
|
end
|
||||||
end;
|
end;
|
||||||
json(Map, Config) when is_map(Map) ->
|
json(Map, Config) when is_map(Map) ->
|
||||||
best_effort_json_obj(Map, Config);
|
best_effort_json_obj(Map, Config);
|
||||||
|
@ -458,4 +464,15 @@ chars_limit_applied_on_format_result_test() ->
|
||||||
?assertEqual(Limit, size(LongStr1)),
|
?assertEqual(Limit, size(LongStr1)),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
|
string_array_test() ->
|
||||||
|
Array = #{<<"arr">> => [<<"a">>, <<"b">>]},
|
||||||
|
Encoded = emqx_utils_json:encode(json(Array, config())),
|
||||||
|
?assertEqual(Array, emqx_utils_json:decode(Encoded)).
|
||||||
|
|
||||||
|
iolist_test() ->
|
||||||
|
Iolist = #{iolist => ["a", ["b"]]},
|
||||||
|
Concat = #{<<"iolist">> => <<"ab">>},
|
||||||
|
Encoded = emqx_utils_json:encode(json(Iolist, config())),
|
||||||
|
?assertEqual(Concat, emqx_utils_json:decode(Encoded)).
|
||||||
|
|
||||||
-endif.
|
-endif.
|
||||||
|
|
Loading…
Reference in New Issue