Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
like image 335
Sheharyar Avatar asked Jun 20 '15 20:06

Sheharyar


2 Answers

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(params)}"

        # ...
    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

like image 116
Sheharyar Avatar answered Nov 19 '22 06:11

Sheharyar


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

like image 27
Arthur Collé Avatar answered Nov 19 '22 07:11

Arthur Collé