diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b4fc79..a077281 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # CHANGELOG + + ## v0.9.1 - 2022-01-21 + +### Changed + * Client ip is now properly serialize as a string + ## v0.9 - 2022-01-05 ### Added diff --git a/lib/uinta/plug.ex b/lib/uinta/plug.ex index 203a3e0..b6c73ba 100644 --- a/lib/uinta/plug.ex +++ b/lib/uinta/plug.ex @@ -157,7 +157,7 @@ if Code.ensure_loaded?(Plug) do status: Integer.to_string(conn.status), timing: formatted_diff(diff), duration_ms: diff / 1000, - client_ip: conn.remote_ip, + client_ip: client_ip(conn), user_agent: get_first_value_for_header(conn, "user-agent"), referer: get_first_value_for_header(conn, "referer"), x_forwarded_for: get_first_value_for_header(conn, "x-forwarded-for"), @@ -196,6 +196,16 @@ if Code.ensure_loaded?(Plug) do |> List.first() end + def client_ip(conn) do + case :inet.ntoa(conn.remote_ip) do + {:error, _} -> + "" + + ip -> + List.to_string(ip) + end + end + @spec method(Plug.Conn.t(), graphql_info()) :: String.t() defp method(_, %{type: type}), do: type defp method(conn, _), do: conn.method diff --git a/mix.exs b/mix.exs index 9edcce4..d3f1ed3 100644 --- a/mix.exs +++ b/mix.exs @@ -8,7 +8,7 @@ defmodule Uinta.MixProject do app: :uinta, name: "Uinta", description: "Simpler structured logs and lower log volume for Elixir apps", - version: "0.9.0", + version: "0.9.1", elixir: "~> 1.8", source_url: @project_url, homepage_url: @project_url, diff --git a/test/uinta/plug_test.exs b/test/uinta/plug_test.exs index 6e69d97..52a0f59 100644 --- a/test/uinta/plug_test.exs +++ b/test/uinta/plug_test.exs @@ -146,12 +146,12 @@ defmodule Uinta.PlugTest do JsonPlug.call(conn(:get, "/"), []) end) - assert message =~ ~r/client_ip:.+/u - assert message =~ ~r/duration_ms: [0-9]+\.?[0-9]+/u - assert message =~ ~r/method: \"GET\"/u - assert message =~ ~r"path: \"/\""u - assert message =~ ~r/status: \"200\"/u - assert message =~ ~r/timing: \"[0-9]+[µm]s\"/u + assert message =~ ~r/client_ip\":\"127.0.0.1\"/u + assert message =~ ~r/duration_ms\":[0-9]+\.?[0-9]+/u + assert message =~ ~r/method\":\"GET\"/u + assert message =~ ~r"path\":\"/\""u + assert message =~ ~r/status\":\"200\"/u + assert message =~ ~r/timing\":\"[0-9]+[µm]s\"/u end test "logs graphql json to console" do @@ -163,13 +163,13 @@ defmodule Uinta.PlugTest do JsonPlug.call(conn(:post, "/graphql", params), []) end) - assert message =~ ~r/client_ip:.+/u - assert message =~ ~r/duration_ms: [0-9]+\.?[0-9]+/u - assert message =~ ~r/method: \"QUERY\"/u - assert message =~ ~r/operation_name: \"getUser\"/u - assert message =~ ~r"path: \"/graphql\""u - assert message =~ ~r/status: \"200\"/u - assert message =~ ~r/timing: \"[0-9]+[µm]s\"/u + assert message =~ ~r/client_ip\":\"127.0.0.1\"/u + assert message =~ ~r/duration_ms\":[0-9]+\.?[0-9]+/u + assert message =~ ~r"path\":\"/graphql\""u + assert message =~ ~r/status\":\"200\"/u + assert message =~ ~r/timing\":\"[0-9]+[µm]s\"/u + assert message =~ ~r/method\":\"QUERY\"/u + assert message =~ ~r/operation_name\":\"getUser\"/u end test "logs graphql json to console with extra headers" do @@ -190,12 +190,12 @@ defmodule Uinta.PlugTest do JsonPlug.call(conn, []) end) - assert message =~ ~r/client_ip:.+/u - assert message =~ ~r/x_forwarded_for: \"someip\"/u - assert message =~ ~r"referer: \"http://I.am.referer\""u - assert message =~ ~r/user_agent: \"Mozilla\"/u - assert message =~ ~r/x_forwarded_proto: \"http\"/u - assert message =~ ~r/x_forwarded_port: \"4000\"/u + assert message =~ ~r/client_ip\":\"127.0.0.1\"/u + assert message =~ ~r/x_forwarded_for\":\"someip\"/u + assert message =~ ~r"referer\":\"http://I.am.referer\""u + assert message =~ ~r/user_agent\":\"Mozilla\"/u + assert message =~ ~r/x_forwarded_proto\":\"http\"/u + assert message =~ ~r/x_forwarded_port\":\"4000\"/u end test "logs paths with double slashes and trailing slash" do