Last active
December 9, 2017 02:12
-
-
Save muizidn/cf81fef1a2ab6e7b7b91e0ea25d0c18f to your computer and use it in GitHub Desktop.
This can be used to export any error log automatically to a text file. Feel free to use for your project!
This file contains hidden or 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
// SWIFT 4.0.1 | |
// XCODE 9.0.1 | |
#if DEBUG | |
var errorFile = "errorLog.txt" | |
// Error object must be conform to CustomStringConvertible protocol | |
func saveErrorLog<T: CustomStringConvertible>(error: T) { | |
// Fetch document directory URL | |
let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] | |
let path = documentDirectory.appendingPathComponent(errorFile).path | |
print("Document Directory path : \(path)") | |
var stringFromFile = "" | |
// If "errorFile" is exist then read | |
do { | |
stringFromFile = try String.init(contentsOfFile: path, encoding: .utf8) | |
}catch let error as NSError { | |
print(error.localizedDescription) | |
} | |
// Write | |
do { | |
try "\(stringFromFile)\(Date())\n\(error.description)\n\n".write(toFile: path, atomically: true, encoding: .utf8) | |
}catch let error as NSError { | |
print(error.localizedDescription) | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment