How to set the stacktrace depth (backtrace_depth) in Elixir Erlang

Continuing the discussion from Stacktraces being cut off:

Thank you for sharing this snippet and the link. I’m wondering if there is a best practice on how to set this flag.

  1. There doesn’t seem to be a function for simply reading a system flag, is this so?
  2. It seems like there should be some way to start an app with this setting, is there some option to pass into elixir --erl "backtrace, 20 or something?
  3. If this has to be done on startup, is there a community convention on where to put something like this, so other devs would know where to find non-default changes made to the system?

I can’t remember how you can set it at startup time but you can do it at runtime using :erlang.system_flag(:backtrace_depth, new_depth). This returns the old value. The startup default is 8. Note that this sets the depth for all processes.

5 Likes

Thank you @rvirding, I ended up doing just that in the start callback function of my Application module.

Just like this for anyone else looking

defmodule MyApp.Application do
  require Logger
  use Application

  def start(_type, _args) do
    :erlang.system_flag(:backtrace_depth, 20)
    ...
3 Likes