How to log exception + stacktrace

Really having a hard time finding documentation around exception handling. Given e is the caught exception, I have tried

Logger.error(e), but that gives me ** (Protocol.UndefinedError) protocol String.Chars not implemented

Exception.message(e) gives me the message as a string, but what im looking for is message + formatted stacktrace.

I have seen Exception.format_banner which sounds like what i want, but Im not sure what the first param (kind) refers to

Thanks for the help :slight_smile:


You are in the right path with Exception.format_banner/3, but this function does not print the stacktrace. Instead, use Exception.format/3, and you will be good to go.

Now about the kind parameter, is basically one of this 3 options: :error, :throw or :exit. For exceptions catched with try do ... rescue, use the :error kind.

For more detail about it, look at the try, catch and rescue guide.


I ran into a related problem. I wanted to log an exception with the correct metadata for it to be treated as an exception by error trackers like Sentry.

Here’s the solution I came up with: