Skip to content

Instantly share code, notes, and snippets.

@akramhussein
Created August 11, 2015 15:07
Show Gist options
  • Save akramhussein/3c4665aabe15cfafbac2 to your computer and use it in GitHub Desktop.
Save akramhussein/3c4665aabe15cfafbac2 to your computer and use it in GitHub Desktop.
CocoaLumberjack Custom Crashlytics Logger in Swift
import Foundation
import CocoaLumberjack
import Crashlytics
class CrashlyticsLogger : DDAbstractLogger
{
static let sharedInstance = CrashlyticsLogger()
private var _logFormatter : DDLogFormatter?
override var logFormatter: DDLogFormatter? {
get {
return _logFormatter
}
set {
_logFormatter = newValue
}
}
override func logMessage(logMessage: DDLogMessage)
{
guard let formatter = self.logFormatter
else
{
print("CrashlyticsLogger: No formatter")
return
}
let formattedMessage = formatter.formatLogMessage(logMessage)
CLSLogv(formattedMessage, getVaList([]))
}
}
@akramhussein
Copy link
Author

To use:

    let crashlyticsLogger = CrashlyticsLogger.sharedInstance
    crashlyticsLogger.logFormatter = CustomLogFormatter()
    DDLog.addLogger(crashlyticsLogger)

@ahernandezlopez
Copy link

CLSLogv is crashing if message contains % characters. formattedMessage.stringByRemovingPercentEncoding solves that issue

@jpmhouston
Copy link

Removing or escaping the % signs isn't correct either. The Crashlytics docs say:

One important thing to note when using Swift is that the format argument of CLSLog, and NSLog, must be a compile-time constant string. While this is enforced by the compiler in Objective-C, it is currently lost in the bridging to Swift at the moment. A possible solution:

func write(string: String) {
   CLSLogv("%@", getVaList([string]))
}

@djvistasa
Copy link

Tried to implement this but am wondering where CustomLogFormatter() is defined?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment