elixir - Logging IP Addresses for all visitors -


i've created first elixir/phoenix app, , want log ip addresses of visitors.

in router.ex, "logging" ip addresses pages exist.

def log_ip(conn, _)   conn.remote_ip   |> tuple.to_list   |> enum.join(".")   |> io.puts   conn end  ... defmodule mysite.router   use mysite.web, :router    pipeline :browser     plug :log_ip     ...   scope "/", mysite     pipe_through :browser 

this works fine usually, seeing in terminal there bots trying access nonexistent pages e.g. "/admin.php", "/command.php". want log these ip addresses, above code not working that.

potentially unrelated, have code handle people hit non-existent links (e.g. /blog/non-existent-page)

defimpl plug.exception, for: phoenix.template.undefinederror   def status(_exception), do: 404 end 

is there way access conn , log remote_ip before return 404 (or better way handle of this)?

you can put logger plug in endpoint rather router. endpoint in lib/<yourapp>/endpoint.ex. see like:

  ...   plug plug.requestid   plug plug.logger   ... 

you can put both function definition , plug :log_ip call after example.


Comments