Skip to content

Instantly share code, notes, and snippets.

@akramhussein
Created August 11, 2015 15:07
Show Gist options
  • Select an option

  • Save akramhussein/3c4665aabe15cfafbac2 to your computer and use it in GitHub Desktop.

Select an option

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
Copy Markdown
Author

To use:

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

@ahernandezlopez

Copy link
Copy Markdown

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

@jpmhouston

Copy link
Copy Markdown

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
Copy Markdown

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