From 86a8a555e1df14b3a62d6b8c9b676e2446780f79 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto <5731772+marandaneto@users.noreply.github.com> Date: Fri, 9 Feb 2024 10:19:25 +0100 Subject: [PATCH] fix and improve logging if the `debug` flag is enabled (#104) --- CHANGELOG.md | 4 ++++ PostHog/PostHogApi.swift | 24 ++++++++++++++++++------ PostHog/PostHogQueue.swift | 2 +- PostHog/PostHogSDK.swift | 1 + PostHogExample/AppDelegate.swift | 1 + 5 files changed, 25 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a362adeba..2a6b74de1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## Next +## 3.1.2 - 2024-02-09 + +- fix and improve logging if the `debug` flag is enabled [#104](https://github.com/PostHog/posthog-ios/pull/104) + ## 3.1.1 - 2024-02-08 - `Application Opened` respects the `captureApplicationLifecycleEvents` config. [#102](https://github.com/PostHog/posthog-ios/pull/102) diff --git a/PostHog/PostHogApi.swift b/PostHog/PostHogApi.swift index 51aad1dcd..738b7b89d 100644 --- a/PostHog/PostHogApi.swift +++ b/PostHog/PostHogApi.swift @@ -27,6 +27,7 @@ class PostHogApi { func batch(events: [PostHogEvent], completion: @escaping (PostHogBatchUploadInfo) -> Void) { guard let url = URL(string: "batch", relativeTo: config.host) else { + hedgeLog("Malformed batch URL error.") return completion(PostHogBatchUploadInfo(statusCode: nil, error: nil)) } @@ -50,6 +51,7 @@ class PostHogApi { do { data = try JSONSerialization.data(withJSONObject: toSend) } catch { + hedgeLog("Error parsing the batch body: \(error)") return completion(PostHogBatchUploadInfo(statusCode: nil, error: error)) } @@ -57,22 +59,23 @@ class PostHogApi { do { gzippedPayload = try data!.gzipped() } catch { + hedgeLog("Error gzipping the batch body: \(error).") return completion(PostHogBatchUploadInfo(statusCode: nil, error: error)) } URLSession(configuration: config).uploadTask(with: request, from: gzippedPayload!) { data, response, error in if error != nil { + hedgeLog("Error calling the batch API: \(String(describing: error)).") return completion(PostHogBatchUploadInfo(statusCode: nil, error: error)) } let httpResponse = response as! HTTPURLResponse if !(200 ... 299 ~= httpResponse.statusCode) { - do { - try hedgeLog("Error sending events to PostHog: \(JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any])") - } catch { - hedgeLog("Error sending events to PostHog") - } + let errorMessage = "Error sending events to batch API: status: \(httpResponse.statusCode), body: \(String(describing: try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any]))." + hedgeLog(errorMessage) + } else { + hedgeLog("Events sent successfully.") } return completion(PostHogBatchUploadInfo(statusCode: httpResponse.statusCode, error: error)) @@ -90,6 +93,7 @@ class PostHogApi { urlComps.queryItems = [URLQueryItem(name: "v", value: "3")] guard let url = urlComps.url(relativeTo: config.host) else { + hedgeLog("Malformed decide URL error.") return completion(nil, nil) } @@ -110,25 +114,33 @@ class PostHogApi { do { data = try JSONSerialization.data(withJSONObject: toSend) } catch { + hedgeLog("Error parsing the decide body: \(error)") return completion(nil, error) } URLSession(configuration: config).uploadTask(with: request, from: data!) { data, response, error in if error != nil { + hedgeLog("Error calling the decide API: \(String(describing: error))") return completion(nil, error) } let httpResponse = response as! HTTPURLResponse if !(200 ... 299 ~= httpResponse.statusCode) { + let errorMessage = "Error calling decide API: status: \(httpResponse.statusCode), body: \(String(describing: try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any]))." + hedgeLog(errorMessage) + return completion(nil, - InternalPostHogError(description: "/decide returned a non 2xx status: \(httpResponse.statusCode)")) + InternalPostHogError(description: errorMessage)) + } else { + hedgeLog("Decide called successfully.") } do { let jsonData = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any] completion(jsonData, nil) } catch { + hedgeLog("Error parsing the decide response: \(error)") completion(nil, error) } }.resume() diff --git a/PostHog/PostHogQueue.swift b/PostHog/PostHogQueue.swift index fe6302a82..f5f51507f 100644 --- a/PostHog/PostHogQueue.swift +++ b/PostHog/PostHogQueue.swift @@ -118,7 +118,7 @@ class PostHogQueue { do { try reachability?.startNotifier() } catch { - hedgeLog("Error: Unable to monitor network reachability") + hedgeLog("Error: Unable to monitor network reachability: \(error)") } #endif } diff --git a/PostHog/PostHogSDK.swift b/PostHog/PostHogSDK.swift index 4d827c649..206af9409 100644 --- a/PostHog/PostHogSDK.swift +++ b/PostHog/PostHogSDK.swift @@ -72,6 +72,7 @@ private let sessionChangeThreshold: TimeInterval = 60 * 30 @objc public func setup(_ config: PostHogConfig) { setupLock.withLock { + toggleHedgeLog(config.debug) if enabled { hedgeLog("Setup called despite already being setup!") return diff --git a/PostHogExample/AppDelegate.swift b/PostHogExample/AppDelegate.swift index 88b338e31..aa5a36e4c 100644 --- a/PostHogExample/AppDelegate.swift +++ b/PostHogExample/AppDelegate.swift @@ -19,6 +19,7 @@ class AppDelegate: NSObject, UIApplicationDelegate { config.captureApplicationLifecycleEvents = false config.flushAt = 1 config.flushIntervalSeconds = 10 + config.debug = true PostHogSDK.shared.setup(config) // PostHogSDK.shared.debug()