Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix and improve logging if the debug flag is enabled #104

Merged
merged 2 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
24 changes: 18 additions & 6 deletions PostHog/PostHogApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand All @@ -50,29 +51,31 @@ class PostHogApi {
do {
data = try JSONSerialization.data(withJSONObject: toSend)
} catch {
hedgeLog("Error parsing the batch body: \(error)")
return completion(PostHogBatchUploadInfo(statusCode: nil, error: error))
}

var gzippedPayload: Data?
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))
Expand All @@ -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)
}

Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion PostHog/PostHogQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions PostHog/PostHogSDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions PostHogExample/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down