Skip to content

Commit

Permalink
Treat tuples as lists
Browse files Browse the repository at this point in the history
  • Loading branch information
reachfh committed Aug 11, 2024
1 parent a5ae057 commit 955dc27
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
25 changes: 19 additions & 6 deletions src/logger_formatter_json.erl
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,26 @@ to_thoas(Value) when is_float(Value) ->
to_thoas(Value) when is_integer(Value) ->
Value;
% is_list/1
to_thoas([]) ->
[];
to_thoas(Value) when is_list(Value) ->
case printable_list(lists:flatten(Value)) of
true ->
% list_to_binary(Value);
iolist_to_binary(io_lib:format("~ts", [Value]));
_ ->
iolist_to_binary(io_lib:format("~0tp", [Value]))
case Value of
[{_K, _V} | _Rest] ->
try
lists:map(fun({K, V}) -> {to_thoas(K), to_thoas(V)} end,
lists:keysort(1, Value))
catch
_:_ ->
iolist_to_binary(io_lib:format("~0tp", [Value]))
end;
_ ->
iolist_to_binary(io_lib:format("~0tp", [Value]))
end
end;
% is_pid/1
to_thoas(Value) when is_pid(Value) ->
Expand All @@ -269,11 +282,11 @@ to_thoas(Value) when is_map(Value) ->
to_thoas(Value) when is_number(Value) ->
Value;
% % is_tuple/1
% to_thoas(Value) when is_tuple(Value) ->
% Value1 = lists:map(fun to_thoas/1, tuple_to_list(Value)),
% iolist_to_binary(io_lib:format("{~0tp}", [Value1]);
% % iolist_to_binary(["{", Value1, "}"]);
% % iolist_to_binary(["{", thoas:encode_to_iodata(Value1, #{escape => unicode}), "}"]);
to_thoas(Value) when is_tuple(Value) ->
lists:map(fun to_thoas/1, tuple_to_list(Value));
% iolist_to_binary(io_lib:format("{~0tp}", [Value1]);
% iolist_to_binary(["{", Value1, "}"]);
% iolist_to_binary(["{", thoas:encode_to_iodata(Value1, #{escape => unicode}), "}"]);
% is_tuple/1
% is_function/1
% is_function/2
Expand Down
6 changes: 3 additions & 3 deletions test/logger_formatter_json_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ all() ->
is_printable,
unstructured,
structured,
% metadata,
metadata,
duplicate_keys].

print_string(_) ->
Expand All @@ -38,7 +38,7 @@ to_string(_) ->

to_thoas(_) ->
?assertEqual(foo, logger_formatter_json:to_thoas(foo)),
?assertEqual(<<"[]">>, logger_formatter_json:to_thoas([])),
?assertEqual([], logger_formatter_json:to_thoas([])),
?assertEqual(<<"[1,2,3]">>, logger_formatter_json:to_thoas([1, 2, 3])),
?assertEqual(<<"foo">>, logger_formatter_json:to_thoas("foo")),
?assertEqual(<<>>, logger_formatter_json:to_thoas(<<>>)),
Expand Down Expand Up @@ -427,7 +427,7 @@ metadata(_) ->
query_params =>
#{'__struct__' => 'Elixir.Plug.Conn.Unfetched', aspect => query_params},
req_cookies => #{'__struct__' => 'Elixir.Plug.Conn.Unfetched', aspect => cookies}}},
?assertEqual(<<"{\"msg\":\"abc\",\"level\":\"info\",\"conn\":{\"__struct__\":\"Elixir.Plug.Conn\",\"adapter\":\"{'Elixir.Plug.Cowboy.Conn',#{pid => <0.672.0>,port => 4000,scheme => <<\\\"http\\\">>,version => 'HTTP/1.1',path => <<\\\"/\\\">>,host => <<\\\"localhost\\\">>,peer => {{127,0,0,1},58196},bindings => #{},cert => undefined,headers => #{<<\\\"accept\\\">> => <<\\\"*/*\\\">>,<<\\\"host\\\">> => <<\\\"localhost:4000\\\">>,<<\\\"user-agent\\\">> => <<\\\"curl/8.6.0\\\">>},ref => 'Elixir.PhoenixContainerExampleWeb.Endpoint.HTTP',sock => {{127,0,0,1},4000},method => <<\\\"GET\\\">>,path_info => undefined,host_info => undefined,streamid => 1,body_length => 0,has_body => false,qs => <<>>}}\",\"assigns\":[],\"body_params\":{\"__struct__\":\"Elixir.Plug.Conn.Unfetched\",\"aspect\":\"body_params\"},\"cookies\":{\"__struct__\":\"Elixir.Plug.Conn.Unfetched\",\"aspect\":\"cookies\"},\"halted\":false,\"host\":\"localhost\",\"method\":\"GET\",\"owner\":[60,48,46,54,55,51,46,48,62],\"params\":{\"__struct__\":\"Elixir.Plug.Conn.Unfetched\",\"aspect\":\"params\"},\"path_info\":\"[]\",\"path_params\":[],\"port\":4000,\"private\":[],\"query_params\":{\"__struct__\":\"Elixir.Plug.Conn.Unfetched\",\"aspect\":\"query_params\"},\"query_string\":\"\",\"remote_ip\":\"{127,0,0,1}\",\"req_cookies\":{\"__struct__\":\"Elixir.Plug.Conn.Unfetched\",\"aspect\":\"cookies\"},\"req_headers\":\"[{<<\\\"accept\\\">>,<<\\\"*/*\\\">>},{<<\\\"host\\\">>,<<\\\"localhost:4000\\\">>},{<<\\\"user-agent\\\">>,<<\\\"curl/8.6.0\\\">>}]\",\"request_path\":\"/\",\"resp_body\":\"nil\",\"resp_cookies\":[],\"resp_headers\":\"[{<<\\\"cache-control\\\">>,<<\\\"max-age=0, private, must-revalidate\\\">>}]\",\"scheme\":\"http\",\"script_name\":\"[]\",\"secret_key_base\":\"nil\",\"state\":\"unset\",\"status\":\"nil\"},\"crash_reason\":\"{#{message => <<\\\"bad argument in arithmetic expression\\\">>,'__struct__' => 'Elixir.ArithmeticError','__exception__' => true},[{erlang,'+',[1,a],[{error_info,#{module => erl_erts_errors}}]},{'Elixir.PhoenixContainerExampleWeb.PageController',home,2,[{file,\\\"lib/phoenix_container_example_web/controllers/page_controller.ex\\\"},{line,8}]},{'Elixir.PhoenixContainerExampleWeb.PageController',action,2,[{file,\\\"lib/phoenix_container_example_web/controllers/page_controller.ex\\\"},{line,1}]},{'Elixir.PhoenixContainerExampleWeb.PageController',phoenix_controller_pipeline,2,[{file,\\\"lib/phoenix_container_example_web/controllers/page_controller.ex\\\"},{line,1}]},{'Elixir.Phoenix.Router','__call__',5,[{file,\\\"lib/phoenix/router.ex\\\"},{line,484}]},{'Elixir.PhoenixContainerExampleWeb.Endpoint',plug_builder_call,2,[{file,\\\"lib/phoenix_container_example_web/endpoint.ex\\\"},{line,1}]},{'Elixir.PhoenixContainerExampleWeb.Endpoint','call (overridable 3)',2,[{file,\\\"deps/plug/lib/plug/debugger.ex\\\"},{line,136}]},{'Elixir.PhoenixContainerExampleWeb.Endpoint',call,2,[{file,\\\"lib/phoenix_container_example_web/endpoint.ex\\\"},{line,1}]},{'Elixir.Phoenix.Endpoint.SyncCodeReloadPlug',do_call,4,[{file,\\\"lib/phoenix/endpoint/sync_code_reload_plug.ex\\\"},{line,22}]},{'Elixir.Plug.Cowboy.Handler',init,2,[{file,\\\"lib/plug/cowboy/handler.ex\\\"},{line,11}]},{cowboy_handler,execute,2,[{file,\\\"/Users/jake/work/phoenix_container_example/deps/cowboy/src/cowboy_handler.erl\\\"},{line,37}]},{cowboy_stream_h,execute,3,[{file,\\\"/Users/jake/work/phoenix_container_example/deps/cowboy/src/cowboy_stream_h.erl\\\"},{line,306}]},{cowboy_stream_h,request_process,3,[{file,\\\"/Users/jake/work/phoenix_container_example/deps/cowboy/src/cowboy_stream_h.erl\\\"},{line,295}]},{proc_lib,init_p_do_apply,3,[{file,\\\"proc_lib.erl\\\"},{line,329}]}]}\",\"domain\":\"[cowboy]\",\"error_logger\":{\"tag\":\"error\"},\"gl\":[60,48,46,51,57,52,46,48,62],\"otel_span_id\":\"052d4929d398aec6\",\"otel_trace_flags\":\"01\",\"otel_trace_id\":\"66b7dd4d7dad9f9aba84fb43389b4611\",\"pid\":[60,48,46,54,55,50,46,48,62],\"time\":1723325773127320,\"xray_trace_id\":\"1-66b7dd4d-dad9f9aba84fb43389b4611@052d4929d398aec6\"}\n">>,
?assertEqual(<<"{\"msg\":\"abc\",\"level\":\"info\",\"conn\":{\"__struct__\":\"Elixir.Plug.Conn\",\"adapter\":[\"Elixir.Plug.Cowboy.Conn\",{\"bindings\":[],\"body_length\":0,\"cert\":\"undefined\",\"has_body\":false,\"headers\":{\"accept\":\"*/*\",\"host\":\"localhost:4000\",\"user-agent\":\"curl/8.6.0\"},\"host\":\"localhost\",\"host_info\":\"undefined\",\"method\":\"GET\",\"path\":\"/\",\"path_info\":\"undefined\",\"peer\":[[127,0,0,1],58196],\"pid\":[60,48,46,54,55,50,46,48,62],\"port\":4000,\"qs\":\"\",\"ref\":\"Elixir.PhoenixContainerExampleWeb.Endpoint.HTTP\",\"scheme\":\"http\",\"sock\":[[127,0,0,1],4000],\"streamid\":1,\"version\":\"HTTP/1.1\"}],\"assigns\":[],\"body_params\":{\"__struct__\":\"Elixir.Plug.Conn.Unfetched\",\"aspect\":\"body_params\"},\"cookies\":{\"__struct__\":\"Elixir.Plug.Conn.Unfetched\",\"aspect\":\"cookies\"},\"halted\":false,\"host\":\"localhost\",\"method\":\"GET\",\"owner\":[60,48,46,54,55,51,46,48,62],\"params\":{\"__struct__\":\"Elixir.Plug.Conn.Unfetched\",\"aspect\":\"params\"},\"path_info\":[],\"path_params\":[],\"port\":4000,\"private\":[],\"query_params\":{\"__struct__\":\"Elixir.Plug.Conn.Unfetched\",\"aspect\":\"query_params\"},\"query_string\":\"\",\"remote_ip\":[127,0,0,1],\"req_cookies\":{\"__struct__\":\"Elixir.Plug.Conn.Unfetched\",\"aspect\":\"cookies\"},\"req_headers\":{\"accept\":\"*/*\",\"host\":\"localhost:4000\",\"user-agent\":\"curl/8.6.0\"},\"request_path\":\"/\",\"resp_body\":\"nil\",\"resp_cookies\":[],\"resp_headers\":{\"cache-control\":\"max-age=0, private, must-revalidate\"},\"scheme\":\"http\",\"script_name\":[],\"secret_key_base\":\"nil\",\"state\":\"unset\",\"status\":\"nil\"},\"crash_reason\":[{\"__exception__\":true,\"__struct__\":\"Elixir.ArithmeticError\",\"message\":\"bad argument in arithmetic expression\"},\"[{erlang,'+',[1,a],[{error_info,#{module => erl_erts_errors}}]},{'Elixir.PhoenixContainerExampleWeb.PageController',home,2,[{file,\\\"lib/phoenix_container_example_web/controllers/page_controller.ex\\\"},{line,8}]},{'Elixir.PhoenixContainerExampleWeb.PageController',action,2,[{file,\\\"lib/phoenix_container_example_web/controllers/page_controller.ex\\\"},{line,1}]},{'Elixir.PhoenixContainerExampleWeb.PageController',phoenix_controller_pipeline,2,[{file,\\\"lib/phoenix_container_example_web/controllers/page_controller.ex\\\"},{line,1}]},{'Elixir.Phoenix.Router','__call__',5,[{file,\\\"lib/phoenix/router.ex\\\"},{line,484}]},{'Elixir.PhoenixContainerExampleWeb.Endpoint',plug_builder_call,2,[{file,\\\"lib/phoenix_container_example_web/endpoint.ex\\\"},{line,1}]},{'Elixir.PhoenixContainerExampleWeb.Endpoint','call (overridable 3)',2,[{file,\\\"deps/plug/lib/plug/debugger.ex\\\"},{line,136}]},{'Elixir.PhoenixContainerExampleWeb.Endpoint',call,2,[{file,\\\"lib/phoenix_container_example_web/endpoint.ex\\\"},{line,1}]},{'Elixir.Phoenix.Endpoint.SyncCodeReloadPlug',do_call,4,[{file,\\\"lib/phoenix/endpoint/sync_code_reload_plug.ex\\\"},{line,22}]},{'Elixir.Plug.Cowboy.Handler',init,2,[{file,\\\"lib/plug/cowboy/handler.ex\\\"},{line,11}]},{cowboy_handler,execute,2,[{file,\\\"/Users/jake/work/phoenix_container_example/deps/cowboy/src/cowboy_handler.erl\\\"},{line,37}]},{cowboy_stream_h,execute,3,[{file,\\\"/Users/jake/work/phoenix_container_example/deps/cowboy/src/cowboy_stream_h.erl\\\"},{line,306}]},{cowboy_stream_h,request_process,3,[{file,\\\"/Users/jake/work/phoenix_container_example/deps/cowboy/src/cowboy_stream_h.erl\\\"},{line,295}]},{proc_lib,init_p_do_apply,3,[{file,\\\"proc_lib.erl\\\"},{line,329}]}]\"],\"domain\":\"[cowboy]\",\"error_logger\":{\"tag\":\"error\"},\"gl\":[60,48,46,51,57,52,46,48,62],\"otel_span_id\":\"052d4929d398aec6\",\"otel_trace_flags\":\"01\",\"otel_trace_id\":\"66b7dd4d7dad9f9aba84fb43389b4611\",\"pid\":[60,48,46,54,55,50,46,48,62],\"time\":1723325773127320,\"xray_trace_id\":\"1-66b7dd4d-dad9f9aba84fb43389b4611@052d4929d398aec6\"}\n">>,
iolist_to_binary(logger_formatter_json:format(#{level => info,
msg => {string, "abc"},
meta => Meta},
Expand Down

0 comments on commit 955dc27

Please sign in to comment.