Skip to content

Commit

Permalink
Special case tuple with map
Browse files Browse the repository at this point in the history
  • Loading branch information
reachfh committed Aug 11, 2024
1 parent e73ff77 commit f1d9e16
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
14 changes: 11 additions & 3 deletions src/logger_formatter_json.erl
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,13 @@ value(_, _) ->
error.

to_output(_Key, Value, Config) when is_map(Value) ->
% maps:iterator/2 is only in OTP 27+
% lists:map(fun ({K, V}) -> {K, to_output(K, V, Config)} end, maps:to_list(maps:iterator(Value, ordered)));
lists:map(fun({K, V}) -> {K, to_output(K, V, Config)} end,
lists:keysort(1, maps:to_list(Value)));
to_output(_Key, {TK, Value}, Config) when is_map(Value) ->
{to_output(_Key, TK, Config), to_output(_Key, Value, Config)};
Value2 =
lists:map(fun({K, V}) -> {K, to_output(K, V, Config)} end,
lists:keysort(1, maps:to_list(Value))),
to_output(_Key, {TK, Value2}, Config);
to_output(Key, Value, Config) ->
iolist_to_binary(to_string(Key, Value, Config)).

Expand All @@ -198,6 +199,13 @@ to_string(X, _) when is_pid(X) ->
pid_to_list(X);
to_string(X, _) when is_reference(X) ->
ref_to_list(X);
% to_string(X, Config) when is_map(X) ->
% lists:map(fun({K, V}) -> {to_string(K, Config), to_string(V, Config)} end,
% lists:keysort(1, maps:to_list(X)));
% to_string({Key, Value}, Config) when is_map(Value) ->
% V2 = lists:map(fun({K, V}) -> {to_string(K, Config), to_string(V, Config)} end,
% lists:keysort(1, maps:to_list(Value))),
% to_string({Key, V2}, Config);
to_string(X, Config) when is_list(X) ->
case printable_list(lists:flatten(X)) of
true ->
Expand Down
3 changes: 2 additions & 1 deletion test/logger_formatter_json_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,8 @@ 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',body_length => 0,has_body => false,host_info => undefined,method => <<\\\"GET\\\">>,path_info => undefined,qs => <<>>,sock => {{127,0,0,1},4000},streamid => 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\":\"<0.673.0>\",\"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\":\"<0.394.0>\",\"otel_span_id\":\"052d4929d398aec6\",\"otel_trace_flags\":\"01\",\"otel_trace_id\":\"66b7dd4d7dad9f9aba84fb43389b4611\",\"pid\":\"<0.672.0>\",\"time\":\"2024-08-10T16:36:13.127320-05:00\",\"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,<<\\\"<0.672.0>\\\">>},{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\":\"<0.673.0>\",\"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\":\"<0.394.0>\",\"otel_span_id\":\"052d4929d398aec6\",\"otel_trace_flags\":\"01\",\"otel_trace_id\":\"66b7dd4d7dad9f9aba84fb43389b4611\",\"pid\":\"<0.672.0>\",\"time\":\"2024-08-10T16:36:13.127320-05:00\",\"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 f1d9e16

Please sign in to comment.