Centralise Swift logs using log.jod.li

log.jod.li

It often happens that we need to gather logs from several sources. Centralising them in one simple server place is the purpose of the log.jod.li initiative. The logs are then visible almost instantaneously on the log.jod.li page or can be retrieved in a format similar to CSV (raw logs). For more details, please visit https://log.jod.li .

Swift situation

This page aims at showing how to use log.jod.li logs centralisation solution using Swift code.

Let’s say you have some Swift code that may not always run as expected. You want to track the unwanted outcomes and centralise their reading on log.jod.li . You could read your Swift logs from Swift dedicated solutions but you would not be able to see the logs at the same place from your Swift programs and those written in completely different technologies. Also, Swift can now be used on Android as well through the Scade.io solution. So, if you wish to centralise your logs from Swift program, you can use a simple GET call to a URL containing:

  • The server name to send the log to the right location. This will be en.log.jod.li .
  • The user name you created on log.jod.li .
  • The user key you received in your email box when you created your user. Make sure nobody else gets access to your key.
  • The log message that you want to record on the log.jod.li server. The maximum length of the message is 1700 characters. It must be passed url encoded so that the string is interpreted correctly by the server.
  • Optionally the application name so that you can filter your logs more easily. Max 32 characters.
  • Optionally the client guid so that you can filter on a given client. Max 32 characters.

Here is an example of URL:

https://en.log.jod.li/?api=a&u=toto&k=1234567890123456&a=AppName&c=ClientGUID&m=testing

Here, the user is “toto”, the key is “1234567890123456”, the application name is “AppName”, the client guid is “ClientGUID” and the log message is “testing”.

Send log with a GET call

What we can do to make use of log.jod.li in this case is using a simple function to  call a URL similar to the one mentioned above. Here is the function:

    func logThroughGet(message: String){
        let urlencodedmessage = message.addingPercentEncoding(withAllowedCharacters: .alphanumerics)
        let url = URL(string: "https://en.log.jod.li/?api=a&u=toto&k=1234567890123456&c=ClientGUID&a=AppName&m=\(urlencodedmessage!)")!
        
        let task = URLSession.shared.dataTask(with: url) {(data, response, error) in
            guard let data = data else { return }
            print(String(data: data, encoding: .utf8)!)
        }
        
        task.resume()
    }

Note that you may need to import Foundation for this function to work.

The function can then be called this way:

logThroughGet(message: "Message sent through GET")

Here is the outcome of the call on log.jod.li web page:

2018-09-21 19:06:33 ClientGUID AppName Message sent through GET

Here is the outcome in the raw CSV format:

2018-09-21 19:06:33,ClientGUID,AppName,Message sent through GET

Send log with a POST call

What we can do to make use of log.jod.li  is using a simple function to  call a URL similar to the one mentioned above but with a POST method. Using a POST method allows longer messages. Here is the function:

    func logThroughPost(message: String){
        let url = URL(string: "https://en.log.jod.li")
        var request = URLRequest(url: url!)
        request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
        request.httpMethod = "POST"
        let urlencodedmessage = message.addingPercentEncoding(withAllowedCharacters: .alphanumerics)
        let postString = "api=a&u=toto&k=1234567890123456&c=ClientGUID&a=AppName&m=\(urlencodedmessage!)"
        request.httpBody = postString.data(using: .utf8)
        let session = URLSession(configuration:URLSessionConfiguration.default, delegate: nil, delegateQueue: nil)
        let dataTask = session.dataTask(with:request) { (data, response, error) -> Void in
            if error != nil{
                print("Error while getting server response: \(error!)")
            }
            else{
                if((response as! HTTPURLResponse).statusCode != 200){
                    print("Error: status code is not 200: \((response as! HTTPURLResponse).statusCode)")
                }
                else
                {
                    if(response!.mimeType! != "text/html"){
                        print("Not printing out, mimeType is not text/html but: \(response!.mimeType!)")
                    }
                    else{
                        print(String(decoding: data!, as: UTF8.self))
                    }
                }
            }
        }
        dataTask.resume()
    }

Note that you may need to import Foundation for this function to work.

The function can then be called this way:

logThroughGet(message: "Message sent through POST")

Here is the outcome of the call on log.jod.li web page:

2018-09-21 19:06:33	ClientGUID	AppName	Message sent through POST

Here is the outcome in the raw CSV format:

2018-09-21 19:06:33,ClientGUID,AppName,Message sent through POST

Conclusion

This is a demonstration that you can log your messages by simply calling a url from Swift code. This is especially useful if you have several technologies for which you need to read logs.