I am using new os.log
thing for logging in my app as follows:
import os.log
class ViewController: UIViewController {
// a bunch of methods
}
extension OSLog {
static let ui = OSLog(subsystem: "io.my.app", category: "ui")
}
extension ViewController {
func upload(locations: [LocationProtocol]) {
os_log("Sending locations to server", log: .ui, type: .debug)
self.server_api.send(locations)
}
}
The logs are shown in Console.app
as expected when I am debugging the app from Xcode. But is it possible to somehow retrieve logged strings from an app's instance deployed on a device? I am testing my app "in the field", away from the laptop, and wanted to dump gathered logs into a text file.
Do I need to configure loggers somehow to persistently store logs, or it is only possible to get crash reports from a deployed app?
Note: I am using Swift 4 / Xcode 9+
I believe that you can solve this with this answer: https://stackoverflow.com/a/13303081/2136375
To summarize:
Add to the following code to didFinishLaunchingWithOptions
to enable the app to log to a file:
var paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = paths[0]
let deviceName = UIDevice.current.name
let fileName = "\(deviceName) - \(Date()).log"
let logFilePath = (documentsDirectory as NSString).appendingPathComponent(fileName) freopen(logFilePath.cString(using: String.Encoding.ascii)!, "a+", stderr)
In the info.plist
add:
As property list item:
"Application supports iTunes file sharing" and set to YES.
As source code:
<key>UIFileSharingEnabled</key>
<true/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With