7

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+

devforfu
  • 1,570
  • 1
  • 19
  • 41
  • See https://stackoverflow.com/questions/44537133/how-to-write-application-logs-to-file-and-get-them – matt May 21 '18 at 16:06
  • According to online info, the `os_log` logs are stored in _/var/db/diagnostics/_ on the device and you can retrieve them from there later and open them in the Console. But I have not tried this myself. – matt May 21 '18 at 16:12
  • @matt But it seems this guy creates a custom solution to manually write logs into a specific file. I would like to use `os.log` instead =) – devforfu May 21 '18 at 16:44
  • @matt Interesting enough that there is no _/var/db/diagnostics/_ path in app's container. – devforfu May 21 '18 at 16:45
  • Did you ever figure this out? – daniel Nov 14 '18 at 23:25

1 Answers1

3

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/>
Rabs G
  • 1,300
  • 1
  • 11
  • 16
  • This works great. Just import os, and use os_log. Download the app's container, or use iTunes file sharing. – barbazoo Feb 09 '20 at 03:30