Skip to content

Commit

Permalink
Handle special formats
Browse files Browse the repository at this point in the history
  • Loading branch information
reachfh committed Aug 12, 2024
1 parent 2dca196 commit 7b61389
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
36 changes: 22 additions & 14 deletions src/logger_formatter_json.erl
Original file line number Diff line number Diff line change
Expand Up @@ -168,21 +168,29 @@ value([], Value) ->
value(_, _) ->
error.

to_output(_Key, Value, _Config) ->
to_thoas(Value).

to_string({level, OutputFormat}, Value, Config) ->
to_output({level, OutputFormat}, Value, Config) ->
format_level(OutputFormat, Value, Config);
to_string(system_time, Value, Config) ->
format_time(Value, Config);
to_output(system_time, Value, Config) ->
list_to_binary(format_time(Value, Config));
% to_string({system_time, OutputFormat},Value,Config) ->
% format_time(OutputFormat, Value,Config);
to_string(mfa, Value, Config) ->
format_mfa(Value, Config);
% to_string(crash_reason,Value,Config) ->
% format_crash_reason(Value,Config);
to_string(_, Value, Config) ->
to_string(Value, Config).
to_output(mfa, Value, Config) ->
list_to_binary(format_mfa(Value, Config));
to_output(_mfa, Value, _Config) ->
to_thoas(Value).

% to_string({level, OutputFormat}, Value, Config) ->
% format_level(OutputFormat, Value, Config);
% to_string(system_time, Value, Config) ->
% format_time(Value, Config);
% % to_string({system_time, OutputFormat},Value,Config) ->
% % format_time(OutputFormat, Value,Config);
% to_string(mfa, Value, Config) ->
% format_mfa(Value, Config);
% % to_string(crash_reason,Value,Config) ->
% % format_crash_reason(Value,Config);
% to_string(_, Value, Config) ->
% to_string(Value, Config).

to_string(X, _) when is_atom(X) ->
atom_to_list(X);
Expand Down Expand Up @@ -269,10 +277,10 @@ to_thoas(Value) when is_list(Value) ->
end;
% is_pid/1
to_thoas(Value) when is_pid(Value) ->
pid_to_list(Value);
list_to_binary(pid_to_list(Value));
% is_reference/1
to_thoas(Value) when is_reference(Value) ->
ref_to_list(Value);
list_to_binary(ref_to_list(Value));
% is_map/1
to_thoas(Value) when is_map(Value) ->
lists:map(fun({K, V}) -> {to_thoas(K), to_thoas(V)} end,
Expand Down
4 changes: 2 additions & 2 deletions test/logger_formatter_json_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ to_thoas(_) ->
iolist_to_binary(logger_formatter_json:to_thoas(<<"foo\nbar">>))),
?assertEqual(<<"793µs"/utf8>>,
iolist_to_binary(logger_formatter_json:to_thoas(<<"793µs"/utf8>>))),
?assertEqual("<0.250.0>",
?assertEqual(<<"<0.250.0>">>,
logger_formatter_json:to_thoas(
c:pid(0, 250, 0))).

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\",{\"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">>,
?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\":[{\"__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\":\"<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 7b61389

Please sign in to comment.