From 147a7300b6db591bbe52e208ca5f464d841de6ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Ku=C4=87?= Date: Wed, 3 Jun 2020 14:15:47 +0200 Subject: [PATCH] Ability to pause log sending and README update --- Example/Logsene/AppDelegate.swift | 2 +- Logsene.podspec | 2 +- Logsene/Classes/Core.swift | 16 ++++++++++++++++ Logsene/Classes/Worker.swift | 13 +++++++++++-- README.md | 18 ++++++++++++++++++ 5 files changed, 47 insertions(+), 4 deletions(-) diff --git a/Example/Logsene/AppDelegate.swift b/Example/Logsene/AppDelegate.swift index e94bb6e..4505920 100644 --- a/Example/Logsene/AppDelegate.swift +++ b/Example/Logsene/AppDelegate.swift @@ -9,7 +9,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // NOTE: Set your token below try! LogseneInit("", type: "example") - + // Here we setup CocoaLumberjack to log to both XCode console and Logsene DDLog.add(DDTTYLogger.sharedInstance!) DDLog.add(LogseneLogger()) diff --git a/Logsene.podspec b/Logsene.podspec index a879fad..5d5bdd6 100644 --- a/Logsene.podspec +++ b/Logsene.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'Logsene' - s.version = '1.3.4' + s.version = '1.3.5' s.summary = 'Sematext Cloud Logs is ELK as a Service. This library lets you collect mobile analytics and log data from your iOS applications.' s.description = <<-DESC diff --git a/Logsene/Classes/Core.swift b/Logsene/Classes/Core.swift index dbbf115..056deb9 100644 --- a/Logsene/Classes/Core.swift +++ b/Logsene/Classes/Core.swift @@ -78,6 +78,22 @@ public func LogseneSetDefaultMeta(_ meta: JsonObject?) { Logsene.defaultMeta = meta } +/** + Pauses sending logs until ```LogseneResumeSendingLogs()``` is called. +*/ +public func LogsenePauseSendingLogs() { + NSLog("Pausing the logs sending process") + Logsene.worker?.pause() +} + +/** + Resumes paused logs sending. +*/ +public func LogseneResumeSendingLogs() { + NSLog("Resuming the logs sending process") + Logsene.worker?.resume(); +} + /** Logs an event. diff --git a/Logsene/Classes/Worker.swift b/Logsene/Classes/Worker.swift index 66bf706..c20abac 100644 --- a/Logsene/Classes/Worker.swift +++ b/Logsene/Classes/Worker.swift @@ -22,6 +22,7 @@ class Worker: NSObject { fileprivate let type: String fileprivate let reach: Reachability fileprivate var isOnline: Bool = true + fileprivate var isActive: Bool = true fileprivate var locationManager: CLLocationManager? = nil init(client: LogseneClient, type: String, maxOfflineMessages: Int, automaticLocationEnriching: Bool, useLocationOnlyInForeground: Bool) throws { @@ -93,17 +94,25 @@ class Worker: NSObject { } } } + + func resume() { + self.isActive = true + } + + func pause() { + self.isActive = false + } fileprivate func handleNewEvent(_ event: JsonObject) throws { try preflightBuffer.add(event) - if preflightBuffer.count >= minBatchSize && isOnline { + if preflightBuffer.count >= minBatchSize && isOnline && isActive { try sendInBatches() } } fileprivate func handleTimerTick() throws { - if isOnline { + if isOnline && isActive { try sendInBatches() } } diff --git a/README.md b/README.md index 9ee19e4..b1441f3 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,7 @@ A few predefined meta fields are included in each event sent to Sematext. The fi - versionCode (app build number, eg. 92) - osRelease (iOS version, eg. 9.3.0) - uuid (device identifier) +- OS type (iOS) You can set your own meta fields with `LogseneSetDefaultMeta`. For example: @@ -85,6 +86,23 @@ LogseneSetDefaultMeta(["user": "user@example.com", "plan": "free"]) Note that these meta fields are global, and will be attached to every event sent to Logsene. +Pausing & Resuming Logs Sending +---------------------------------------- + +The library can be instructed to stop sending logs on demand. To do that you need to call the following function: + +```swift +LogsenePauseSendingLogs() +``` + +Logs sending can be resumed by calling the following function: + +```swift +LogseneResumeSendingLogs() +``` + +Note that the logs that are in the buffer and were waiting to be sent at the time of pausing will not be sent until the logs sending process is resumed. + Centralized Logging -------------------