How to Change Elixir Log Levels for a Running Application

Logging is essential to any production system, but it’s also terribly easy to make your logs so noisy they become worthless. That’s why I’m a big fan of carefully specifying the criticality level of everything I log. Not only does it allow us to easily search the logs for potential problems, it allows us to quiet our logs down and raise the signal to noise ratio. In production, I don’t want or need debug level logging… until I do. Thankfully, Elixir makes it easy for us to change what level we’re logging at on the fly.

Let’s say we have a Phoenix website and we want to create a log entry every time we serve the site’s index while we’re developing, but not when we go to production.

require Logger

defmodule LogBlogWeb.PageController do
  use LogBlogWeb, :controller

  def index(conn, _params) do
    Logger.log(:debug, "hello there!")
    render conn, "index.html"

By default, a Pheonix app runs with debug logging under the dev configuration and info under the prod config.

# config/dev.exs
config :logger, :console, format: "[$level] $message\n"

# config/prod.exs
config :logger, level: :info

This is great! It’s exactly the behavior we want in our production system. At least, that is, until we need to see the debug information because something is wrong in production.

Or, we would be if it wasn’t for the Erlang VM and iex.

In order to be able to connect to our running system, we need to start the process with a node name. Instead of starting our website with mix phx.server like we normally might, we start it with elixir instead.

MIX_ENV=prod PORT=4000 elixir --name myapp@localhost -S mix phx.server

Now that we have a name for our process, we can use iex to connect to the running process via the --remsh option in a separate shell.

iex --sname debug --remsh "myapp@localhost"

Erlang/OTP 20 [erts-9.2]  [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Interactive Elixir (1.5.3) - press Ctrl+C to exit (type h() ENTER for help)

Finally, now that we have an interactive session established, we can change the Logger configuration to output the debug logs.

iex(myapp@localhost)1> Logger.configure(level: :debug)

If we check the logs now, we’ll see our debug message.

08:56:34.205 [info] GET /
08:56:34.218 [debug] Processing with LogBlogWeb.PageController.index/2
  Parameters: %{}
  Pipelines: [:browser]
08:56:34.218 [debug] hello there!
08:56:34.227 [info] Sent 200 in 22ms

When we’re done gathering the extra information, we can simply set the level back to info.

iex(myapp@localhost)2> Logger.configure(level: :info)

And there you have it folks. That’s how you change the log level of your Elixir application without ever taking it offline.


, ,

  1. #1 by Svetlana Rosemond on July 5, 2018 - 4:17 pm

    Just found this blog from StackOverflow.

    I just read your post about being a VB developer or Software Developer and I’ve got to say it made me question if I should get off the fence and learn Elixir. 2 months ago, I was all in to learn Elixir because I learned Python on my own, and I wanted to add to my experience. The problem was, am I going to use Elixir to the fullest extent? Give what Elixir was made for, I doubt it. Python is flexible in a variety of areas, and to some extent is even better than Powershell.

    I assume from your blog, you are self-taught, meaning you don’t have a CS degree. Is most of your job back end web development? If so, did you learn data structures and algorithms? If you used books, can you name them? While I know Python, I’m not a fan of web development(front or back end), so I never learned Django or Flask. Problem is, I’m struggling to find a job where Python is used but not for the web. Do you know Elixir?

    Liked by 1 person

    • #2 by Christopher J. McClellan on July 5, 2018 - 6:03 pm

      You found my Elixir blog on Stack Overflow? Wow! That was fast. Would you mind linking back to it so I can read the Q&A?

      That blog is one of my favorites, even if it is a bit rant-y in hind sight. I’m glad it had a positive impact for you!

      I am self taught in the sense that I don’t have a CS degree, but I always feel weird saying I’m self taught. I’ve had a lot of great mentors along the way and I owe them a great debt. I didn’t get to be where I am through sheer will power. I’ve had a lot of help and many great teachers, just not in a classroom.

      I’ve done lots of different kinds of development over the years. I started with desktop software based on MS Access & SQL Server and a proprietary scripting language for a Space Planning/Merchandising software. Then I moved to .Net desktop and web development. I’m pretty good on the backend, but know just enough front end to get by really. For the last 2-3 years I’ve been bouncing back and forth from embedded software and big data. Knowing that history provides a lot of context for why I think we should strive to be able to program in any language/problem domain. My career has forced me to be able to jump between languages & domains and it’s been very good for my skill set and career.

      I have ended up learning quite a bit about data structures and algorithms along the way, but only as I’ve needed to. The big thing about algos, in my opinion, is to be able to spot the Big O complexity of an algorithm. Knowing that an algo will become exponemtionally slower as the size of the set increases is invaluable. Having an undertanding of trees and tree traversal have helped me on occasion too, but mostly in the context of custom code and Git history analysis (Rubberduck and GitNStats respectively). The truth is, data structures and algos have only come up on a handful of occasions during my near decade in software. As for books, I’ve read many, but the ones I recommend to folks are “Code Complete” by Steve McConnel and “Working Effectively with Legacy Code” by Michael Feathers.

      Do I know Elixir?…. A bit. We’ve been using it for some microservices on my current project. I’m no expert at it, but I’ve found it to be an incredibly powerful and enjoyable language. The Erlang VM is a truly miraculous environment for distributed and parallelizable computing.

      Thank you so much for taking the time to reach out and letting me know you’ve enjoyed my blog. You’ve given me some of the motivation I’ve been needing to tackle a series on Lambda Calculus that I’ve been thinking of for a little while now.


      • #3 by Svetlana Rosemond on July 6, 2018 - 2:48 pm

        There was no Q/A, I was browsing StackOverflow profiles and came across yours that had a link to the blog.

        I’m glad to hear that you’re still able to get into Software without a CS degree. It gives people like me hope. I’m not opposed to working hard, it’s just discouraging when companies only care about a degree rather than what you really know.

        For awhile I gave up wanting to learn Elixir because even if I didn’t work with the language professionally, I would never use it in my day-to-day activities. Python, while I never worked professionally with it, I use it in places where some might use Powershell. What I’m going to use Elixir for, I can do with Python, it might sound foolish to say, because one is OO, the other is functional, but I’m never going to use Elixir to its fullest extent on my own.

        Liked by 1 person

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: