我正在使用新的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)
}
}
当我从 Xcode 调试应用程序时,日志按预期显示在Console.app
中。但是,是否可以以某种方式从设备上部署的应用程序实例中检索记录的字符串?我正在"现场"测试我的应用程序,远离笔记本电脑,并希望将收集的日志转储到文本文件中。
我是否需要以某种方式配置记录器来永久存储日志,或者只能从已部署的应用程序获取崩溃报告?
注意:我正在使用Swift 4/Xcode 9+
我相信你可以用这个答案解决这个问题: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)
步骤 2
在info.plist
中添加:
替代项1
作为属性列表项:
"应用程序支持 iTunes 文件共享"并设置为"是"。
替代项2
作为源代码:
<key>UIFileSharingEnabled</key>
<true/>