Created
August 11, 2015 15:07
-
-
Save akramhussein/3c4665aabe15cfafbac2 to your computer and use it in GitHub Desktop.
CocoaLumberjack Custom Crashlytics Logger in Swift
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
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([])) | |
} | |
} |
CLSLogv
is crashing if message contains %
characters. formattedMessage.stringByRemovingPercentEncoding
solves that issue
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])) }
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
To use: