How to register something in the controller when starting Phoenix Server?

I am trying to print some debugging information from one of my controllers in my Phoenix application when the server is running.

defmodule PhoenixApp.TopicController do use PhoenixApp.Web, :controller alias PhoenixApp.Topic plug :action def index(conn, _params) do # ... log "this text" # ... render(conn, "index.html") end end 
+45
elixir phoenix-framework
Jun 20 '15 at 20:33
source share
2 answers

Well, it turns out pretty simple. You need to require the Logger elixir module in your controller and call one of its methods to register 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:

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



Source: Elixir Documentation - Logger

+63
Jun 20 '15 at 20:47
source share

You can also just do IO.puts or IO.inspect and it will appear, but IO.puts can be annoying if what you are trying to print does not implement the String.Chars protocol

+10
Jun 21 '15 at 6:46
source share



All Articles