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

ElixirPhoenix Framework

Elixir Problem Overview

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")

    # ... 

Elixir Solutions

Solution 1 - Elixir

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  "Logging this text!"
        Logger.debug "Var value: #{inspect(params)}"

        # ...

Supported levels are:

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

Source: Elixir - Logger Documentation

Solution 2 - Elixir

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


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSheharyarView Question on Stackoverflow
Solution 1 - ElixirSheharyarView Answer on Stackoverflow
Solution 2 - ElixirArthur ColléView Answer on Stackoverflow