How to Emit Crash Reports
I’ve been running a swift app written using Vapor called swiftpack.co for few years at this point. It’s a simple app running on Heroku, backed by a mongo database. It works, but one of the main reason I wouldn’t really recommend anyone publishing a server-side swift app was that if the app crashes, all you see in the Heroku logs is something like `Process existed with code 5`. Well, as the Predator says, good hunt.
So I was quite impressed when someone sent me a link to Backtrace and immediately decided to give it a try. This is the result.
This can save you days of debugging, especially considering the fact that the nastiest bugs tend to show up when your app is under loud after you’ve finally deployed it to production.
How to Make Backtraces Work on Heroku
Add Backtraces package as your dependency and add this to main.swift
import Backtrace// Do this first
Now the readme says that you need to build the app with debug symbols, which are generally stripped for the release version an app, for this to work.
$ swift build -c release -Xswiftc -g
How to do this on Heroku? I’m using the Vapor-provided build pack to deploy my app and chances are that so are you. I was digging into the implementation and discovered that you can pass any extra parameters you want to pass to the build command using SWIFT_BUILD_FLAGS environmental variable. So go to settings section of your Heroku app and add a new config var SWIFT_BUILD_FLAGS with value -Xswiftc -g.
That’s it, you can try yourself by adding a crashing code to one of your routes or something like that, and trigger a crash.
To collect the logs, I’d recommend installing Heroku CLI and dumping the logs into a text file like this
heroku logs -a packagecatalog — num=10000 > logs.txt
since the log view on the website does not really show you anything from the history.
And that’s it. I’m not 100% sure if using this has any drawbacks, but I’ve been using it in production for few weeks at this point and I don’t see any so far.
Thanks to @alfa for putting this package together and happy coding.