Can't log exception with :logger

Hi,

I’m struggling to log an exception by using :logger directly.

I’ve created a new project, in application.ex I’m adding handler, it successfully logs regular Logger.error/1 :logger.error/1 but completely ignores raise "x"

application.ex

defmodule Odin.Application do
  use Application

  @impl true
  def start(_type, _args) do
    children = [
    ]

    :ok =
      :logger.add_handler(
        Odin,
        :logger_std_h,
        %{
          config: %{type: :file, file: 'log.log', max_no_bytes: 100_000_000},
          level: :debug
        }
      )

    opts = [strategy: :one_for_one, name: Odin.Supervisor]
    Supervisor.start_link(children, opts)
  end
end

Forgot to mention OTP and Elixir:
Erlang/OTP 24 [erts-12.2] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [jit] [dtrace]
IEx 1.13.1 (compiled with Erlang/OTP 24)

1 Like

raise/1 do not log anything, so I do not understand what is the question here. Can you provide full MCVE?

Hm, how do you log exception then? I’d like to log all :logger events and errors/exceptions in the code to a file.

Handled exceptions aren’t (and IMHO should not be) logged. Unhandled exceptions will be logged on process boundary.

1 Like

Let’s say we have this pseudocode, how to log this?

  def a do
    b(:c)
  end

  def b(:d) do
    :ok
  end