Skip to content

Commit

Permalink
Change logs to be structured
Browse files Browse the repository at this point in the history
  • Loading branch information
kmakiela committed Feb 17, 2020
1 parent 618009e commit 723828f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 17 deletions.
16 changes: 9 additions & 7 deletions lib/mongoose_push.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ defmodule MongoosePush do

{time, push_result} =
if pool == nil do
Logger.error(~s"No pool matching mode=#{mode} and tags=#{inspect(tags)}")
Logger.error(fn ->
"action=choose_pool, result=fail, mode=#{inspect(mode)}, tags=#{inspect(tags)}"
end)

{0, {:error, {:generic, :no_matching_pool}}}
else
request =
Expand All @@ -95,17 +98,16 @@ defmodule MongoosePush do
defp maybe_log(:ok), do: :ok

defp maybe_log({:error, {type, reason}} = return_value) do
Logger.warn(
~s"Unable to complete push request due to service error: #{inspect(reason)} in category: #{
type
}"
)
Logger.warn(fn ->
"action=push, result=fail, reason=#{inspect(reason)}, type=#{inspect(type)}"
end)

return_value
end

defp maybe_log({:error, reason} = return_value) do
Logger.warn(~s"Unable to complete push request due to #{inspect(reason)}")
Logger.warn(fn -> "action=push, result=fail, reason=#{inspect(reason)}" end)

return_value
end
end
24 changes: 18 additions & 6 deletions lib/mongoose_push/service/apns/state.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,31 @@ defmodule MongoosePush.Service.APNS.State do
nil ->
all_topics = Certificate.extract_topics!(config[:auth][:cert])
default_topic = all_topics[:topic]
Logger.info(~s"Successfully extracted default APNS topic: #{default_topic}")

Logger.info(fn ->
"worker=apns_state, action=extract_default_topic, result=success, topic=#{
default_topic
}"
end)

Keyword.put(config, :default_topic, default_topic)

default_topic ->
Logger.info(~s"Using user-defined default APNS topic: #{default_topic}")
Logger.info(fn ->
"worker=apns_state, action=use_user-defined_topic, result=success, topic=#{
default_topic
}"
end)

config
end
catch
_, reason ->
Logger.warn(
~s"Unable to extract APNS topic from the #{config[:mode]} certificate " <>
"due to: #{inspect(reason)}"
)
Logger.warn(fn ->
"worker=apns_state, action=extract_topic_from_certificate, result=fail, mode=#{
config[:mode]
}, reason=#{inspect(reason)}"
end)

config
end
Expand Down
16 changes: 12 additions & 4 deletions lib/mongoose_push/service/apns/supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,12 @@ defmodule MongoosePush.Service.APNS.Supervisor do
error =
case not File.exists?(cert_path) or not File.exists?(key_path) do
true ->
Logger.error(~s"Required authentication elements are missing. Got:
certificate_path=#{cert_path}, key_path=#{key_path}")
Logger.error(fn ->
"worker=apns_supervisor, action=authenticate_cert, result=fail, reason=required_elements_missing, cert_path=#{
cert_path
}, key_path=#{key_path}"
end)

:bad_auth

false ->
Expand Down Expand Up @@ -132,8 +136,12 @@ defmodule MongoosePush.Service.APNS.Supervisor do
error =
case is_nil(key) or is_nil(team) or not File.exists?(p8_file_path) do
true ->
Logger.error(~s"Required authentication elements are missing. Got:
key=#{key}, team=#{team}, p8_file=#{p8_file_path}")
Logger.error(fn ->
"worker=apns_supervisor, action=authenticate_token, result=fail, reason=required_elements_missing, key=#{
key
}, team=#{team}, p8_file=#{p8_file_path}"
end)

:bad_auth

false ->
Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"joken": {:hex, :joken, "2.2.0", "2daa1b12be05184aff7b5ace1d43ca1f81345962285fff3f88db74927c954d3a", [:mix], [{:jose, "~> 1.9", [hex: :jose, repo: "hexpm", optional: false]}], "hexpm", "b4f92e30388206f869dd25d1af628a1d99d7586e5cf0672f64d4df84c4d2f5e9"},
"jose": {:hex, :jose, "1.9.0", "4167c5f6d06ffaebffd15cdb8da61a108445ef5e85ab8f5a7ad926fdf3ada154", [:mix, :rebar3], [{:base64url, "~> 0.0.1", [hex: :base64url, repo: "hexpm", optional: false]}], "hexpm", "6429c4fee52b2dda7861ee19a4f09c8c1ffa213bee3a1ec187828fde95d447ed"},
"lager": {:hex, :lager, "3.8.0", "3402b9a7e473680ca179fc2f1d827cab88dd37dd1e6113090c6f45ef05228a1c", [:rebar3], [{:goldrush, "0.1.9", [hex: :goldrush, repo: "hexpm", optional: false]}], "hexpm", "f6cb541b688eab60730d8d286eb77256a5a9ad06eac10d43beaf55d07e68bbb6"},
"logfmt": {:hex, :logfmt, "3.3.1", "d9eaf344f81f4b4a6f2a1c73d79cb9cb4c4ff1d7c878d7a1bf3cb92761f653db", [:mix], [], "hexpm", "5030c9dafe1ef85e416cc867cd8db836de512c54047735c7a3e7be0ba67a532e"},
"logger_lager_backend": {:hex, :logger_lager_backend, "0.1.0", "4858d5ac26a3a6085274933bf8b3061973cadac4adbe8e0181933e8cece78abb", [:mix], [{:lager, "~> 3.2", [hex: :lager, repo: "hexpm", optional: false]}], "hexpm", "eb382bc640ee1bea62314db56c971517b1fe5700ef3e62edef13607143c0bfbf"},
"makeup": {:hex, :makeup, "1.0.0", "671df94cf5a594b739ce03b0d0316aa64312cee2574b6a44becb83cd90fb05dc", [:mix], [{:nimble_parsec, "~> 0.5.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "a10c6eb62cca416019663129699769f0c2ccf39428b3bb3c0cb38c718a0c186d"},
"makeup_elixir": {:hex, :makeup_elixir, "0.14.0", "cf8b7c66ad1cff4c14679698d532f0b5d45a3968ffbcbfd590339cb57742f1ae", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "d4b316c7222a85bbaa2fd7c6e90e37e953257ad196dc229505137c5e505e9eff"},
Expand Down

0 comments on commit 723828f

Please sign in to comment.