Created
March 13, 2018 13:51
-
-
Save fassko/2e4852095a18498f8cd7a7c34b5e1836 to your computer and use it in GitHub Desktop.
XCGLogger loggly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// LogglyDestination.swift | |
// qminder-tv | |
// | |
// Created by Kristaps Grinbergs on 03/01/2018. | |
// Copyright © 2018 Qminder. All rights reserved. | |
// | |
import Foundation | |
import XCGLogger | |
open class LogglyDestination: BaseQueuedDestination { | |
/// Loggly URL | |
fileprivate let logglyUrlString = "http://logs-01.loggly.com/bulk/KEY/tag/TAG/" | |
/// Max entries before sending to loggly | |
fileprivate let maxEntriesInBuffer = 5 | |
/// Loggly queue | |
fileprivate let logglyQueue = DispatchQueue(label: "logglyQueue") | |
/// Message buffer | |
fileprivate var buffer: [String] = [String]() | |
/// Observer | |
fileprivate var observer: NSObjectProtocol? | |
/// Background task identifier | |
fileprivate var backgroundTaskIdentifier: UIBackgroundTaskIdentifier = UIBackgroundTaskInvalid | |
/// Standard fields to send to loggly | |
fileprivate lazy var standardFields: [String: Any] = { | |
var dict = [String: Any]() | |
dict["lang"] = Locale.preferredLanguages[0] | |
if let infodict = Bundle.main.infoDictionary { | |
if let appname = infodict["CFBundleName"] { | |
dict["appname"] = appname | |
} | |
if let build = infodict["CFBundleVersion"] { | |
dict["build"] = build | |
} | |
if let version = infodict["CFBundleShortVersionString"] { | |
dict["version"] = version | |
} | |
} | |
dict["devicename"] = UIDevice.current.name | |
dict["devicemodel"] = UIDevice.current.model | |
dict["osversion"] = UIDevice.current.systemVersion | |
dict["sessionid"] = String(arc4random_uniform(999999)) | |
return dict | |
}() | |
/** | |
Init Logger | |
- Parameters: | |
- owner: XCGLogger | |
- identifier: Identifier | |
*/ | |
public override init(owner: XCGLogger?, identifier: String) { | |
super.init(owner: owner, identifier: identifier) | |
observer = NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "UIApplicationWillResignActiveNotification"), | |
object: nil, queue: nil, using: {[unowned self] _ in | |
let tmpbuffer = self.buffer | |
self.buffer = [String]() | |
self.backgroundTaskIdentifier = UIApplication.shared.beginBackgroundTask(withName: "saveLogRecords") { | |
self.endBackgroundTask() | |
} | |
self.sendLogsInBuffer(stringbuffer: tmpbuffer) | |
}) | |
} | |
/** | |
End background task | |
*/ | |
private func endBackgroundTask() { | |
if self.backgroundTaskIdentifier != UIBackgroundTaskInvalid { | |
UIApplication.shared.endBackgroundTask(self.backgroundTaskIdentifier) | |
self.backgroundTaskIdentifier = UIBackgroundTaskInvalid | |
print("Ending background task") | |
} | |
} | |
/** | |
Send logs in bufer | |
- Parameters: | |
- stringbuffer: String buffer | |
*/ | |
private func sendLogsInBuffer(stringbuffer: [String]) { | |
let allMessagesString = stringbuffer.joined(separator: "\n") | |
if let allMessagesData = allMessagesString.data(using: .utf8) { | |
guard let url = URL(string: logglyUrlString) else { return } | |
var request = URLRequest(url: url) | |
request.httpMethod = "POST" | |
request.httpBody = allMessagesData | |
let task = URLSession.shared.dataTask(with: request) | |
task.resume() | |
} | |
} | |
/** | |
Write override | |
- Parameters: | |
- message: Log message | |
*/ | |
override open func write(message: String) { | |
var msgDictionary = standardFields | |
msgDictionary["message"] = message | |
guard let jsonData = try? JSONSerialization.data(withJSONObject: msgDictionary, options: JSONSerialization.WritingOptions(rawValue: 0)) else { return } | |
guard let jsonString = String(data: jsonData, encoding: .utf8) else { return } | |
addLogMsgToBuffer(message: jsonString) | |
} | |
/** | |
Add log message to buffer | |
- Parameters: | |
- message: Log message | |
*/ | |
private func addLogMsgToBuffer(message: String) { | |
logglyQueue.async { | |
self.buffer.append(message) | |
if self.buffer.count > self.maxEntriesInBuffer { | |
let tmpbuffer = self.buffer | |
self.buffer = [String]() | |
self.sendLogsInBuffer(stringbuffer: tmpbuffer) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment