Skip to content

Commit

Permalink
Merge pull request #14 from sematext/allow_stopping_logs_sending
Browse files Browse the repository at this point in the history
Ability to pause log sending and README update
  • Loading branch information
Rafał Kuć authored Jun 3, 2020
2 parents 289044f + 147a730 commit ec99bfb
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Example/Logsene/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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("<your-token>", type: "example")

// Here we setup CocoaLumberjack to log to both XCode console and Logsene
DDLog.add(DDTTYLogger.sharedInstance!)
DDLog.add(LogseneLogger())
Expand Down
2 changes: 1 addition & 1 deletion Logsene.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions Logsene/Classes/Core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 11 additions & 2 deletions Logsene/Classes/Worker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}
}
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -85,6 +86,23 @@ LogseneSetDefaultMeta(["user": "[email protected]", "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
-------------------

Expand Down

0 comments on commit ec99bfb

Please sign in to comment.