How to Log something in Controller when Phoenix Server is running?

*爱你&永不变心* 提交于 2019-12-31 08:54:16

问题


I'm trying to print some debug information from one of my Controllers in my Phoenix app when the server is running.

defmodule PhoenixApp.TopicController do
  use PhoenixApp.Web, :controller

  def index(conn, _params) do
    log("this text")

    # ... 
  end
end

回答1:


Okay, turns out it's pretty straight forward. You need to require the Logger elixir module in your controller and call one of it's methods to log your text.

defmodule PhoenixApp.TopicController do
    require Logger

    def index(conn, _params) do
        Logger.info  "Logging this text!"
        Logger.debug "Var value: #{inspect(var)}"

        # ...
    end
end

Supported levels are:

  • :debug - for debug-related messages
  • :info - for information of any kind
  • :warn - for warnings
  • :error - for errors

Source: Elixir - Logger Documentation




回答2:


You can also just do IO.puts or IO.inspect and it'll show up, but IO.puts can be troublesome if what you're trying to print doesn't implement the String.Chars protocol



来源:https://stackoverflow.com/questions/30958616/how-to-log-something-in-controller-when-phoenix-server-is-running

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!