Integration with CocoaLumberjack
CocoaLumberjack Introduction
CocoaLumberjack is a fast & simple, yet powerful & flexible logging framework for Mac and iOS. It is a popular logging framework that is used in many iOS and Mac projects. One of the main features of CocoaLumberjack is its ability to log to multiple destinations at the same time, which makes it a great choice for logging in production apps. It is also highly customizable and extensible, which makes it a great choice for developers who need a logging framework that can be tailored to their specific needs.
In GitHub, CocoaLumberjack has the most stars among all logging frameworks that are compatible with Swift.
Integrating with CocoaLumberjack
To integrate Shipbook with CocoaLumberjack, you need to create a new class that extends DDAbstractLogger
and implements the log
method. This class will be used to send logs to Shipbook. Then, you need to add an instance of this class to CocoaLumberjack's DDLog
class.
Installation
Just create a new file class called DDShipbookLogger
and add copy the followingcode:
import CocoaLumberjackSwift
import ShipBookSDK
class DDShipbookLogger: DDAbstractLogger {
fileprivate static let _instance = DDShipbookLogger()
public static var shared: DDShipbookLogger {
return _instance
}
fileprivate override init() {
}
override func log(message logMessage: DDLogMessage) {
let severity: Severity?;
switch logMessage.flag {
case DDLogFlag.verbose:
severity = .Verbose
case DDLogFlag.debug:
severity = .Debug
case DDLogFlag.info:
severity = .Info
case DDLogFlag.warning:
severity = .Warning
case DDLogFlag.error:
severity = .Error
default:
severity = .Info
}
Log.message(msg: logMessage.message, severity: severity!, function: logMessage.function ?? "", file: logMessage.fileName , line: Int(logMessage.line))
}
override public var loggerName: DDLoggerName {
get {
return DDLoggerName("io.shipbook.cocoalumberjack")
}
}
}
Usage
Then add the following code to your AppDelegate
or app
class to start Shipbook and add the logger to CocoaLumberjack:
import CocoaLumberjackSwift
import ShipBookSDK
...
ShipBook.start(appId:"YOUR_APP_ID", appKey:"YOUR_APP_KEY")
DDLog.add(DDShipbookLogger.shared)
Now you can use CocoaLumberjack as you normally would and all logs will be sent to Shipbook.
DDLogVerbose("Verbose")
DDLogDebug("Debug")
DDLogInfo("Info")
DDLogWarn("Warn")
DDLogError("Error")