Logging in server-side Swift

Sebastian Kreutzberger
SwiftyBeaver Blog
Published in
4 min readOct 13, 2016

--

With the release of Swift 3 in September 2016, Swift became available for Linux. That opened it to a whole new world of software development: server-side apps. Since logging is a key component in software development, making SwiftyBeaver, the leading logging framework for Swift, available for server-side apps became a top priority.

And today, I am more than happy to announce that it got accomplished. SwiftyBeaver is the world’s first logging framework that supports the whole spectrum of Swift-compatible devices. Macs, iPhones, iPads, Apple TVs, Apple Watches, and finally Linux servers — SwiftyBeaver covers them all.

Since we really love server-side Swift, we wanted to make it as convenient as possible for Swift developers to get started with server-side Swift to share our excitement. Continue reading to learn more.

SwiftyBeaver on Linux

Already before the official release of Swift 3 in September 2016, the work on porting SwiftyBeaver from Swift 2 to Swift 3 had started. On release day of Swift 3, SwiftyBeaver’s porting to Swift 3 was finished and celebrated with the new major version tag 1.0

Swift 3 was built to support Linux out-of-the box and so we thought that making SwiftyBeaver work under Linux would be a piece of cake. But that was wrong.

The porting took nearly a month.

Parts of Swift’s Foundation library were still more or less centered around being run on the Apple platform and under Linux certain functionalities simply were not implemented, yet. Additionally, some basic functions like arc4_random() to generate random numbers (which is important in SwiftyBeavers end-to-end encryption component) just were not existing under Linux at all.

The solution for generating random numbers under Linux was a new wrapper which is using the operating system’s own /dev/urandom CLI command. That wrapper and many improvements also found their way in our dedicated AES256CBC encryption framework which is also embedded in SwiftyBeaver itself.

In total, the porting of SwiftyBeaver to server-side Swift took nearly a month and 2 additional open-source projects spun off the process, too: our logging provider for Vapor and the website ported to server-side Swift.

SwiftyBeaver Logging Provider for Vapor

Writing web apps nearly always requires a web framework which needs a tightly integrated logging system to get things done. In Swift you can choose between the web frameworks Vapor, IBM’s Kitura and Perfect.

We saw a great fit in Vapor’s philosophy

We picked Vapor as the first web framework where we wanted to add the powerful logging capabilities of SwiftyBeaver because we saw a great fit in the project’s philosophy, the roadmap, the creators and especially the community behind it.

Vapor embeds external libraries as providers and so we created the SwiftyBeaver Logging Provider for Vapor to easily log to all SwiftyBeaver destinations (console, file, cloud) from inside a Vapor web app.

Here is an example of a simple Vapor-based web app. It logs to console and file each time a call the index page is called in a browser:

SwiftyBeaver Website in Vapor

Before we started the work on porting the SwiftyBeaver cloud backend from Node.js to server-side Swift (yes, that is the plan!), we wanted to immerse ourselves even deeper into the new world of server-side Swift to really understand the user’s pains and needs.

A starting point for your own server-side Swift adventures

We decided to port our own website swiftybeaver.com, written in Node.js using the Express.js web framework, to server-side Swift using the Vapor web framework.

Long story short, we succeeded and on the way learned a lot about writing and deploying a real server-side web app in Swift. Additionally we could also contribute new features and idea to the Vapor ecosystem and will continue doing so.

You can find the source code of swiftybeaver.com written in Swift here and it should act as inspiration and starting point for your own server-side Swift adventures.

Conclusion

Swift on the server is great and just feels right. Using a statically typed programming language makes writing web apps so much safer and more fun and it is even faster than Node.js.

The team and I are super excited to help to pave the way for a bright future of Swift, be it on the server or the client and I am inviting all of you to try it for yourself.

See you in our Slack channel!

Sebastian

--

--

Sebastian Kreutzberger
SwiftyBeaver Blog

Creator of SwiftyBeaver, Co-founder & CTO at YGO. Prior CTO / CEO / founder of several VC-backed companies, including Wunderlist & RhodeCode.