简体   繁体   中英

Phoenix/Elixir - Logger works in some modules, but not others

I have a phoenix app in which I'm logging events using the Logger module.

Outputs to console as expected when using like this in a controller:

#web/controllers/v1/some_controller.ex
defmodule API.V1.SomeController do
  use AgilePulse.Web, :controller
  require Logger
  plug AgilePulse.Plugs.Params when action in [:create]

  def create(conn, params) do
    Logger.info "#{inspect(conn)}"
  end

end

But outputs nothing when used like this:

#web/plugs/params.ex
defmodule AgilePulse.Plugs.Params do
  require Logger

  def init(opts), do: opts

  def call(%Plug.Conn{params: %{"data" => data}} = conn, _opts) do
    Logger.info "#{inspect(conn)}"
  end

  def call(conn, _opts), do: conn

end

Why is that? And how can you get it to work in this scenario?

Currently using Elixir 1.2 and Phoenix 1.2.1.

Most likely the first call/2 pattern in your Plug isn't matching and is just plain not being called.

Add a Logger.info/1 call to the second call/2 pattern and you should see it work as expected.

def call(conn, _opts) do
  Logger.info "Hello!"
  conn
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM