mbriggs
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 ![]()
Marked As Solved
kelvinst
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.
Also Liked
mike_bianco
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:
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









