Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS os.log from an app deployed on device

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+

like image 584
devforfu Avatar asked May 21 '18 15:05

devforfu


1 Answers

I believe that you can solve this with this answer: https://stackoverflow.com/a/13303081/2136375

To summarize:

Step 1

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)

Step 2

In the info.plist add:

Alt 1

As property list item:

"Application supports iTunes file sharing" and set to YES.

Alt 2

As source code:

<key>UIFileSharingEnabled</key>
<true/>
like image 165
Rabs G Avatar answered Oct 14 '22 04:10

Rabs G