来自部署在设备上的应用程序的 iOS os.log

dev*_*rfu 5 xcode logging ios swift

我正在使用新os.log事物登录我的应用程序,如下所示:

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

Console.app当我从 Xcode 调试应用程序时,日志按预期显示。但是是否有可能以某种方式从部署在设备上的应用程序实例中检索记录的字符串?我正在“现场”测试我的应用程序,远离笔记本电脑,并希望将收集的日志转储到文本文件中。

我是否需要以某种方式配置记录器以持久存储日志,或者只能从已部署的应用程序获取崩溃报告?

注意:我使用的是Swift 4 / Xcode 9+

Rab*_*s G 3

我相信你可以用这个答案解决这个问题:https ://stackoverflow.com/a/13303081/2136375

总结一下:

步骤1

添加以下代码以didFinishLaunchingWithOptions使应用程序能够记录到文件:

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)
Run Code Online (Sandbox Code Playgroud)

第2步

在添加中info.plist

备选方案 1

作为属性列表项:

“应用程序支持 iTunes 文件共享”并设置为 YES。

另选 2

作为源代码:

<key>UIFileSharingEnabled</key>
<true/>
Run Code Online (Sandbox Code Playgroud)