How I used slack to create a remote logger for my server and applications

As a iOS Developer I always face the problem that in order to see the logger of any app when running on a device I had to connect it with Mac.

When Swift became open sourced and the posibility to write server applications open, I face the problem that I wanted to get a notification when there was an error on my server.

To solve both of the above problem, I decided that I had to find a way to create some kind of remote logger. The first though was to create something from scratch. Once I listed the requirements and I saw the work involved, I looked at other alternatives.

The application side, wheather it was an iOS Application, Web Application or anything that needs to log was the easy part. I just had to make a URL Request with all the details needed and the rest will be taken care from the logging server.

The logging server was a bit more tricky. First, I had to create a server and host it somewhere. The I had to create a client to access the server. Support searches, notifications, etc.

There had to be an easier way. It was when I though of Slack. The server is already there, there is an API to access it, search functionality is ready, sharing is ready, etc. It basically has all the things I need and probably a lot more.

Setting Up Slack

  1. Create a new channel

This is very easy, just go to your slack channels and click on add

2. Create a webhook

We need a way to access the channel from our application. The easiest way is through a webhook.

Use the above link and click on “Incoming Webhook Intergration”.

From there select the channel we created on the first step and press “Add Incoming WebHooks intergration”. After that you will get your Webhook URL, which we will use in the following steps to create the connection. The URL looks like:

https://hooks.slack.com/services/<some token>/<some other token>

3. Create the SlackLogger

Now that we have the Webhook setup, we need a convenient and easy way to access it. So I created the following classes. They are specific to Kitura Webserver, but the can be easily modified to be used in all scenarios. What makes the specific is that I call the URL through KituraRequest. This can be replaced with URLSession for iOS.

4. Setting Up

Once everything is in place, use the following lines to setup the SlackController:

SlackController.shared.slackWebhook = “<replace with Webhook URL>"
SlackController.shared.slackErrorWebhook = “<replace with Webhook URL>"

I am using 2 web hooks, one for every log and another for error logs. The reason is that I will only monitor the first one when needed, but on the error one I want to be notified at all times. So on the first one i have the notifications off and on the error one I have them on.

5. Sending a log line — directly

You can either do it by directly accessing SlackController:

SlackController.shared.log(type, msg: msg, functionName: functionName, lineNum: lineNum, fileName: fileName)

Or by using the LoggerAPI, as I will explain in the next points

6. Sending a log line - Integrating to the LoggerAPI

We first set it up with LoggerAPI. To do that we initialize Logger API with:

let logLevel = LoggerMessageType.debug
SlackLogger.use(logLevel, localLogger: HeliumLogger(logLevel))

We then use LoggerAPI as we would normally use it:

Log.info(“Server will be started on ‘\(controller.url)’.”)

Which will be logger locally and only on the normal slack logger

Log.error(error.localizedDescription)

Which will be logger locally and both on the normal and error slack logger

Summary

This is an easy way to send logs from your application to slack. Once I find some free time I will create it as a framework and open sourced it on github.