[英]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. 我有一个phoenix应用程序,其中使用Logger
模块记录事件。
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. 当前使用Elixir 1.2和Phoenix 1.2.1。
Most likely the first call/2
pattern in your Plug isn't matching and is just plain not being called. 插件中的第一个call/2
模式很可能不匹配,只是没有被调用。
Add a Logger.info/1
call to the second call/2
pattern and you should see it work as expected. 将Logger.info/1
调用添加到第二个call/2
模式,您应该会看到它按预期工作。
def call(conn, _opts) do
Logger.info "Hello!"
conn
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.