Skip to content

Commit 9065406

Browse files
authored
Merge pull request #332 from cegela/feat/log-user-agent-and-referer
log user agent and refrer on static mapped requests
2 parents efb7ca2 + 56c3026 commit 9065406

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

lib/cegela/static.ex

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ defmodule Cegela.Static do
4141
end
4242

4343
defp log(%Plug.Conn{} = conn) do
44-
Logger.info(%{static: conn.request_path})
44+
ua = conn.req_headers |> Enum.filter(&match?({"user-agent", _}, &1)) |> Enum.map(&elem(&1, 1))
45+
ref = conn.req_headers |> Enum.filter(&match?({"referer", _}, &1)) |> Enum.map(&elem(&1, 1))
46+
47+
Logger.info(%{
48+
static: conn.request_path,
49+
ua: ua,
50+
ref: ref
51+
})
4552
end
4653
end

test/cegela/static_test.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,14 @@ defmodule Cegela.StaticTest do
4343
capture_log([level: :info], fn ->
4444
:get
4545
|> conn("/krakem")
46+
|> put_req_header("user-agent", "unit-test")
47+
|> put_req_header("referer", "http://example.org/")
4648
|> Static.call(static_routes: %{"/krakem" => url})
4749
end)
4850

4951
assert log =~ ~S(static: "/krakem")
52+
assert log =~ ~S(ua: ["unit-test"])
53+
assert log =~ ~S(ref: ["http://example.org/"])
5054
end
5155

5256
test "does nothing to something that does not match a static" do

0 commit comments

Comments
 (0)