Swift

LogHive provides a simple REST API that you can use to push events and errors. Here is an example code for Swift:

Push Event

let url = URL(string: "https://api.loghive.app/v1/event/add")!

var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("YourPersonalApiKey", forHTTPHeaderField: "ApiKey")
request.addValue("application/json", forHTTPHeaderField: "Content-Type")

let json: [String: Any] = [
    "project": "yourprojectname",
    "group": "yourgroupname",
    "event": "your-event-name",
    "description": "your-description",
    "notify": false
]

let jsonData = try! JSONSerialization.data(withJSONObject: json, options: [])

request.httpBody = jsonData

let task = URLSession.shared.dataTask(with: request) { data, response, error in
    if let error = error {
        print("Error: \(error)")
        return
    }

    guard let response = response as? HTTPURLResponse,
        (200...299).contains(response.statusCode) else {
            print("Server error")
            return
    }

    if let data = data,
        let jsonString = String(data: data, encoding: .utf8) {
        print("Response: \(jsonString)")
    }
}

task.resume()

Push Insight

let url = URL(string: "https://api.loghive.app/v1/insight/add")!

var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("YourPersonalApiKey", forHTTPHeaderField: "ApiKey")
request.addValue("application/json", forHTTPHeaderField: "Content-Type")

let json: [String: Any] = [
    "project": "MySaas",
    "insight": "system1-online",
    "value": 1
]

let jsonData = try! JSONSerialization.data(withJSONObject: json, options: [])

request.httpBody = jsonData

let task = URLSession.shared.dataTask(with: request) { data, response, error in
    if let error = error {
        print("Error: \(error)")
        return
    }

    guard let response = response as? HTTPURLResponse,
        (200...299).contains(response.statusCode) else {
            print("Server error")
            return
    }

    if let data = data,
        let jsonString = String(data: data, encoding: .utf8) {
        print("Response: \(jsonString)")
    }
}

task.resume()

Last updated