Crash Reporting for Server-side Swift Apps

Petr Pavlík
Dec 26, 2019 · 2 min read

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
Backtrace.install()

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.

Petr Pavlík

Written by

iOS dev @ Perry Street Software, server-side Swift fan. I’ve also made http://swiftpack.co, npm for server-side swift

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade